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....


In reply to Re: find loses $found in find by demerphq
in thread find loses $found in find by Dave05

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.