开发者

Perl: how do I remove the last written line in an output file?

开发者 https://www.devze.com 2023-02-14 17:13 出处:网络
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 ch

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消