in reply to Recursive sub efficiency

my @files = grep(/\w+/,readdir(DIR)); close (DIR); foreach my $subdir (@files){ if(-d $dir.'/'.$subdir){
It looks like you only operate on sub-directories, and you just discard files. I'm not sure if it is more efficient (speed and/or memory) without Benchmarking, but the code would be more straightforward if you filtered out what you didn't need in the grep:
my @dirs = grep { /\w/ and -d "$dir/$_" } readdir(DIR); closedir (DIR); foreach my $subdir (@dirs){
Update: Fixed -d and closedir

Replies are listed 'Best First'.
Re^2: Recursive sub efficiency
by skywalker (Beadle) on Jun 10, 2010 at 19:56 UTC
    Yep your right Im only interested in the folders nothing else.

    However the inline grep and directory check does not return any anything. Ive updated the directory handle to a scaler as per Ikegami's advice.

    my @files = grep(/\w/,readdir($fh)); #returns folder & files my @dirs = grep {/\w/ and -d} readdir($fh); #returns blank
      He meant
      my @dirs = grep { /\w/ && -d "$dir/$_" } readdir($dh);
      which should probably be
      my @dirs = grep { !/^\.\.?\z/ && -d "$dir/$_" } readdir($dh);
      My mistake. I updated my code.

      Comment out your @files line, then @dirs should contain something. You can not call readdir two times like that.