|Perl: the Markov chain saw|
Re: What is the scope of $_?by Athanasius (Chancellor)
|on Nov 26, 2012 at 14:55 UTC||Need Help??|
$_ is global in scope (see perlvar#General-Variables). However, in
$_ is implicitly localised, as though written:
which explains the behaviour you’re seeing.
Update: From perlsyn#Foreach-Loops:
If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. This implicit localization occurs only in a foreach loop.
Hope that helps,
Athanasius <°(((>< contra mundum