Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Recursive Directory print

by sn1987a (Deacon)
on Mar 06, 2014 at 14:17 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1077234]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found