Perl Basics   «Prev  Next»

Lesson 20Hash references
Objective Learn how to use references to hashes.

Perl Hash References

In the previous lesson you learned how to use references to arrays and anonymous arrays. In this lesson we will cover references to hashes and anonymous hashes. Creating hash references is very similar to creating array references. First consider this hash from the "Perl hashes" lesson:

Perl Hash Example

%viceprez = (
   'Franklin' => 'Henry A. Wallace',
   'Harry'    => 'Alben W. Barkley',
   'Dwight'   => 'Richard M. Nixon',
   'John'     => 'Lyndon B. Johnson', 
   'Lyndon'   => 'Hubert H. Humphrey',
   'Richard'  => 'Spiro T. Agnew',
   'Gerald'   => 'Nelson A. Rockefeller',
   'Jimmy'    => 'Walter F. Mondale',
   'Ronald'   => 'George H. Bush',
   'George'   => 'J. Dan Quayle',
   'Bill'     => 'Al Gore, Jr.',
   );

You can create a reference to that hash like this:
 $hashref = \%viceprez;
 
You now have a reference relationship just like you did with array and scalar references.


Perl hash diagram consisting of Vice Presidents
Perl hash diagram consisting of Vice Presidents

You can dereference the hash like this:
$foo = $$hashref{'Franklin'};    # 'Henry A. Wallace'

or like this:
$foo = $hashref->{'Franklin'};   # 'Henry A. Wallace'

In fact, Perl 5 makes a special allowance for using a bareword to index a hash, just so that you can do this:

$foo = $hashref->{Franklin};     # 'Henry A. Wallace'

This is probably the most common way to do it.
For all the C programmers taking this, I have a warning that should help prevent some confusion with hashes.


Warning for C programmers

It should be noted that while this is legal:
  1. $$hashref{OTHERS}{'calvin'}
  2. $$arrayref[3][1]
may be confusing to C programmers who expect operator precedence to work differently in Perl.
The truth is, the $ is not even an operator in Perl, so the comparison does not work.
Rather than trying to forget everything you ever learned about C, go ahead and just use the
  1. $hashref->{OTHERS}{'calvin'} and
  2. $arrayref->[3][1] constructs.

Array/Hash References Versus Element References

Recall that there is a distinction between the array as a whole and each of its constituent scalar values. The array's value maintains its own reference count, and each of its elements has its own. When you take a reference to an array, its own reference count is incremented without its elements getting affected, as shown in Figure 2.6.
Array Hash Reference Values
Figure 2.6. - Take a reference to an array, its own reference count is incremented without its elements getting affected

In contrast, Figure 2.7 shows the picture when you create a reference to an element of an array or a hash.
when you create a reference to an element of an array or a hash
Figure 2.7 - Create a reference to an element of an array or a hash


Additional topics to look at are :
  1. Anonymous Perl Hashes
  2. Using hash reference as hash

Hash Function - Exercise

Click the Exercise link below to answer several questions with respect to a hash example.
Hash Function - Exercise

Perl Complete Reference