in reply to
Re: What is the scope of $_?
in thread What is the scope of $_?
If all the OP's examples use the global $_ 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.
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 scop
+e)
abc();
print "\$_ = $_\n"; # prints $_ = x