| Next | The Identity Function | 32 |
sub walk_tree {
my ($dir, $filefunc, $dirfunc) = @_;
if (-d $dir) {
$dirfunc->($dir);
opendir my $dh, $dir or return;
while (my $file = readdir $dh) {
next if $file eq '.' || $file eq '..';
walk_tree("$dir/$file", $filefunc, $dirfunc);
}
} else {
$filefunc->($dir);
}
}
Now we can print just the names of the directories:
walk_tree($DIR, sub {}, sub { print $_[0] });
Or accumulate a list of plain files:
my @FILES;
walk_tree($DIR, sub {push @FILES, $_[0]}, sub {});
| Next | ![]() |
Copyright © 2001 M. J. Dominus |