opening a file in perl

Anthony David adavid at
Wed May 1 10:59:03 EST 2002

Nemo - earth native <nemo at> writes:

> 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?

Read the file one line at a time instead of sucking it into a list.

while (<IN>) {


Anthony David

Gambling(n): A discretionary tax on those asleep during high school maths
0xA72CE1ED fingerprint = EA1E C69E FE59 BBE1 AA4B  F354 BD09 9765 A72C E1ED

More information about the linux mailing list