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. | [reply] |
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.
| [reply] [d/l] |