Sobrique Sobrique - 7 months ago 12
Perl Question

What is "%_" in perl?

I've just been given a code snippet:

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


As an idiom for deduplication. It seems to work, but - there's no
%_
listed in
perlvar
.

I'd normally be writing the above by declaring
%seen
e.g.:

my %seen; my @list = grep { not $seen{$_}++ } @list;


But
%_
seems to work, although it seems to be global scope. Can anyone point me to a reference for it? (Or at least reassure me that doing the above isn't smashing something important!)

Answer

It's 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 %_ implicitly, but punctuation variables such as %_ 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


Note that punctuation variables are also special in that they are "super globals". This means that unqualified %_ 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 %_ (as you did) can have very far-reaching effects.