0

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!

DVK
  • 126,886
  • 32
  • 213
  • 327
Abdel
  • 5,826
  • 12
  • 56
  • 77
  • As in "undo the last print command" from within Perl? Or just remove the last line of the file after Perl's finished building it? – Marc B Mar 01 '11 at 18:32
  • I mean "undo last print command". Overwrite last print command would also do the trick for me. – Abdel Mar 01 '11 at 18:35
  • [`seek( $fh, -12, SEEK_CUR )`](http://perldoc.perl.org/functions/seek.html) – Brad Gilbert Mar 01 '11 at 18:39
  • possible duplicate of [In Perl, how do I change, delete, or insert a line in a file, or append to the beginning of a file?](http://stackoverflow.com/questions/4388304/in-perl-how-do-i-change-delete-or-insert-a-line-in-a-file-or-append-to-the-be) – Robert P Mar 02 '11 at 00:44

3 Answers3

4

See: In Perl, how do I change, delete, or insert a line in a file, or append to the beginning of a file?

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
3

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: $!";
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
2

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

David Harris
  • 2,332
  • 1
  • 13
  • 25