In addition to what was said above, there also is the temporary assignment possible with local, which will also close your file handles as soon as they go out of scope:
sub scan_file { my ($filename) = @_; local *FILE; open FILE, "< $filename" or die "couldn't read '$filename': $!"; return grep { ! /^#/ } FILE; # Perl closes FILE here };
But if you're already using Perl 5.8.x and don't need your script to run under 5.005, use the three argument open and the lexical filehandle as proposed by geekgrrl above.
In reply to Re: filehandle cleanup
by Corion
in thread filehandle cleanup
by dbaad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |