How to create an empty file?

B

bill

What's the best way to create an empty file? I mean is there
something less circuitous than

{
my $inane;
open my $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

and that does not use system(), as in

system('touch', $empty);

?

Thanks!

bill
 
A

A. Sinan Unur

What's the best way to create an empty file? I mean is there
something less circuitous than

{
my $inane;
open my $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

D:\Home> perl -c t.pl
"my" variable $inane masks earlier declaration in same scope at t.pl
line 8.

Further, if we get rid of that:

D:\Home> cat t.pl
#! /usr/bin/perl

use strict;
use warnings;

open my $inane, '>', 'empty' and close $inane
or die "Failed to create empty: $!\n";

__END__

D:\Home> t
Global symbol "$inane" requires explicit package name at D:\Home\asu1
\UseNet\clp
misc\t.pl line 7.
Execution of D:\Home\asu1\UseNet\clpmisc\t.pl aborted due to compilation
errors.

This should have given you a clue that this is not such a great idea.

I am not sure what problem you are trying to solve, but if you find
yourself doing this frequently you probably want to put the correct code
in a sub:

#! /usr/bin/perl

use strict;
use warnings;

my $r = create_empty_file('inane');
die $r if $r;


sub create_empty_file {
eval {
open my $fh, '>', $_[0]
or die "Cannot create $_[0]: $!\n";
close $fh or die "Cannot close $_[0]: $!\n";
};
return $@;
}

__END__
 
A

axel

bill said:
What's the best way to create an empty file? I mean is there
something less circuitous than
{
my $inane;
open my $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

Not really, although you could chop the above down by a line:

{
open TMPFILE, '>', $empty and close TMPFILE
or die "File error with $empty: $!";
}

Axel
 
K

ko

bill said:
What's the best way to create an empty file? I mean is there
something less circuitous than

{
my $inane;
open my $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

and that does not use system(), as in

system('touch', $empty);

?

Thanks!

bill

perl -MExtUtils::Command -e touch nonexistent_file

HTH -keith
 
A

A. Sinan Unur

....


Way cool. Thanks!

Of course, you should be careful using it because all the subs in
ExtUtils::Command use @ARGV rather than subroutine arguments.

Sinan
 
T

Tintin

bill said:
What's the best way to create an empty file? I mean is there
something less circuitous than

{
my $inane;
open my $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

and that does not use system(), as in

system('touch', $empty);

Your two examples do different things (sometimes). Which is it to be?
 
A

Anno Siegel

Tintin said:
Your two examples do different things (sometimes). Which is it to be?

If the objective is to create an *empty* file, the "touch" example is
bogus anyway.

Anno
 
H

Heinrich Mislik

What's the best way to create an empty file? I mean is there
something less circuitous than

{
my $inane;
open my $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

use File::Slurp;
write_file('empty');

Cheers

Heinrich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top