my $first; my $last; { open(my $fh, '<', 'file') or die("Unable to open input file: $!\n"); while (<$fh>) { $first = $_ if not defined $first; $last = $_; } } #### 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(); } #### my $first; my $last; { open(my $fh, '<', 'file') or die("Unable to open input file: $!\n"); ($first, $last) = (<$fh>)[0, -1]; }