in reply to Re^2: Perl Out of memory error
in thread Perl Out of memory error

For some good reading, see buffered read. A simple while loop will read in 1 line at a time, each of which can be converted to ASCII and then tested.

As BrowserUK has pointed out, it's very possible this is not the cause of your error. But, on the assumption that maybe it is, we'll go on. The following statement is the problem:

read(FP2, $s, $file_size);

There is logic in the file to compute $buf_size but it is never used. Instead the entire file is loaded into the variable all at once. Then, you apply a regex against the entire file. This is very inefficient, even if it doesn't overwhelm memory.

Is the trailer record the last one in the file? Use File::ReadBackwards to get that trailer record. The rest is just math.

Dum Spiro Spero