Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: rough approximation to pattern matching using local

by BrowserUk (Patriarch)
on Jan 25, 2015 at 12:58 UTC ( [id://1114431]=note: print w/replies, xml ) Need Help??


in reply to rough approximation to pattern matching using local

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1114431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found