Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Array Normalization of File::DirWalk

by jdporter (Paladin)
on Feb 23, 2007 at 12:26 UTC ( [id://601732]=note: print w/replies, xml ) Need Help??


in reply to Array Normalization of File::DirWalk

I'm sorry to say it, but File::DirWalk is not a very well written module. To do what you want to do properly, you have to reach into the object, breaking its encapsulation. (Hey, at least it's possible. :-) And the module's documentation is poor, in that it does not tell you what the depth parameter is really for or how it's used.

The following works:

for my $directory ( @directory ) { my $dw = new File::DirWalk; $dw->onDirLeave( sub{ my $dir = shift; if ( $dw->{'depth_count'} == $depth ) { push @folder, $dir; } return File::DirWalk::SUCCESS; } ); $dw->setDepth( $depth + 1 ); $dw->walk( $directory ); }

Note two things:

  1. The test of the object's data member 'depth_count' against $depth;
  2. passing $depth + 1 to the setDepth method.

Just for fun, here's how I'd probably write the above:

my $dw = new File::DirWalk; sub on_dir_leave { $dw->{'depth_count'} == $depth and push @folder, shift; File::DirWalk::SUCCESS } $dw->onDirLeave( \&on_dir_leave ); $dw->setDepth( $depth + 1 ); $dw->walk( $_ ) for @directory;
A word spoken in Mind will reach its own level, in the objective world, by its own weight

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2024-04-18 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found