in reply to Re: tail a file in perl
in thread tail a file in perl
Instead of using a circular array, this uses @lines as a FIFO buffer, storing the last $maxlines lines, using push and shift. Much less code! I'm not sure if this is the way it's implemented in the aforementioned perlfaq5 and other discussions...my @lines; my $maxlines = 10; while (<>) { push @lines, $_; shift @lines if (@lines > $maxlines); } print join("\n", @lines) . "\n";
Update: The solutions here using seek to read blocks starting at the end are going to be more efficient than this one (as the whole file is not read), although this solution is fine for reasonable-sized files.. Plus I think it's much simpler to comprehend.
blokhead
|
---|