<?xml version="1.0" encoding="windows-1252"?>
<node id="1005774" title="Re^3: What is the scope of $_?" created="2012-11-26 18:16:39" updated="2012-11-26 18:16:39">
<type id="11">
note</type>
<author id="757127">
tobyink</author>
<data>
<field name="doctext">
&lt;p&gt;As I said, all the OP's examples use the global &lt;c&gt;$_&lt;/c&gt; but some Perl control structures [doc://local|localize] that variable.&lt;/p&gt;

&lt;p&gt;The same effect is illustrated here using global &lt;c&gt;$::x&lt;/c&gt;...&lt;/p&gt;

&lt;code&gt;
use warnings;
use strict;

sub abc()
{
	$::x = 'x';
}

foreach $::x (1..2)
{
	print "Before:$::x\n";
	abc();
	print "After:$::x\n";
}

print "\$::x = $::x\n";  # warning for uninitialized value $x (global scope)

abc();
print "\$::x = $::x\n";  # prints $x = x
&lt;/code&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-757127"&gt;
&lt;small&gt;&lt;small&gt;
&lt;tt&gt;perl -E'sub Monkey::do{say$_,for@_,do{($monkey=&amp;#x5B;caller(0)]-&gt;&amp;#x5B;3])=~s{::}{ }and$monkey}}"Monkey say"-&gt;Monkey::do'
&lt;/tt&gt;&lt;/small&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1005679</field>
<field name="parent_node">
1005754</field>
</data>
</node>
