<?xml version="1.0" encoding="windows-1252"?>
<node id="1005754" title="Re^2: What is the scope of $_?" created="2012-11-26 14:46:51" updated="2012-11-26 14:46:51">
<type id="11">
note</type>
<author id="922586">
Lotus1</author>
<data>
<field name="doctext">
&lt;p&gt;If all the OP's examples use the global &lt;c&gt;$_&lt;/c&gt; then  printing it at the end would show the last value assigned. What actually happens is a warning for 'Use of uninitialized value $_ ...' The subroutine uses whatever $_ that is in scope when it is called. 
&lt;/p&gt;
&lt;c&gt;
use warnings;
use strict;

sub abc()
{
  $_ = 'x';
}

foreach(1..2)
{
    my $loopvar = 'in the loop';
  print "Before:$_\n";
  abc();
  print "After:$_\n";
}

print "\$_ = $_\n";  # warning for uninitialized value $_ (global scope)

abc();
print "\$_ = $_\n";  # prints $_ = x
&lt;/c&gt;</field>
<field name="root_node">
1005679</field>
<field name="parent_node">
1005709</field>
</data>
</node>
