Reputation: 6106
I was wondering whether it is possible to remove the last line that was written in the output file with [print OUT "blabla";]
in perl? Many thanks in advance!
Upvotes: 0
Views: 3670
Reputation: 46395
See: In Perl, how do I change, delete, or insert a line in a file, or append to the beginning of a file?
Upvotes: 4
Reputation: 2340
Another suggestion: Defer printing the line until you know that you need to print it.
Upvotes: 2
Reputation: 12979
This will delete the last line from a file:
open (FH, "+< $file") or die "can't update $file: $!";
while ( <FH> ) {
$addr = tell(FH) unless eof(FH);
}
truncate(FH, $addr) or die "can't truncate $file: $!";
Upvotes: 3