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

At 340460, I was replying and used the construct $self->{_$_}. I didn't quote it because hashkeys autoquote. Yet. Fletch correctly pointed out that $self->{_$_} deparses to $self->{$_->_}. Not only that, but it actually executes!
perl -e '$self->{a$_};print keys %$self, $/;' -------- Can't call method "a" on an undefined value at -e line 1.

Going further, I get the following:

package Foo; sub new { my $c = shift; bless \$c, $c } sub bar { 'bar' } package main; $foo = Foo->new; $x{bar$foo} = 1; print "$_ => $x{$_}\n" for keys %x; -------- bar => 1

Further testing shows that $x{bar $foo} and $x{$foo->bar} also work.

Now, I knew that $x{bar()} works, because that's how you get constants in there. But, method calls?

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re: Hashkey stringification ... sometimes.
by bart (Canon) on Mar 29, 2004 at 12:43 UTC
    Don't rely on "automatic quoting" too much... It's only garanteed to work for barewords. _$_ most definitely is not a bareword (= the kind of word one normally uses for variable and sub names). The same goes for the left hand side of =>, I'm sure _$_ will do the same magic trick there as it did in your code...
    my %hash = ( _$_ => 'foo' );
    this shows the following error when ran like this:
    Can't call method "_" on an undefined value 
    

    The fact that it actually compiles, without any complaint, may be a bit unfortunate... But it's nothing out of the ordinary.

      I just found an uglier case.
      rob@dev01 $ perl -MO=Deparse -e '$x{_$}' Missing right curly or square bracket at -e line 1, at end of line syntax error at -e line 1, at EOF -e had compilation errors. rob@dev01 $ perl -MO=Deparse -e '$x{_$}}' $x{$}->_}; -e syntax OK

      Just interpose the _ and $ in $_. In almost 9 years, I've never run into that. I must have typed "{$_}" almost a million times and never mistyped it. *shudders* That's the kind of typo that brings grey hair to men in the teens!

      And, then you have $x{y$}, which is transliteration, $x{s$}, which tries to do substitution ... either it quotes or it doesn't!

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Hashkey stringification ... sometimes.
by broquaint (Abbot) on Mar 29, 2004 at 12:42 UTC
Re: Hashkey stringification ... sometimes.
by QM (Parson) on Mar 29, 2004 at 14:15 UTC
    For those just tuning in to Perl, notice that _ (underline, underbar, or just bar) is a legal identifier, hence a legal sub name and method name.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      But remember that it defaults to package main if no explicit package is specified, whether you use $_, @_, %_, or _:
      $ perl -we'package Foo; sub _ { print "hi" } package Bar; _; main::_' hihi