in reply to Empty a text file?

You could do it with more error checking.

my $file = 'file.dat'; open my $fh, '>', $file or die "Can't write '$file': $!"; close $fh or die "Close error: $!";

You could do it with a call to the shell. (If 'file.dat' is not really a literal, you have to watch out for shell metacharacters in the filename.)

system( "echo -n > file.dat" );

Or you could just use truncate:

truncate 'file.dat', 0 or die "Can't truncate file.dat: $!";

Replies are listed 'Best First'.
Re^2: Empty a text file?
by mr_mischief (Monsignor) on Apr 11, 2008 at 18:29 UTC
    OT, but FYI:

    If you're using that second one under Linux (under bash on Linux at least) then you're wasting a call to echo. This will work just as well:

    perl -e 'system q{>file.dat}'