Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by kennethk (Abbot)
on Nov 26, 2012 at 18:40 UTC ( #1005740=note: print w/ replies, xml ) Need Help??


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

<tangent> How is @_ not global in scope? I mean, it gets implicitly localized on any subroutine call, but it's present by default and package independent:

use strict; use warnings; @_ = @ARGV; package Foo; print for @_; @_ = qw(1 2 3 4 5); { local @_ = reverse @_; print for @_; } package main; print for @_;
</tangent>

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re^2: What is the scope of $_?
Select or Download Code
Re^3: What is the scope of $_?
by tobyink (Abbot) on Nov 26, 2012 at 19:35 UTC

    You are correct. I was under the impression that a reference to @_ behaved differently than a reference to another localized array, once the localization went out of scope.

    But the following example appears to show they behave the same...

    use strict; use Data::Dumper; our @X; my ($underscore, $scissors); @_ = (1..3); @X = (1..3); sub foo { local @X = (4..6); $underscore = \@_; $scissors = \@X; } foo(4..6); print Dumper($underscore, $scissors);

    So (like $_) @_ is just a global array that gets localized by certain control structures. (In particular, sub.)

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2015-07-02 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls