This way I can achieve what I wanted - creating objects on the fly while walktree is doing its job.package WalkTree; use strict; my $DIRSEP = $^O =~ /Mac/ ? ':' : $^O =~ /Win|OS-2|DOS/ ? '\\' : '/'; my $MACOS = ( $^O =~ /Mac/ ) || 0; my $WINOS = ( $^O =~ /Win|OS-2|DOS/ ) || 0; sub walktree { my ( $dir, $filefunc, $dirfunc, $prune ) = @_; my @values; $MACOS and $dir =~ s/:$//; if ( -d $dir ) { if( $prune and $dir =~ /$prune/o ) { return undef } ref $dirfunc and $dirfunc->( $dir ); local *DH; opendir DH, $dir or warn "opendir '$dir' failed\n$!"; my $entry; while ( defined( $entry = readdir DH )) { !$MACOS and next if( $entry eq '.' or $entry eq '..' ); $MACOS and next if( $entry eq "Icon\n" ); my $fullpath = "$dir$DIRSEP$entry"; if( -d $fullpath ) { walktree( $fullpath, $filefunc, $dirfunc, $prune ); } elsif( -f $fullpath ) { ref $filefunc and $filefunc->( $fullpath ); } push @values, $fullpath; } closedir DH; } else { warn "$PACKAGE::walktree() - need a directory argument\n"; } return @values; } 1;
A different question now is whether a different design would be easier to maintain.my $filefunc = sub { if( $filter ) { $_[0] =~ /$filter/o and push @files, File->new( name => $_[0] +) } else { push @files, File->new( name => $_[0] ) } }; WalkTree::walktree( $dir, $filefunc, sub { push @dirs , File->new( name => $_[0] ) }, $prune );
In reply to Re: coderefs, walktree, OO
by axelrose
in thread coderefs, walktree, OO
by axelrose
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |