Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^5: Problem with -d inside find sub

by Anonymous Monk
on Apr 28, 2012 at 11:33 UTC( #967800=note: print w/replies, xml ) Need Help??


in reply to Re^4: Problem with -d inside find sub
in thread Problem with -d inside find sub

The problem is with local and lexical $_
Example:
use 5.010; sub my_sub (&) { my $code = shift; local $_ = '1234'; $code->(); } my_sub { say }; given ("test") { when ("test") { my_sub { say }; } }
Output:
1234 test
Why?
Because my $_ (lexical - from the given/when blocks ($_ = 'test')) has a higher priority (aka is in the block where you are) than the local $_ (local - from the my_sub ($_ = '1234')

How to fix this?
use 5.010; sub my_sub (&) { my $code = shift; local $_ = '1234'; $code->(); } my_sub { say }; sub my_sub2 { my_sub { say }; } given ("test") { when ("test") { my_sub2(); } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2016-08-30 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (411 votes). Check out past polls.