http://qs1969.pair.com?node_id=1143359


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.