It is clear that you are expecting match() to construct a variable called $bob, give it the value 45; in such a way that when the anonymous subroutine that is the third argument is run, that variable will exist and be visible to that subroutine.

What isn't clear is quite how what you are doing would achieve that goal.

A simple diagnostic run just before you invoke the anonymous sub:

use Data::Dump qw[ pp ]; ... sub match ($$$) { no strict 'refs'; my ($lhs, $rhs, $fun) = @_; my $locals = {}; # populate locals match_inner($lhs, $rhs, $locals); while (my ($k, $v) = each %$locals) { local ${$k} = $v; } pp $locals; return $fun->(); }

Shows that what you've done is add a key:bob with the value 45 to the hash(ref) name $locals:

C:\test>1114414.pl Name "main::bob" used only once: possible typo at C:\test\1114414.pl l +ine 52. { bob => 45 } Use of uninitialized value $bob in print at C:\test\1114414.pl line 52 +.

What that also shows is that the first warning:Name "main::bob" used only once: is produced at compile time; ie. long before your code ever gets invoked; so nothing your code does at runtime will resolve the problem you are being warned about.

I think that's because the while loop localizes each variable only within the body of the loop.

Indeed. Everything you did with local is undone as soon as you leave the body of the loop.

In order to achieve your goal -- the declaration and initialisation of variables either within the scope of, or closed over by, your anonymous subroutine -- you would have to be modifying the symbol table prior to that anonymous subroutines compilation! Ie. You'd have to do something before your code was run.

And that a tall order unless your code is running within the compiler itself.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Re: rough approximation to pattern matching using local by BrowserUk
in thread rough approximation to pattern matching using local by gregory-nisbet

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.