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.


In reply to RE: Using directory handles in recursive functions by ahunter
in thread Using directory handles in recursive functions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.