On May 7, 2004, at 2:32 AM, Xavier Broccoli wrote:

The strange thing is that I'm not slurping up the
whole file

The code you posted *does* slurp the whole file.


foreach my $line (<FILE1>) {
   chomp $line;
   (do something...)
}

"foreach" is documented in perlsyn as "LABEL foreach VAR (LIST) BLOCK". That means that the contents of the parenthesis is evaluated in list context. In list context, <FILE1> returns an array of all the previously-unread lines in the file. In other words, what happens above is that the entire contents of the file referred to by FILE1 is slurped into a temporary array, and then each item in the array is assigned to $line in turn.


To read the file a line at a time, you need to use <FILE1> in scalar context, something like this:

while (my $line = <FILE1>) {
        chomp $line;
        (do stuff...)
}

Is there a way to force Perl to not slurp up the whole file at once

Yes - don't ask it to. ;-)


sherm--



Reply via email to