Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: tail -1 emulation efficiency

by coec (Chaplain)
on May 06, 2004 at 02:33 UTC ( [id://350967]=note: print w/replies, xml ) Need Help??


in reply to tail -1 emulation efficiency

Have you looked at File::Tail? http://search.cpan.org/~mgrabnar/File-Tail-0.98/Tail.pm

Update

use File::Tail; my $ref=tie *FH,"File::Tail",(name=>$name, tail=>1); print "$_";

I don't have this module installed so this is untested.
CC

Replies are listed 'Best First'.
Re: Re: tail -1 emulation efficiency
by woodstea (Sexton) on May 06, 2004 at 02:50 UTC
    Well, File::Tail seemed to be mostly about an emulation of the "tail -f" idea, and I couldn't see in my first perusal how to just do the last line. Perhaps I'll have to look at the module's code itself. It doesn't seem like the POD docs are very clear on that point.
      and I couldn't see in my first perusal how to just do the last line.

      Quote simply: You can tell File::Tail to start reading at N lines from the end, then just let the object pass out of the scope:

      sub get_last_line { my $name=shift @_; my $file=File::Tail->new(name=>$name,tail=>1); return $file->read; }
      The finding of the tail is fairly efficient: File::Tail grabs a chunk from the end of file, and counts the newlines in the chunk. If it has enough to satisfy the request, it returns the data. If it doesn't have enough, it calculates the average length of line in what it already has, then multiplies the average with the number of lines it still needs, and tries again. This repeats until enough lines are in the buffer.

      The effect is that it should get the required number of lines with very few reads even if the line length distribution is strange.

      I see I will have to rewrite the POD for File::Tail though - this is the second case I heard of someone wanting just a few lines from the end, and not finding that described in the docs.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-18 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found