Actually, I've been thinking about this and I think my preferred interface might be an object that lets you iterate in several directions so you can pick depth-first or bredth-first and decide to prune your search in what I consider more natural ways:

# The simple case like File::Find / File::Iterator my $iter= File::Whatever->new( $root ); while( $iter->NextItem() ) { doSomething( $iter->GetFileName() ); } # Recurse yourself: my $iter= File::Whatever->new( $root ); MyFunc( $iter ); sub MyFunc { my $iter= shift(@_); # Uncomment next line for depth-first search: MyFunc( $iter ) while $iter->PushPath(); while( my $file= $iter->NextFile() ) { doSomething( $file ); } # Uncomment next line for bredth-first search: #MyFunc( $iter ) while $iter->PushPath(); $iter->PopPath(); } # Example of pruning your search: my $iter= File::Whatever->new( $root ); MyFunc( $iter ); sub MyFunc { my $iter= shift(@_); while( my $file= $iter->NextFile() ) { doSomething( $file ); } while( my $sub= $iter->PushPath() ) { if( $sub =~ /debug/i ) { $iter->PopPath(); } else { MyFunc( $iter ); } } $iter->PopPath(); } # Bredth-first search w/o recursion: my $iter= File::Whatever->new( $root ); do { while( my $file= $iter->NextFile() ) { doSomething( $file ); } } while( $iter->NextPath() ); # Bredth-first search w/ pruning w/o recursion: my $iter= File::Whatever->new( $root ); do { while( my $file= $iter->NextFile() ) { doSomething( $file ); } while( $iter->NextPath() =~ /debug/i ) { $iter->PopPath(); } } while( ! $iter->Finished() ); # Depth-first search w/o recursion: my $iter= File::Whatever->new( $root ); do { 0 while $iter->PushPath(); while( my $file= $iter->NextFile() ) { doSomething( $file ); } } while( $iter->PopPath() ); # Depth-first search w/ pruning w/o recursion: my $iter= File::Whatever->new( $root ); do { while( my $sub= $iter->PushPath() ) { $iter->PopPath() if $sub =~ /debug/i; } while( my $file= $iter->NextFile() ) { doSomething( $file ); } } while( $iter->PopPath() ); # The opposite of pruning: # Only do things to files in directories named "debug" my $iter= File::Whatever->new( $root ); do { if( 'debug' eq $iter->GetDirName() ) { while( my $file= $iter->NextFile() ) { doSomething( $file ); } } } while( $iter->NextPath() );
But I need to think about that a lot more before I'm sure that such makes sense or if I can make it better. (:

                - tye

In reply to Re^3: File::Find problem (yucky callbacks) by tye
in thread File::Find problem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.