my $first;
my $last;
{
open(my $fh, '<', 'file')
or die("Unable to open input file: $!\n");
while (<$fh>) {
$first = $_ if not defined $first;
$last = $_;
}
}
or
use File::ReadBackwards ();
my $first;
my $last;
{
open(my $fh, '<', 'file')
or die("Unable to open input file: $!\n");
$first = <$fh>;
}
{
my $fh = File::ReadBackwards->new('file')
or die("Unable to open input file: $!\n");
$last = $fh->readline();
}
or
my $first;
my $last;
{
open(my $fh, '<', 'file')
or die("Unable to open input file: $!\n");
($first, $last) = (<$fh>)[0, -1];
}