Path::Tiny handles opening and closing and doing different things to a file. Its methods include spew(), but the module's own documentation recommends using append() for what you want since it locks the file and overwrites it, whereas spew() creates a temporary copy.
[18:25][nick:~/monks]$ cat 1135741.pl #! perl -w use strict; use feature qw/ say /; use Path::Tiny qw/ path /; my $file = 'test.dat'; my @lines = path( $file )->lines( { chomp => 1 } ); # do something with @lines ... my @append = (); # empty array path( $file )->append( @append, { truncate => 1 } ); say "Done."; __END__
[18:25][nick:~/monks]$ cat test.dat foo bar baz quux
[18:26][nick:~/monks]$ perl 1135741.pl Done.
[18:26][nick:~/monks]$ cat test.dat [18:26][nick:~/monks]$
In reply to Re^3: Clear the contents of the text file
by 1nickt
in thread Clear the contents of the text file
by shroh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |