in reply to Getting files in a directory tree

Just to provide an alternative,
sub recurse { my $d = shift; opendir D,$d; # who needs error checking anyways while(<D>) { if( -d $_ ){ recurse($_); else{ #regular file } } }

Replies are listed 'Best First'.
Re: Re: Getting files in a directory tree
by Roger (Parson) on Mar 04, 2004 at 05:24 UTC
    Beware of the side effects of the Perl built-in variables, your solution is not likely going to work as you expected, the $_ will get clobberred in your recursion and you will sit there scratching your head for hours trying to find out what's going wrong. :-)

Re: Re: Getting files in a directory tree
by esskar (Deacon) on Mar 04, 2004 at 05:38 UTC
    1. do not forget to close the directory using closedir
    2. using recursion and D as the directory handle can be danagerous; better
    sub recurse { my $folder = shift; my $dir; if(opendir($dir, $folder)) # who needs error checking anyways? me! { while(defined(my $entry = readdir($dir))) { next if $entry =~ /^\.\.?$/; # avoiding endless loop :) if( -d "$folder/$entry" ){ recurse("$folder/$entry"); } else { #regular file } } closedir($dir); } }