accessing hash values with %hash->{foo} ?



I ran across this bizarreness while cleaning up someone else's old
code. A short-but-complete script to demonstrate:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %hash;
%hash->{alpha} = 'one';
%hash->{beta} = 'two';

print Dumper(\%hash);
__END__

$VAR1 = {
'beta' => 'two',
'alpha' => 'one'
};


Why does this work? I cannot seem to find any reference to this
alternate hash syntax of using the % sigil and an arrow to access the
values of a plain hash. I made cursory examinations of perldata and
perlref, but nothing jumped out at me.

Interestingly, running this through -MO=Deparse gives the following
output:
my %hash;
$hash{'alpha'} = 'one';
$hash{'beta'} = 'two';
print Dumper(\%hash);

which seems to suggest that the interpeter is allowing %hash->{foo} to
be syntactic sugar for $hash{foo}.

Does anyone know of a reason for this, or of a documentation that
mentions it?

(This is perl, v5.6.1 built for sun4-solaris)

Thank you,
Paul Lalli

.



Relevant Pages

  • Re: Perl Analyzing Maillog
    ... "Nick Chettle" wrote in message ... ... use warnings; ... use strict; ... should use a hash instead. ...
    (perl.beginners)
  • Re: problems with logical && (and) statement
    ... am trying to check for is if the host is on the same network in both lists, ... use strict; ... use warnings; ... Odd number of elements in hash assignment at ./clpm.pl line 22. ...
    (perl.beginners)
  • Re: Printing an Array: Formatting Problem
    ... as key for the hash. ... use strict; ... use warnings; ... Do you care at all about invalid IP addresses? ...
    (comp.lang.perl.misc)
  • Re: Processing multiple delimiter files
    ... You got stuck way before here (where you claim that you can ... Here is one way to create the hash: ... use warnings; ... use strict; ...
    (comp.lang.perl.misc)
  • Re: Hash Manipulation
    ... The Perl programming lanuage has what it called a 'strict' mode. ... I want this table cell data to be Red and in Bold. ... "attributes values in a hash" for that particular data value. ...
    (perl.beginners)