develooper Front page | perl.perl5.porters | Postings from July 2013

Re: [perl #118701] __PACKAGE__ does not work as a hash key

Thread Previous | Thread Next
Jim Avera
July 3, 2013 01:10
Re: [perl #118701] __PACKAGE__ does not work as a hash key
Message ID:
On 07/02/2013 03:52 PM, James E Keenan via RT wrote:
> As Father Chrysostomos explained, hash keys always autoquote
>> identifiers.
>>> Special-casing __PACKAGE__ would introduce another hidden gotcha,
>> not
>>> eliminate it.
>> If you need the special literal there are a few easy ways to do it
>>      perl -E'%h=(main=>"Hello World"); say $h{+ __PACKAGE__ }'
>>      perl -E'%h=(main=>"Hello World"); say $h{ __PACKAGE__, }'
>>      perl -E'%h=(main=>"Hello World"); say $h{ ( __PACKAGE__ ) }'
>>      perl -E'%h=(main=>"Hello World"); say $h{ do{ __PACKAGE__ } }'
>> basically anything that tells perl (the compiler) that you want
>> it treated as a statement will work.
>> on 5.16 or later you can also pretend it is a subroutine
>>      perl -E'%h=(main=>"Hello World"); say $h{ __PACKAGE__() }'
>> So, do we have a bug here?
>> A feature?
>> Something that needs better documentation?
>> Thank you very much.
>> Jim Keenan
IMO hidden or silent gotchas are the last significant 
software-engineering risk specific to Perl, now that the fundamental 
machinery works reliably (and btw, thanks immensely to all who made that 

The special __(PACKAGE|FILE|LINE)__ symbols are well-documented and 
vanishingly unlikely to be innocently used as an application symbol.  So 
it's hard to see how special-casing them to always be treated as string 
literals with the implied values (and never auto-quote themselves) would 
make things worse.    Removing hidden or silent traps is always a good 
thing in the abstract.

But I'm not familiar with perl internals, so doing so might be 
excessively risky.

I think those who would do the work are the only ones qualified to say 
whether fixing this is prudent.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About