Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

What is %_

by Russ (Deacon)
on May 16, 2000 at 01:48 UTC ( [id://11757]=perlquestion: print w/replies, xml ) Need Help??

Russ has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Random synapse firing:
(Thinking of the next "syllabus entry" for some Perl training I do around work)
It occurred to me that Perl has $_ (default variable), @_ ("my" arguments), but there seems to be no %_.

Perl does reserve this symbol, however:

russ@MyLinuxBox:~$ perl -w my %_ = (a => 1, b => 2); Can't use global %_ in "my" at - line 1, near "my %_"
Since I find no mention of it in `man perlvar`, nor in any other man page I've searched...nor in any book I own, this may make an interesting SoPW question.

So, what does it do (if anything)?
Should I avoid using it, and if so, why?

Russ

P.S. My intention was to store incoming "named arguments" (see Object Oriented Perl by Damian Conway p. 31) in an idiomatic fashion.

sub mySub{ my %Defaults = (Name => 'Russ', Job => 'Programmer', Language =? 'Perl'); my %_ = (%Defaults, @_); # Warning, this does not compile! ### #(Now use %_ as my argument list) ### print "The name is: $_{Name}\n"; }

Replies are listed 'Best First'.
Re: What is %_
by takshaka (Friar) on May 16, 2000 at 05:25 UTC
    With the Perl special variables, the entire typeglob is declared (global in package main), even though only one or two types actually may be used by Perl. So you get lots of extraneous globals available, like @*, %_, @@, %%, $INC, etc.

    Unless you're writing obfuscated code, it's probably a better idea to create your own lexical variable.

Re: What is %_
by guice (Scribe) on May 16, 2000 at 02:25 UTC
    Well, I'm not totally sure what sets %_ myself, but I have found out that you can use %_.
    Just don't 'my' it:
    &mySub(); sub mySub{ my %Defaults = (Name => 'Russ', Job => 'Programmer', Language => 'Perl'); local %_ = (%Defaults, @_); # Warning, this does not compile! ### #(Now use %_ as my argument list) ### print "The name is: $_{Name}\n"; }
    That works perfectly fine. It would be intersting to see if there is an internal perl action/method/function that will actually set %_, like it does with $_ and @_;

    Any takers?

    -- philip
    We put the 'K' in kwality!

Re: What is %_
by ikegami (Patriarch) on Sep 29, 2015 at 18:32 UTC

    %_ 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.

RE: What is %_
by lhoward (Vicar) on May 16, 2000 at 02:13 UTC
    Scanning through the perl 5.6 source-code I found some interesting hints, but no direct evidence as to %_ real purpose.

    from /ext/Opcode/Safe.pm

    By default, the only variables shared with compartments are the "underscore" variables $_ and @_ (and, technically, the less frequently used %_, the _ filehandle and so on).
    perlguts has the following mention
        L        (none)              Debugger %_<filename
        l        vtbl_dbline         Debugger %_<filename element
    
Re: What is %_
by Russ (Deacon) on May 16, 2000 at 06:50 UTC
    Thanks for the posts so far.

    I've searched the perl-porters mailing list archive. Other than obfuscated .sigs, etc., I've only found one thought-provoking mention (in a comment in a patch) of %_. The author says (something like) "Someday, we may have to change the %_ hack."

    Anyone know if %_ has a specific purpose, or is it an artifact of the typeglob creation, per takshaka?

    Follow-up question...
    Since _ variables ($_, @_) hold arguments, would %_ make intuitive sense to you as "the variable holding named arguments"? It really was a random thought, but since I thoroughly enjoy learning idiomatic Perl, this idea struck me with some force.

    Other than declaring it local (which suggests some deeper meaning in and of itself), how would you feel about code (a subroutine) which stores its named arguments in %_? Does this help or hinder self-documentation?

    Russ

    P.S. Why can't I "maintain" my original post? I would prefer to simply edit it, rather than make a new post for updates...

      "%_<filename" has a purpose in the debugger (see %DB::dbline), and that may be what the author of the patch meant by "the %_ hack".

      I also noticed that the module "English" assigns the whole typeglob (*_) to the name "ARG", i.e. @ARG becomes equivalent to @_, $ARG equivalent to $_, and therefore %ARG becomes equivalent to our mysterious %_. This further bolsters the theory that it's just a leftover from the others in the same typeglob.

      > Since _ variables ($_, @_) hold arguments, would %_ make
      > intuitive sense to you as "the variable holding
      > named arguments"? It really was a random thought, but
      > since I thoroughly enjoy learning idiomatic Perl, this
      > idea struck me with some force.

      Doesn't seem intuitive to me, sorry. I prefer named variables.

      > Other than declaring it local (which suggests some deeper
      > meaning in and of itself), how would you feel about
      > code (a subroutine) which stores its named arguments
      > in %_? Does this help or hinder self-documentation?

      Definitely hinders. In general, you want to stay away from using global special variables in the way that they were not intended, even if they appear as if they are not being "used" at the moment. Future versions of perl may do something with the %_ hash - who knows? The whole reason you cannot declare it with 'my' is because it is a special global variable. You can localize these, but not "my" them. Better to just create something like %subargs, or even %_subargs if you really like the underscore. :)

      Finally, as to your lack of editing problem, try complaining at the Editor Requests section. It may be a bug in the code, as sometimes (albeit very rarely) I cannot edit my own nodes either.

Re: What is %_
by Preceptor (Deacon) on Sep 29, 2015 at 14:45 UTC

    At risk of a spot of necromancy - I've just been handed a use of %_:

    @list = grep { ! $_{$_} } @list;

    It's neat because it's self contained. Although it does seem to leave some namespace pollution as a result

    Found via: http://stackoverflow.com/questions/32846792/what-is-in-perl

      > It's neat because it's self contained. Although it does seem to leave some namespace pollution as a result

      If you want to use this "unique" construction multiple times, it'll break if you don't clean up %_ afterwards.

      So its neat for a one liner, in every other situation I'd rather prefer an explicit my %seen or importing uniq from List::MoreUtils .

      update

      Btw your code does nothing, you forgot the ++ part.

      @list = grep { !$_{$_}++ } @list;

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: What is %_
by Incorporeal (Initiate) on Jul 18, 2007 at 21:33 UTC
    I agree with all above comments, and would like to emphasize that *_ is more often than not better to just leave built-in functions assigning to, rather than yourself.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-19 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found