Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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'

Comment on Re^3: What is the scope of $_?
Select or Download Code
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 imbibing at the Monastery: (14)
As of 2015-11-26 14:00 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (701 votes), past polls