in reply to find loses $found in find

File::Find maintains its state in package level lexicals. So when you call File::Find from within the wanted() function it resets the state and thus looses track of where it was. I believe that this is implicitly a bug. I contemplated hacking File::Find into a OO version that maintains its state within an object thus allowing you to do what you wanted to do, but I dont have the time to deal with code that crotchety right now (it has gone on my list of things to do though.)

The way to deal with this is to use something like a breadth-first-traversal pattern. Ie, when you find a file that will cause the files in some other directory to change then push that file onto a list. Find all the files that need such an action _first_ then do the updating afterwards. Something like so: (this says that if we find a file in the first find then we will delete all similarly named files in a different dir)

# untested example code that could cause all of your .pl files to be + deleted! my @fix; find {no_chdir=>1, wanted=>sub{ -f && m/\.pl$/ && push @fix,[File::Spec->splitpat +h($_)];} },@paths; foreach my $fix (@fix) { find { no_chdir=>1, wanted=>sub{ -f && (File::Spec->splitpath($_))[2] eq $fix->[2] && +unlink $_ } },do{ (my $x=$fix->[0].$fix->[1]) =~s/foo/bar/g; $x}; }
This is fairly idomatic perl code (sorry) but hopefully you get the idea.

--- demerphq
my friends call me, usually because I'm late....

Replies are listed 'Best First'.
Re: Re: find loses $found in find
by Dave05 (Beadle) on Sep 13, 2002 at 13:31 UTC

    Well, this is enlightenment - of the can of worms variety! Looking at my code, I 'find' <sigh> that just about my whole script is actually already inside a 'find'.

    So I'll re-work the whole thing using the breadth-first approach, but it's a real shame when such a useful thing as File::Find turns out to have such limitations.

    Can anyone suggest an alternative searching module to File::Find before I start chopping up my script?

    Dave

      Have you checked out the fabtastic File::Find::Rule? From the looks of your conditions it should be a perfect fit!
      HTH

      _________
      broquaint

        Good idea. And it looks like it maintains its state internally so the recursion shouldnt be a problem...

        --- demerphq
        my friends call me, usually because I'm late....

      Unfortunately I cant suggest anything short of rewriting File::Find to be object oriented.

      Sorry.

      --- demerphq
      my friends call me, usually because I'm late....