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__