in reply to Re: tail -1 emulation efficiency
in thread tail -1 emulation efficiency

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.

Replies are listed 'Best First'.
Re: Re: Re: tail -1 emulation efficiency
by matija (Priest) on May 06, 2004 at 04:43 UTC
    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.