Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: What is %_

by ikegami (Patriarch)
on Sep 29, 2015 at 18:32 UTC ( #1143359=note: print w/replies, xml ) Need Help??


in reply to What is %_

%_ is a hash. You can have a hash named _ because _ is a valid name for a variable. (I'm sure you are familiar with $_ and @_.)

No Perl builtin currently sets it or reads it implicitly, but punctuation variables are reserved. "Perl variable names may also be a sequence of digits or a single punctuation or control character (with the literal control character form deprecated). These names are all reserved for special uses by Perl"

Punctuation variables can't be declared as lexical variables, which is why my %_; is failing. As with other punctuation variables, the best you can do is local %_;. Keep in mind that use strict; won't help remind you of the need to do this, so it's more error prone than using a non-punctuation variable.

Punctuation variables are also special in that they are "super globals". This means that unqualified reference %_ refers to %_ in the root package, not %_ in the current package.

$ perl -E' %::x = ( "%::x" => 1 ); %::_ = ( "%::_" => 1 ); %Foo::x = ( "%Foo::x" => 1 ); %Foo::_ = ( "%Foo::_" => 1 ); package Foo; say "%x = ", keys(%x); say "%_ = ", keys(%_); say "%::x = ", keys(%::x); say "%::_ = ", keys(%::_); say "%Foo::x = ", keys(%Foo::x); say "%Foo::_ = ", keys(%Foo::_); ' %x = %Foo::x %_ = %::_ <-- surprise! %::x = %::x %::_ = %::_ %Foo::x = %Foo::x %Foo::_ = %Foo::_

This means that forgetting to use local %_ can have very far-reaching effects.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-12-08 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?