For the simple things, like packing a number into 4 bytes, there is pack, which does that, and many more things. The usual approach would be to pack all the offsets of the lines (or a better key) into one long string, as four bytes per line:
my $offsets = '';
while (<$fh>) {
my $offset = tell $fh;
$offsets .= pack 'N', $offset;
};
Whether to store the whole file or just the offset of every 10 lines is a matter of (low-level) optimization. |