# the usual precautions use strict; use warnings; # automatically die when open() or so fails use autodie; my $filename = shift @ARGV; open my $handle, '<', $filename ; # determine the block length my $first_block = ''; $first_block .= <$handle> for 1..4; my $block_length = length $first_block; sub get_line { my ($handle, $block_length, $line) = @_; # assume the file starts with line 1: seek $handle, $block_length * ($line - 1), 0; my $result; my $read_len = read $handle, $result, $block_length; if ($read_len == $block_length) { return $result } else { die "Error while reading file: expected $block_length bytes, but got $read_len instead"; } }