crabbdean has asked for the wisdom of the Perl Monks concerning the following question:
For this I'll refer to a previous node 349582 and especially tye's response to that node 350948 in which he describes a "next" method iterator.
I've since used the guts of this "next" method (Big Thanks to tye) in a module I'm writing. I'm wanting to amend that method so that you can specify it to only go so deep into the directory tree. Here is the code:
I'm having trouble building into this how to track the depth to ensure it only goes as far as specified. Any suggestions?
Update: I should add that what makes this tricky is the code makes it go DOWN a directory tree first before starting on the next level, not across the directory tree as is usually the case.
Dean
The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
I've since used the guts of this "next" method (Big Thanks to tye) in a module I'm writing. I'm wanting to amend that method so that you can specify it to only go so deep into the directory tree. Here is the code:
As you can see in this code I've put in the checks for the $self->{level} in that it will only push to the $self->{dirs} stack if the current level is below the specified depth or depth == -1 (if depth is set to -1 it will fully recurse the directory tree)sub this_deep { my ($self, $depth) = @_; $depth = -1 unless @_ > 1; $self->{depth} = $depth; } sub next { my $self = shift; while( 1 ) { if ( @{ $self->{output} } ) { my $line = shift @{ $self->{output} }; return $line; } if ( ! @{ $self->{dirs} } ) { return; } my $dir = shift @{ $self->{dirs} }; if (-d $dir) { if( opendir( DIR, $dir ) ) { map { my $file = $_; my $fullfile = File::Spec->catfile( $dir, $file ); if (-d $fullfile && ($self->{level} <= $self->{dep +th} || $self->{depth} == -1)) { push (@{ $self->{dirs} }, $fullfile); } } File::Spec->no_upwards( readdir(DIR) ); closedir DIR; } else { warn "\a\a\aopendir FAILED, $dir: $!"; } } else { warn "\a\a\aNOT A DIRECTORY: $dir\n"; } } }
I'm having trouble building into this how to track the depth to ensure it only goes as far as specified. Any suggestions?
Update: I should add that what makes this tricky is the code makes it go DOWN a directory tree first before starting on the next level, not across the directory tree as is usually the case.
Dean
The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Depth Listing in Directory Traversal (one more stack)
by tye (Sage) on Jul 20, 2004 at 06:36 UTC | |
by crabbdean (Pilgrim) on Jul 20, 2004 at 13:16 UTC | |
Re: Depth Listing in Directory Traversal
by hbo (Monk) on Jul 20, 2004 at 04:36 UTC | |
by crabbdean (Pilgrim) on Jul 20, 2004 at 04:56 UTC | |
by hbo (Monk) on Jul 20, 2004 at 05:35 UTC | |
Re: Depth Listing in Directory Traversal
by adamk (Chaplain) on Jul 21, 2004 at 04:26 UTC |
Back to
Seekers of Perl Wisdom