http://www.perlmonks.org?node_id=375796


in reply to Depth Listing in Directory Traversal

sub next { my $self = shift; while( 1 ) { if ( @{ $self->{output} } ) { my $line = shift @{ $self->{output} }; return $line; }
Since we see no other references to this array ref, I'm assuming this code is cut down from its full version.
if ( ! @{ $self->{dirs} } ) { return; } my $dir = shift @{ $self->{dirs} }; if (-d $dir) {
Add a proper declaration for $olddir above, and:
if ($oldir){ $self->{level} += ($dir=~tr!/!/!) - ($olddir=~tr!/!/!); } $olddir=$dir;
I haven't tested this, but the idea is that, since the paths are all absolute, a change in level means a difference in the number of path seperators. (You might want to use a more portable form of the seperator than a literal "/".)