Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

RE: Using directory handles in recursive functions

by ahunter (Monk)
on Jun 10, 2000 at 22:40 UTC ( #17521=note: print w/replies, xml ) Need Help??

in reply to Using directory handles in recursive functions

In the cases where I've wanted to make file handles local (without using File::Handle :-), I've done the following:
{ local(*FH); open(FH, "<whatever") }
I think my works with file globs as well, but I'm not sure, as this is one instance where local() seems more intuitive (!) to me.

This is also useful for opening an arbitary number of files. Take a reference to the glob before exiting the local block:

{ local(*FH); open(FH, "<whatever") $fh = \*FH; } print $fh "Foo\n";
As an added bonus, the file will be closed automagically when $fh goes out of scope :-)

I would guess that opendir() and so on will work fine with this. I haven't tried it, of course, but there's no reason it shouldn't. Note that the asterix in local(*FOO) is important. local(FOO) has a different meaning...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://17521]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2018-04-24 00:07 GMT
Find Nodes?
    Voting Booth?