There's more than one way to do things | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
There is more than one $_. All your examples use the global $_. This is global in scope (as are all the "punctuation variables", except @_ which is just weird). However, certain constructs implicitly localize variables. for loops do this. Also (from Perl 5.10) you can create a new variable called $_ using my $_ or given (though the behaviour of given will change in 5.18 - it will not implicitly create a lexical $_). These have the same scope as any other lexical variables - the remainder of the block they're defined in, where the file itself counts as a block. If you want to access your caller's lexical $_, then the official way is to use a sub prototype. I've also written lexical::underscore which allows you to delve multiple levels down the call stack.
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
In reply to Re: What is the scope of $_?
by tobyink
|
|