Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Detecting Last line of a file

by data64 (Chaplain)
on Jun 18, 2005 at 14:46 UTC ( [id://467988]=note: print w/replies, xml ) Need Help??


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."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://467988]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2024-03-29 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found