in reply to Re: Best way to read line x from a file
in thread Best way to read line x from a file

A couple of small things went wrong in your 2nd example.

The correct version should be something like this:

use File::Slurp; my $f = read_file $filename; my $line2 = $1 if ($f =~ m!\A(?:.*\n){@{[$line_no-1]}}(.*)\n!m);
... but I wouldn't recommend it.

And for the sake of completeness, here the solution spelled out with Tie::File which lots of people mentioned already.

use Tie::File; tie my @file, 'Tie::File', $filename or die "Couldn't tie '$filename': $!"; my $line2 = $file[9];

-- Hofmator