http://www.perlmonks.org?node_id=1077234


in reply to Recursive Directory print

In addition to the issue corrected by Don Coyote above, there are two addition problems:

First, is the return print_rec($_). The return means you will stop processing as soon as you finish processing the first directory you encounter.

Second,the handle DIR is global and the later opendirs will overwrite the early ones. You are better of using a lexical handle like my $dir.

With those corrections the sub looks like:
sub print_rec { my $path = shift; opendir my $dir, $path; while( readdir($dir) ) { if(!/\.|\.\./ && -d $_) { print "$_ is a directory\n"; print_rec($path.'\'.$_); } elsif (!-d) { print "$_ \n"; } } }