Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Reading the last line of a file in a Pure Way.

by hackmare (Pilgrim)
on Aug 27, 2001 at 22:00 UTC ( #108194=perlquestion: print w/replies, xml ) Need Help??
hackmare has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

My ignorance is again shining brightly. it is well polished and lacks no luster.

I have only spartan knowledge of file seek, search, and rewind functionality and such things. I wonder if there is another way than reading the contents of a file to get the last line. I do not wish to load the file contents into an array as this would prov cumbersome.

I seek your wisdom in order to facilitate the following task: I wish to mimmick the soiled, non-Perl ways of shell, by implementing an equivalent of tail -1 myfile

This clearly would be an easy task for backticks, but I fear their demonic ways and wish to attempt to avoid them like the plage they can be.

The ignorant one.

--Hackmare.

Replies are listed 'Best First'.
Re: Reading the last line of a file in a Pure Way.
by runrig (Abbot) on Aug 27, 2001 at 22:05 UTC
Re: Reading the last line of a file in a Pure Way.
by idnopheq (Chaplain) on Aug 27, 2001 at 22:07 UTC
    Check this out.

    From Perl Power Tools, tail ...

    Here's a very complete version and its manpage by Thierry Bézecourt, and test generator with readme. This also implements xtail functionality through either a symlink or being called with tail -f

    HTH
    --
    idnopheq
    Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Re: Reading the last line of a file in a Pure Way.
by cLive ;-) (Prior) on Aug 27, 2001 at 22:08 UTC
Re: Reading the last line of a file in a Pure Way.
by blakem (Monsignor) on Aug 27, 2001 at 22:09 UTC

      Thanks for the pointer, blakem. Did the trick, and I'm on my way.

      There's one ref in the pointer you gave me that addresses a 'Pure' way to do it using seek. It seems that many the replies addressed all the ways to find the tail end of a file in an efficient way, but vnpandey gave the most helpful answer in Re: Is there a way to read a file backwards?

      That was great help.

      --Hackmare. The luster of my ignorance is dimming.

Re: Reading the last line of a file in a Pure Way.
by jmcnamara (Monsignor) on Aug 07, 2002 at 17:54 UTC

    This does tail -1 in a pure (evil) way:     perl -pe '${}}=${_}}{${_}=${}}' file

    --
    John.

        Please enlighten me

        how does this work and what is the }{

        I realise -p feeds all lines to the script, sets the ORS to each one but how is this suppressing printing and what is the magic }{

        Thanks if you spare the time

Re: Reading the last line of a file in a Pure Way.
by wog (Curate) on Aug 27, 2001 at 22:07 UTC
    my $last; { local $_; open FILE, "<myfile" or die "opening myfile: $!\n"; $last = $_ while <FILE>; close FILE; # last line in $last. }

    ((added:) ...read the file, keep track of each line read in $last. At the end, $last will contain the last line successfully read.)

Re: Reading the last line of a file in a Pure Way.
by Your Mother (Bishop) on Aug 23, 2004 at 20:35 UTC

    One more™

    use IO::All; my $file = shift || die "Give me the file, man!\n"; print io($file)->backwards->getline;

      Sigh. For some reason, every time I see a new example of IO::All, my feeling of "this is wrong" gets even stronger. I appreciate Ingy's valiant attempt for more DWIMmery, but a lot of his APIs give me the heebie jeebies.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://108194]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2018-07-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (465 votes). Check out past polls.

    Notices?