Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: recursive sub

by mkmcconn (Chaplain)
on Oct 30, 2001 at 11:58 UTC ( #122045=note: print w/replies, xml ) Need Help??


in reply to recursive sub

Anonymous,
The following function seems to fit your description
# recursively operate on files use DirHandle; sub go_in { my $dir = shift; # -- this just flips the toothpicks, in case # we're in windows. It's not really necessary. $dir =~ tr/\\/\//; # -- and this just adds a trailing slash, for # concatenating later. if ($dir !~ m/\/$/){ $dir .= '/'; } my $dh = new DirHandle ($dir); # or die, I suppose. return unless $dh; while ( defined( my $item = $dh->read )) { next if $item =~ m/\.$/; $item = $dir.$item; # paranoid about symlinks. next if -l $item; # -- call recursively, but not forever: # (discard visited directories) if ( -d $item ){ go_in($item); next; } # -- operate on files here -- # } }

DirHandle is a very simple module, suitable for simple tasks.
updated: edited out some playful peculiarities.
mkmcconn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2023-03-22 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (60 votes). Check out past polls.

    Notices?