Perl Reference to a Hash
Perl References - Part 3
Forward: In this part of the series, we look at Perl Reference to a Hash.
By: Chrysanthus Date Published: 12 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
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.
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,
%{$href}
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:
${$href}{'key'}
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:
$href->{'key'}
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;
print "Content-Type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Code Sample</title>\n";
print "</head>\n";
print "<body>\n";
my %ha = (Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green");
my $href = \%ha;
print $$href{'Apple'} . "<br />";
my $haref = {Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green"};
print $haref->{'Banana'};
print "</body>\n";
print "</html>\n";
That is it for Perl Reference to a Hash. We stop here and continue in the next part of the series.
Chrys
Related Links
Perl ReferenceObject Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design
NEXT