Perl: how do I remove the last written line in an output file?
I was wo开发者_开发问答ndering 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!
See: In Perl, how do I change, delete, or insert a line in a file, or append to the beginning of a file?
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: $!";
Another suggestion: Defer printing the line until you know that you need to print it.
精彩评论