Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: What is the scope of $_?

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

in reply to What is the scope of $_?

There is more than one $_. All your examples use the global $_. This is global in scope (as are all the "punctuation variables", except @_ which is just weird).

However, certain constructs implicitly localize variables. for loops do this.

Also (from Perl 5.10) you can create a new variable called $_ using my $_ or given (though the behaviour of given will change in 5.18 - it will not implicitly create a lexical $_). These have the same scope as any other lexical variables - the remainder of the block they're defined in, where the file itself counts as a block.

If you want to access your caller's lexical $_, then the official way is to use a sub prototype. I've also written lexical::underscore which allows you to delve multiple levels down the call stack.

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^2: What is the scope of $_?
by kennethk (Abbot) on Nov 26, 2012 at 18:40 UTC
    <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 @_;

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

      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'
Re^2: What is the scope of $_?
by Lotus1 (Curate) on Nov 26, 2012 at 19:46 UTC

    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

      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'

        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://1005709]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-01-24 00:14 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (199 votes). Check out past polls.