Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Recursive Directory print

by sn1987a (Hermit)
on Mar 06, 2014 at 14:17 UTC ( #1077234=note: print w/replies, xml ) Need Help??

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"; } } }

Replies are listed 'Best First'.
Re^2: Recursive Directory print
by Anonymous Monk on Mar 06, 2014 at 16:52 UTC

    The return statement is undoubtedly the crux of the OP’s problem:   as soon as the innermost occurrence returns, all of them do.   This program will probably work as intended if the word return is taken out.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1077234]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2016-10-25 16:18 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (323 votes). Check out past polls.