Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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
Re^4: What is the scope of $_?
by Lotus1 (Chaplain) on Nov 27, 2012 at 13:49 UTC

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-12-27 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls