http://www.perlmonks.org?node_id=255837

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

Is there a more efficient way of grabbing the first and last line of a file.

I'm currently doing the following

#!/usr/bin/perl use warnings; use strict; open (FOO, "test") || die "ERROR Unable to open test: $!\n"; my @array = <FOO>; close FOO; my $first = shift (@array); my $last = pop (@array);
Reading the entire contents of the file into memory is time consuming and inefficient.

Any suggestions?