crabbdean has asked for the wisdom of the Perl Monks concerning the following question:
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"; } } }
|
---|
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 |