Using lexical filehandles seems to work for me

Yes, for relatively "flat" directory structures. There is a limit for the number of open file/directory handles (OS specific), and if the directory structure is deep enough, you will run out of file handles. That will happen even earlier if you have a lot of open files.

There is no need to keep the file/directory handle open while recursing directory structures, all you need is a single handle for arbitary nested directories:

#!/usr/bin/perl use strict; use warnings; sub scan { my $dirname=shift; opendir my $d,$dirname or die "Can't open $dirname: $!"; my @files=readdir $d; closedir $d; for my $n (@files) { next if $n=~/^\.{1,2}$/; if (-d "$dirname/$n") { print "DIR: $dirname/$n\n"; scan("$dirname/$n"); } else { print "NOTDIR: $dirname/$n\n"; } } } scan(".");

There even is no need to use recursion at all:

#!/usr/bin/perl use strict; use warnings; sub scan { my $topdir=shift; my @todo=($topdir); while (@todo) { my $dirname=shift @todo; opendir my $d,$dirname or die "Can't open $dirname: $! +"; while (my $n=readdir $d) { next if $n=~/^\.{1,2}$/; if (-d "$dirname/$n") { print "DIR: $dirname/$n\n"; push @todo,"$dirname/$n"; } else { print "NOTDIR: $dirname/$n\n"; } } closedir $d; } } scan(".");

This way, you don't need a (possibly big) array of directory entry names for each recursion level, but only a list of directory names not yet scanned. And because this is not recursive, you won't get the "Deep recursion on subroutine" warning with deeply nested directories (see perldiag).

Try changing shift @todo to pop @todo for a different scan order.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^2: Descending a directory tree, returning a list of files by afoken
in thread Descending a directory tree, returning a list of files by Rodster001

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.