Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2016-06-30 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (405 votes). Check out past polls.