in reply to What is the scope of $_?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What is the scope of $_?
by kennethk (Abbot) on Nov 26, 2012 at 18:40 UTC | |
by tobyink (Canon) on Nov 26, 2012 at 19:35 UTC | |
|
Re^2: What is the scope of $_?
by Lotus1 (Vicar) on Nov 26, 2012 at 19:46 UTC | |
by tobyink (Canon) on Nov 26, 2012 at 23:16 UTC | |
by Lotus1 (Vicar) on Nov 27, 2012 at 13:49 UTC |