in reply to How to conditionally terminate finddepth?

Perhaps others will see a "better" way, but having scanned the man page for File::Find, the only thing I could think of would be to issue a "die" in the "wanted" sub, and wrap the "finddepth()" call in an eval:
eval "finddepth(\\&wanted,\$base)"; if ( $@ ) { print "new file: $@\n"; } sub wanted { die $File::Find::name if ( -M _ <= 30 ); }
(updated to include the underscore in "wanted")

Replies are listed 'Best First'.
Re^2: How to conditionally terminate finddepth?
by runrig (Abbot) on Sep 12, 2005 at 22:32 UTC
    Good answer, but there's no reason to do a string eval. Get some efficiency and compile time checking (and save some backslashes) with a block eval:
    eval { finddepth(\&wanted, $base) };
Re^2: How to conditionally terminate finddepth?
by fizbin (Chaplain) on Sep 13, 2005 at 03:58 UTC
    Nice, but I'd be wary of missing some other important die in there, (say, a directory read error, or something equally dire) so would do:
    my $newfile = undef; eval { finddepth( sub {if (-M $_ <= 30) {$newfile=$File::Find::name; die "";}}, $base); }; die "$@" if ($@ and !defined($newfile)); if (defined($newfile)) {print "$base has something new in it\n";} else {print "$base is just old stuff\n";}
    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
      Or you could forgo eval/die and use goto
      my $newfile = undef; finddepth( sub { if (-M $_ <= 30){ $newfile=$File::Find::name; goto done_finddepth; } }, $base ); done_finddepth: die "the new file is $newfile";
      I once suggested an official breakout function to the perl5-porter, to be used like
      my $count = 0; &find( sub { $count++; File::Find::breakout() if $count == 500; }, '/');
      but nothing came of it. File::Find's way of doing things can mess with your head.

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re^2: How to conditionally terminate finddepth?
by sk (Curate) on Sep 12, 2005 at 22:27 UTC
    Thanks very much graff. That did it! Very good use of the eval-die combo!

    print "$base: old directory\n" if (not $@);
    :-)

    -SK