Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: What is the scope of $_?

by tobyink (Abbot)
on Nov 26, 2012 at 23:16 UTC ( #1005774=note: print w/replies, xml ) Need Help??

in reply to Re^2: What is the scope of $_?
in thread What is the scope of $_?

As I said, all the OP's examples use the global $_ but some Perl control structures localize that variable.

The same effect is illustrated here using global $::x...

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
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^4: What is the scope of $_?
by Lotus1 (Deacon) on Nov 27, 2012 at 13:49 UTC

    So when the foreach loop finishes it restores the undef value to $_. Thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005774]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2016-10-24 17:30 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.