Eating memory

Dirk Koopman djk at
Tue May 23 13:12:44 BST 2006

On Tue, 2006-05-23 at 12:53 +0100, Simon Wilcox wrote:
> Sorry, I have a perl question :-)
> I've been working on a simple script to munge some logs and produce some
> stats but top suggests that it's loading the whole file into memory when I
> think it should be working line by line. This is obviously not good when
> there are gigs of logs to look through.
> I've reduced the script to the simplest case but it is still not reading
> the file line by line. I must be doing something stupid - can anyone see
> what it is ?
> #! /usr/bin/perl
> use warnings;
> use strict;
> my $filename = $ARGV[0];
> open INFILE, "$filename" or die "Can't open file : $!\n";
> foreach (<INFILE>) {
> }

You probably mean "while (<INFILE>) { }"

I imagine what is happening is that foreach evaluates <INFILE> in list
context then processes it. That means it reads the whole lot in first. 

While does not do this as it is working in scalar context on the result
of obtaining the next record from <INFILE> and stuffing it into $_.


More information about the mailing list