Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: File handles - there must be a better way

by pokki (Monk)
on May 13, 2013 at 18:51 UTC ( #1033332=note: print w/replies, xml ) Need Help??

in reply to File handles - there must be a better way

Sounds like you're stuck on old-style global file handles (open FOO, ...). This forces you to maintain an array of file handle names to avoid collisions, since you can only have one file handle of a given name. If you're already using lexical file handles, disregard the rest of this post!

Use lexical file handles instead (in general, this is preferred; there is zero advantage to using the other style, and even if the only thing you get from lexical file handles is lexical scope, that's a net win):

my %filehandle_of; foreach my $filename (@filenames) { # here $filehandle is a reference to a new anonymous filehandle in + each iteration, you can push it into an array if you don't need the +mapping instead if (open my $filehandle, '<', $filename) { $filehandle_of{$filename} = $filehandle; } else { warn "could not open '$filename' for reading: $!\n"; next; } }

See also perldoc -f open.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033332]
and a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2018-04-24 12:53 GMT
Find Nodes?
    Voting Booth?