tcf03 has asked for the wisdom of the Perl Monks concerning the following question:

I have two subroutines
sub get_first { # usage: get_first("firstfile"); my $f1=shift; # first file tie my @file1, 'Tie::File', "$workdir$f1" or die("Can't tie $f1: $ +!\n"); my $first=@file1[0]; untie @file1; my ($dow, $month, $day, $time, $year, $line)=split(/\s+/, $first); my $start_time=join ' ', $month,$day,$year,$time; return $start_time; } sub get_last { # usage: get_last("lastfile"); my $f2=shift; # last File tie my @file2, 'Tie::File', "$workdir$f2" or die("Can't tie $f2: $ +!\n"); my $last=@file2[$#file2]; untie @file2; my ($dow, $month, $day, $time, $year, $line)=split(/\s+/, $last); my $end_time=join ' ', $month,$day,$year,$time; return $end_time; }
and they are called like this:
$rfmain_beg=&get_first($rfmain[0]); $rfmain_end=&get_last($rfmain[$#rfmain]);
where @rfmain holds a list of file names. The problem is that this takes a very long time, the files are about 120 - 220 meg each. I just need the very first line of the first file and the very last line of the last file. These are log files and each line begins with a date. Is there a better way of doing this? Ive looked around, but Tie::File seems to be mentioned quite a bit.

Thanks in advance
Ted

UPDATE
Thanks using File::ReadBackwards worked nicely.

Replies are listed 'Best First'.
Re: Speeding up Tie::File
by dragonchild (Archbishop) on Apr 11, 2005 at 17:54 UTC
    There is no reason for Tie::File. That's for when you want to make modifications in the middle of a file. You have the exact opposite need.
    sub get_first_line { my ($filename) = @_; open my $fh, '<', $filename or die "Cannot open '$filename' for reading: $!\n"; return scalar <$fh>; } use File::ReadBackwards; sub get_last_line { my ($filename) = @_; my $fh = File::ReadBackwards->new( $filename ) or die "Cannot open '$filename' for reading backwards: $!\n"; return $fh->readline; }