in reply to Turning a recursive function into an iterator
What do you think about it? is there anything fundamentally flawed with this approach?{ my @fq; # files my @dq; # directories my $currentDir; sub initDir { push @dq, shift; }; sub getNextFile { my $f; if ( $#fq >= 0 ) { $f = pop @fq; } else { if ( $#dq >= 0 ) { my $r; # filename read local *D; $currentDir = pop @dq; opendir(D, $currentDir) || die "$currentDir: $!"; while ( defined( $r = readdir( D ) ) ) { next if $r =~ /^\.{1,3}$/; my $ff = $currentDir . '/' . $r; if ( -d $ff ) { push @dq, $ff; } else { push @fq, $ff; } } closedir D; $f = getNextFile(); } else { $f = undef; } } return $f; } } my $dir = "c:/inetpub"; # execution start here initDir( $dir ); my $f; my $i = 0 ; while ( defined( $f = getNextFile() ) ) { $i++; print "[$i] $f \n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Thank you
by sfink (Deacon) on Jan 19, 2004 at 21:33 UTC | |
|
•Re: Thank you
by merlyn (Sage) on Jan 19, 2004 at 16:13 UTC | |
by l3nz (Friar) on Jan 19, 2004 at 16:17 UTC | |
|
Re: Thank you
by diotalevi (Canon) on Jan 19, 2004 at 19:31 UTC |