opening a file in perl

Anthony Wesley Anthony.Wesley at
Wed May 1 10:19:22 EST 2002

Nemo - earth native wrote:
> Hi guys, a quick question for the perl guru's around...
> I've a small script which opens a file, reads through it looking for
> various information, uses that to calculate some figures, and closes.
> Pretty simple stuff.
> ...except when the file being opened is the better part of 400meg,
> and so I get Out of Memory errors - the perl seems to wants to read the
> whole file into memory before looking at it at all.
> At the moment is opens the file as such:
>         open( IN, "chunk.txt" ) or die "Hrm. Couldn't open the file./n";
> And then just runs through line by line:
>         foreach my $line ( <IN> )
>         {
> (insert a few possible regexp matches in `if` statements here. simple stuff)
>         }
> So, how do you get perl to not load the whole file into memory?
> .../Nemo

Just to be clear - executing a filehandle in an array context triggers special behaviour. The file is read into memory and then
split into an array of lines (or whatever the record separator variable is set to) before the rest of that statement is allowed
to run.

This happens when doing something innocent like 

		foreach $line (<IN>)

cheers, Anthony

More information about the linux mailing list