Broad Network

Reference to a Hash in Perl

Perl References Optimized Part 3

Perl Course

Foreword: In this part of the series, we look at Perl Reference to a Hash.

By: Chrysanthus Date Published: 10 Jul 2015


This is part 3 of the series, Perl References Optimized. In this part of the series, we look at Perl Reference to a Hash. You should have read the previous parts of the series before reaching here, as this is a continuation.

A Hash Reference
A reference to a scalar or array or hash is a short piece of code (address) that refers to the scalar or array or hash respectively. A reference is held by a scalar variable independent of whether the value is a scalar, or array or hash. There are two common ways to create a reference to a hash.

Creating a Reference with Backslash
The following code segment shows how to create a reference to a hash with a backslash:

    %ha = (Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green");
    $href = \%ha;

The first statement creates a hash in the normal way. In the second statement the right operand is the hash variable preceded by the backslash. The resulting right operand, which is a reference to a hash, is assigned to a scalar variable. So the reference to a hash is held by a scalar variable and not a hash variable. A reference is just a short piece of code (address) that refers to a region in memory; a hash with its elements occupy a region in memory. When dealing with a reference, you are more interested in the region occupied by a value (hash) in memory than the variable that identifies the value.

Reference from Anonymous Hash
Anonymous means, no name. The following code shows how to create a reference from an anonymous hash:

my $href = {Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green"};

This hash is the same hash as the one above, but it has no name. It would occupy the same memory region as the one above. However, here, instead of arc brackets, you have curly braces. The curly braces return a reference to the hash (region). This reference, which would be the same as the one above (%ha) is assigned to the scalar variable, $href. The reference to a scalar, array or hash is held by a scalar variable.

In the previous reference situation, the name of the hash is ha; however, here the array has no name. That is why you have one statement here and not two in order to create the reference.

Obtaining a Hash from a Reference
One way to get the hash from a hash reference (variable holding the reference) is to use the braces (again, as with the array). For the above reference, you would type,


You begin with the hash sign, %, since you are dealing with a hash. This is followed by braces. Inside the braces, you have the variable that holds the reference to the hash.

You usually do not use the hash as a whole (as indicated above). You usually use an element from the hash. For a hash that has a name, if you want to use the hash name to get an element, you would type something like,


where the name of the hash (variable) is ha. When you have a reference to the hash, you do a similar thing but with the braces as follows:


That is you replace, ha, with {$href}.

Another way of accessing a hash is applicable when you also want an element (value of key) from the hash (this is what you do most of the times). With this way, you do not start with the preceding scalar sign, $. You also omit the braces. However, you follow the hash reference variable, with an arrow, -> (minus sign followed by greater than sign), as in the following example:


Now, %{$aRef} can be abbreviated to %$aRef without the braces, and similarly ${$href}{'key'} can be abbreviated to $$href{'key'}. However, $href->{'key'} cannot be abbreviated by omitting ->, as $href is holding a reference and href is not the name of a hash.

Read and try the following code, which illustrates the use of reference to a hash:

use strict;

my %ha = (Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green");
my $href = \%ha;
print $$href{'Apple'} . "\n";

my $haref = {Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green"};
print $haref->{'Banana'};

That is it for Perl Reference to a Hash. We stop here and continue in the next part of the series.


Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course



Become the Writer's Fan
Send the Writer a Message