Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Array Normalization of File::DirWalk

by jdporter (Canon)
on Feb 23, 2007 at 12:26 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2021-08-02 08:58 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (22 votes). Check out past polls.