Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

Andrew.


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-01 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (14 votes), past polls