Expanding on GrandFather's answer, the problem here is that wanted is a globally accessible name, which is defined in the lexical scope of a single call to findfile. There will therefore be only ever be one wanted function that will only see one lexically scoped $target. So the second, third, and later times you call findfile, wanted will not see the private values of $target set on those calls.

If this confuses you, see the code example at Re (tilly) 9: Why are closures cool? to see the issue at work.

On an unrelated note, once you resolve that bug you will have an even more mysterious one that Perl is not giving you a warning about. The problem is in the line

(my $foundfile = $File::Find::name) if $File::Find::name =~ $target;
It looks innocuous, but the problem is that my has both a compile time and a run time effect. What run time effect, you say? Well as an optimization, Perl does not actually clear lexical variables when you leave a function. Instead if possible it will reinitialize them when it encounters them again. (This is generally faster.) The problem is that in the pattern:
my $foo = $bar if condition();
if condition() is false, then my $foo is not executed, and Perl does not reinitialize your data.

The result? If your condition ever matches, you will continue to return that file name until the next time the condition matches at which point you will return the new file name until another one matches, etc.

The fix is to never, ever, ever put a my where it can be hidden by a conditional. In this case you would avoid it with:

my $foundfile; $foundfile = $File::Find::name) if $File::Find::name =~ $target;
See Unusual Closure Behaviour for further discussion.

In reply to Re: Adding a dispatch table and getting "Variable $x will not stay shared" errors. by tilly
in thread Adding a dispatch table and getting "Variable $x will not stay shared" errors. by gctaylor1

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.