Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hash References

  • %hash
  • HASH

Similarly to the Array references one can create references to Hashes as well.

my $phones_ref = \%phones;
Hash            Hash Reference
%phones         %{ $phones_ref }
$phones{Foo}    ${ $phones_ref }{Foo}
                $phones_ref->{Foo}
keys %phones    keys %{ $phones_ref }

Using the print function to print out the content of an Array was quite OK. Printing a Hash was a disaster. The same happens once you dereference a reference to an array or hash.

#!/usr/bin/env perl
use strict;
use warnings;

my %phones = (
    Barney  => 123,
    Fred    => 456,
    Wilma   => 789,
);
my $phones_ref = \%phones;

print $phones_ref, "\n";             # HASH(0x703fed1)
my @names = sort keys %$phones_ref};
print "@names\n";                    # Barney Fred Wilma

print "$phones{Fred}\n";             # 456
print "$phones_ref->{Fred}\n";       # 456