in reply to Perl Out of memory error

Not so much "COBOL" files as EBCDIC.

It reads the entire file in at once, which is probably why you are running out of memory.

Dum Spiro Spero

Replies are listed 'Best First'.
Re^2: Perl Out of memory error
by Anonymous Monk on Oct 05, 2015 at 21:51 UTC
    Can you please show how to rewrite the script so it does not read the entire file into memory. I am very new to Perl and I am still learning the basics.

      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