http://www.perlmonks.org?node_id=1013769


in reply to Going through a big file [solved]

> First I tried the obvious

open in, shift @ARGV; for(<in>){

Obvious?

You will hardly find any example in the perldocs ever trying this.

Rather

open in, shift @ARGV; while (<in>){

or better

open my $in, '<', shift @ARGV; while (my $line = <$in>){

Explanation

your code is semantically equivalent to

my @temp_list=<in>; for(@temp_list){
slurping the whole file as a first step.

for expects a list and evaluates in list context¹, so it's greedily swallowing all at once.

But while iterates in scalar context, that is line by line (as defined by '$/')

Cheers Rolf

¹) with a little magical exception in recent Perl versions (>= 5.8 ?) regarding ranges, which isn't relevant here