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

Does %_ have a function or is it just for fun?

by larard (Monk)
on Jul 16, 2007 at 17:27 UTC ( [id://626885]=perlquestion: print w/replies, xml ) Need Help??

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

I was recently bitten by a bug in my code, where I accidentally wrote
$_{foo}; #$_ containts a hashref.
I had meant to use $$_{foo}, but was suprised that despite using strict and warnings, perl had seemingly autodeclared %_.
My original code read something like this, to show it isn't completely contrived.
use strict; use warnings; use DBI; use Data::Dumper; #connect and execute stored procedure #... push @results, $_{user_id} while ($_ = $sth->fetchrow_hashref()); print Dumper(\@results);
Which prints
$VAR1 = [ undef, undef, undef ];
I soon spotted the problem, caused by my ever shoddy typing. However a trip to perlvar failed to enlighten. Further investigation revealed that %_ does indeed exist, but is perhaps unused. 'What is %_' was asked a little over 7 years, but didn't elicit a definitive answer.

What is %_ for? If there is no current use, would anyone care to suggest what %_ might be used for?

Replies are listed 'Best First'.
Re: Does %_ have a function or is it just for fun?
by ikegami (Patriarch) on Jul 16, 2007 at 17:48 UTC

    Punctuation variables don't trigger strict errors or "used only once" warnings, even if they have no use.

      and that's documented in the Technical Note on the Syntax of Variable Names in perlvar.

      -derby
Re: Does %_ have a function or is it just for fun?
by injunjoel (Priest) on Jul 16, 2007 at 17:55 UTC
    larard,
    I use %_ in my code here and there.
    I even mention it's use in The Uniqueness of hashes. Have a look.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
Re: Does %_ have a function or is it just for fun?
by FunkyMonk (Chancellor) on Jul 16, 2007 at 21:23 UTC

    I seem to remember reading somewhere (ie I might be wrong), that due to the magic nature of typeglobs and the punctuation variables, that you can't just get a single type for each. That probably isn't too clear, it's easier with an example: Since Perl wants $_, you also get %_, @_ and &_ for free.

    My (very limited) testing demonstrates that that's the case for other punctuation variables, too:

    use strict; use warnings; use Data::Dumper; # $\ - output record separator %\ = ( 1 => 2); @\ = ( 1, 2 ); print Dumper \%\, \@\;; # $@ - eval error %@ = ( 3 => 4); @@ = ( 3, 4 ); print Dumper \%@, \@@;

Re: Does %_ have a function or is it just for fun?
by ForgotPasswordAgain (Priest) on Jul 16, 2007 at 17:37 UTC

    I don't know "why", but I just note incidentally that you can also do that with the coderef slot:

    $ perl -Mstrict -Mwarnings -le'*_=sub{"hi"}; print &_' hi
      There's nothing special about _ in your post. Typeglobs are unconditionally not subject to strict.
      >perl -Mstrict -Mwarnings -le"*f=sub{'hi'}; print &f" hi
Re: Does %_ have a function or is it just for fun?
by naikonta (Curate) on Jul 17, 2007 at 06:51 UTC
    What is %_ for?

    Since it's not documented in perlvar, I don't think it's for particular use. I think that FunkyMonk's reply is making sense. I also got the same impression upon reading about symbol table in perlmod and perlref.

    If there is no current use, would anyone care to suggest what %_ might be used for?

    Well, you can use it as you will normally use hash. Initialize it, assign elements to it, get a value from a key. Since it's sort, it's also perfect for golf. And it can also be confusing so one may use it in obfucasted code. You can use it to alias a hash variable with some lengthy you'd be tried to type it over and over.

    my %hash_variable_with_not_actually_that_long_name = (one => 1); print $hash_variable_with_not_actually_that_long_name{one}, "\n"; *_ = \%hash_variable_with_not_actually_that_long_name; print $_{one}, "\n"; # output 1 1
    Additionally, with the aliasing, you can alias other sigils at once. But, by using _ you're likely to get into problem.
    $_ = 2; print $_, "\n"; *_ = *hash_variable_with_no_actually_that_long_name; print $_{one}, "\n"; print $_, "\n"; # output 2 1 (empty, with 'uninit' warnings under -w)

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Does %_ have a function or is it just for fun?
by impossiblerobot (Deacon) on Jul 18, 2007 at 12:42 UTC

    Variables/symbols that start with punctuation (including punctuation variables themselves) are automatically global, and forced into main:: ... except if the punctuation it starts with is the underscore, which doesn't make it automatically global ... except if it is the underscore variable/symbol itself, which is also automatically global.

    Maybe I need to rewrite that. It's even confusing me now.


    Impossible Robot

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found