Lesson 05 – References

In Perl, there is no such things as array of arrays, array of hashes, hash of arrays and hash of hashes. Perl has no values that are arrays or hashes.

$president{'name'} = ('Clinton', 'Bush', 'Obama');

This is a scalar assignment (because you’re assigning to a scalar variable). That means the right-hand side will be evaluated in scalar context.

in scalar context, the comma operator evaluates its left operand, throws the result away, then returns the right operand.

$x = (1, 2);

In this line, 1, 2 will evaluate 1, ignore it, then return 2 and you’ll get a warning for having a constant in void context.

“You can’t have a hash whose values are arrays; hash values can only be scalars.” – from perlreftut

In Perl, “value” is synonymous with “scalar value”. Perl is somewhat unique with its containers/values duality. You have containers like scalar variables, arrays, and hashes and then you have values like undef, numbers, strings, references.

The main things to keep in mind are the behaviors of arrays, hashes, and the comma operator in scalar/list context. In list context it evaluates both of its operands in list context, then concatenates them. Or shorter: , is list concatenation. An array in scalar context yields the number of its elements. An array in list context yields a list of its elements. A hash in scalar context yields a string (this is somewhat obscure). the main thing to keep in mind is that for an empty hash that string will be “” (false), and for a non-empty hash it will be true.

In Perl, when we use an array with a reference to another array, that reference is considered a scalar.

In Perl, an array is a container and a list is … multiple values.
The following example uses references to build a nested data structure. It is an anonymous hash inside a hash. Notice how it uses curly braces for the anonymous hash instead of parentheses. (Square brackets would be used if it was an anonymous array.)

use strict;
use warnings;

my %pizza = (
              "plain" => {"medium" => 7.99, "large" => 9.99}, 
              "pepperoni" => {"medium" => 9.99, "large" => 10.99}, 
              "supreme" => {"medium" => 10.99, "large" => 12.99}, 
              "veggie" => {"medium" => 9.99, "large" => 10.99}

print "A large pepperoni pizza costs $", $pizza{pepperoni}{large}, "\n";

Of course, you can build the same nested data structure by explicitly assigning a hashref to a scalar variable and using it inside a hash.

my %plain_pricing = ("medium" => 7.99, "large" => 9.99);
my $plain = \%plain_pricing;
my %pepperoni_pricing = ("medium" => 9.99, "large" => 10.99);
my $pepperoni = \%pepperoni_pricing;
my %supreme_pricing = ("medium" => 10.99, "large" => 12.99);
my $supreme = \%supreme_pricing;
my %veggie_pricing = ("medium" => 9.99, "large" => 10.99);
my $veggie = \%veggie_pricing;
my %pizza2 = (
               "plain" => $plain, 
               "pepperoni" => $pepperoni, 
               "supreme" => $supreme, 
               "veggie" => $veggie

print "A large pepperoni pizza costs $", $pizza2{pepperoni}{large}, "\n";

You can also assign a value directly to the nested data structure and Perl will automatically create the reference.

my %pizza3;

$pizza3{pepperoni}{large} = 10.99;
print "A large pepperoni pizza costs $", $pizza3{pepperoni}{large}, "\n";

If you want to explicitly destroy a reference, you write undef $ref;

Normally there is no need to manually clear references. They get destroyed at the end of whatever scope they are used in.

Think of it as “once the variable is no longer needed, Perl undef()s it for you.”

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>