in reply to coderefs, walktree, OO

I found a better solution now by making the walktree code clearer to myself (many thanks go to Frank, my local Perl guru :))
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;
This way I can achieve what I wanted - creating objects on the fly while walktree is doing its job.
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 );
A different question now is whether a different design would be easier to maintain.

I found Randal Schwartz Linux Mag column using tie() in TieFinder very appealing. He manages to get results in the moment of a successful match.

Perhaps I should post the final result into the "Craft" categorie. It is most helpful on Macs where you can't do searching with pattern matching on filenames otherwise.

Thanks to everybody following my chaotic ideas.

Replies are listed 'Best First'.
Re: Re: coderefs, walktree, OO
by axelrose (Scribe) on Jan 28, 2002 at 02:49 UTC
    I've made a complete example available in the Code section
    flexible find