If you are playing chdir games, I would first suggest trying File::Find. Understanding that such an answer is not always the best, and there is frequently knowledge to be gleaned from reinventing wheels, I would focus your attention to the local() keyword.

I believe if you try this:

sub foo { my $dir = shift; chdir $dir; local *CURRENT_DIR; # Everything else follows }
you will have better success. Basically, the local() protects the calling function's version of CURRENT_DIR from any changes made locally.

In a broader issue, let me give you a few hints. If your directory structure is very deep, you will run out of file handles soon. Those of us with (way too much) experience writing this kind of code usually suck the contents into an array and then close the directory handle.

opendir FOO, "$dir" or die "Couldn't open $dir: $!"; @contents = readdir FOO; closedir FOO;
This will also obviate your problem. A common variant on this is to read the contents like
@contents = grep !/^\.\.?$/, readdir FOO;
but I do not much care for this. If you are parsing the contents of the directory later, you have just caused perl to walk the array twice when a simple next() could have done the job.

If the directory is too big to suck in at once and you are still running out of file handles, you can do it this way

sub recurse_me { my $dir = shift; opendir FOO, "$dir" or die "Couldn't open $dir: $!; while( readdir FOO ) { if ( -d $_ ) { push @dirs, $_; } else { # do something interesting } } closedir FOO; recurse_me( $_ ) for ( sort @dirs ); }
Probably more than you asked, but I thought I may help you avoid some later pain.

Hth,
mikfire


In reply to RE: Using directory handles in recursive functions by mikfire
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.