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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: What is the scope of $_?
by tobyink (Canon) on Nov 26, 2012 at 23:16 UTC | |
by Lotus1 (Vicar) on Nov 27, 2012 at 13:49 UTC |