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


in reply to Cleaning up a text file with compact regex

open( my $fh, '<', 'file.txt' ) or die "Cannot read file.txt: $!\n"; # clearer idiom, anyone? scalar <$fh> for 1 .. 6; while (<$fh>) { next if /^;/; # ... process lines }

Replies are listed 'Best First'.
Re^2: Cleaning up a text file with compact regex
by betterworld (Curate) on Apr 24, 2007 at 09:49 UTC
    while (<$fh>)

    You should always put local($_); before such a loop, because the global variable $_ is set to each line and is not cleaned up in the end.

    Update: http://perl.plover.com/local.html has an example of the dangers of this kind of while loop.