Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2016-09-25 13:01 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (470 votes). Check out past polls.