what about this:
while( <> ) {
push @lines, $_;
shift @lines if $#lines > 100;
}
I agree CPAN is great.
But you have to put hings in balance.
When the time + effort to search for what you need,
evaluate the 20 possible modules, select one or two, compare,
understand how they work, I would rather write my own two lines.
I would not do this for complex stuff, like parsing XML/HTML,
sending emails, DB interogations, etc.
Where is the limit for "complex" for each one, it is for each
one to decide. If I am not able to write a line of perl to
do my stuff, chances are I will not be able to use properly a
CPAN module.
CPAN is not going to think for you.
If you *must* "roll your own", you might try this. However, read the
notes at the end of the code before using.
--untested--
#!/usr/bin/perl -w
#use strict pragma
use strict;
#get the filename of the file to process -
#die if no filename is provided
my $in_file = shift
or die "No filename provided\n";
#open the file to process -
#die if it can't be opened
open(IN, $in_file)
or die "Can't open $in_file: $!\n";
#read the file - in reverse - into an array
my @reverse = reverse <IN>;
#close the file
close IN;
#declare a counter
my $count = 0;
#declare an array to hold the lines we want
my @lines;
#while there is still an array
#containing the original file (in reverse) ...
while(@reverse){
#increment the counter
$count++;
#push onto the array for the lines we want the
#value of the current line - shift it from the
#original file (in reverse) array
push @lines, shift @reverse;
#break the loop if we reach the amount of lines
#from the end of the file (ie last 100 lines
#of the original file)
last if $count == 100;
}
#print the filnal results (ie last 100 lines of the file)
print reverse @lines;
--untested--
I tested (yes, I *always* put untested for the code, because IMHO it's
never tested enough) this with two files. One with 5 lines and my
messages file (which has well over 100 lines). It worked for both.
However, this method has some issues. First, if the file being read
exceeds the system memory, the script will most likely crap. It also
uses open versus sysopen.. And, there's no file locking. And, you
will most likely miss log messages if you use this againist an active
log file. There are propbably other issues with this method, but if
you want to use it instead of using a tested method, be my guest
This was "quick and dirty" - so the quality is most likely below
standards.
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL.
http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
"We have reason to believe that man first walked upright to free
his hands for masturbation." -- Lily Tomlin