Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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...


Comment on RE: Using directory handles in recursive functions
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://17521]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2015-07-29 18:53 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (267 votes), past polls