8.8.3. Discussion
Each strategy has different features, useful in different
circumstances. The linear access approach is easy to write and best
for short files. The Tie::File module gives good performance,
regardless of the size of the file or which line you're reading (and
is pure Perl, so doesn't require any external libraries). The DB_File
mechanism has some initial overhead, but later accesses are faster
than with linear access, so use it for long files that are accessed
more than once and are accessed out of order.
It is important to know whether you're counting lines from 0 or 1.
The $. variable is 1 after the first line is read,
so count from 1 when using linear access. The index mechanism uses
many offsets, so count from 0. Tie::File and DB_File treat the file's
records as an array indexed from 0, so count lines from 0.
Here are three different implementations of the same program,
print_line. The program takes two arguments: a
filename and a line number to extract.