in reply to Detecting Last line of a file

To detect last line of file, use the "eof" function. Check perlfunc for more informtion about eof

Replies are listed 'Best First'.
Re^2: Detecting Last line of a file
by tphyahoo (Vicar) on Jun 18, 2005 at 15:38 UTC
    Yeah, this EOF seems like a good way to go. It's addressed in some, but not all (eg, not the activestate) perlfaq5:

    "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?

    .....

    The following code snippet deletes the last line of a file without making a copy or reading the whole file into memory:

    open (FH, "+< $file"); while ( <FH> ) { $addr = tell(FH) unless eof(FH) } truncate(FH, $addr);"
    Presumably you can modify this to do things other than deleting. Hope this helps!

    Or, you could do like the most recent perlfaq5 suggests and use Tie::File:

    "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file? Use the Tie::File module, which is included in the standard distribution since Perl 5.8.0."