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

Just stumbled across the fact that %hash->{key} is valid syntax (even under strict!) to access a hash key:
use strict; use warnings; use Data::Dumper; my %hash; %hash->{foo} = "bar"; print Dumper(\%hash);
Anybody knows why and can point to a man page explaining it?

Replies are listed 'Best First'.
Re: %hash->{key} is valid syntax?
by jweed (Chaplain) on Oct 27, 2004 at 00:46 UTC
    use diagnostics; reveals the root of the issue:
    Using a hash as a reference is deprecated at tmp.pl line 7 (#1)
    (D deprecated) You tried to use a hash as a reference, as in %foo->{"bar"} or %$ref->{"hello"}. Versions of perl <= 5.6.1 used to allow this syntax, but shouldn't have. It is now deprecated, and will be removed in a future version.



    Code is (almost) always untested.
    http://www.justicepoetic.net/
Re: %hash->{key} is valid syntax?
by pg (Canon) on Oct 27, 2004 at 00:39 UTC

    Not really. It gives you this warning:

    Using a hash as a reference is deprecated at d.pl line 7.
      Not really. It gives you this warning: Using a hash as a reference is deprecated at d.pl line 7.
      Gotcha -- I was using perl 5.6.1, which didn't warn. Thanks!
Re: %hash->{key} is valid syntax?
by TedYoung (Deacon) on Oct 27, 2004 at 12:23 UTC

    Yes %h->{foo} is the same thing as $h{foo}, just like @a->[2] is the same as $a[2]. It provided a way to deal with hashes and arrays without needing to learn about sigils. It was depricated in 5.8.1. I had asked a question about this a while back.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)