Abdel
Abdel

Reputation: 6106

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

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

Answers (3)

David Harris
David Harris

Reputation: 2340

Another suggestion: Defer printing the line until you know that you need to print it.

Upvotes: 2

FreeAsInBeer
FreeAsInBeer

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

Related Questions