Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Recursive Directory print

by sn1987a (Monk)
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"; } } }


Comment on Re: Recursive Directory print
Select or Download Code
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 rifling through the Monastery: (9)
As of 2014-08-22 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls