in reply to Adding a dispatch table and getting "Variable $x will not stay shared" errors.
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
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 $foundfile = $File::Find::name) if $File::Find::name =~ $target;
if condition() is false, then my $foo is not executed, and Perl does not reinitialize your data.my $foo = $bar if condition();
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:
See Unusual Closure Behaviour for further discussion.my $foundfile; $foundfile = $File::Find::name) if $File::Find::name =~ $target;
|
|---|