Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: coderefs, walktree, OO

by axelrose (Scribe)
on Jan 22, 2002 at 04:21 UTC ( #140541=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140541]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-06-07 03:47 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.