Lesson 17 | Perl references |
Objective | Learn how Perl 5 uses references to aggregate data into arbitrarily larger dimensions. |
Perl References
In the previous lessons you learned about Perl's basic aggregate data types: arrays and hashes. Arrays and hashes are useful, but they have one major drawback:
they are limited to two-dimensional data, and a lot of data has more than two dimensions. In the next few lessons you will learn how Perl 5's new references feature allows you to aggregate data into arbitrarily larger dimensions.
Using references, you can create lists and hashes of lists and hashes. Before we get into the really complex data structures, we will need to cover the basics of Perl references. This is the stuff that the more-complex data structures are made of.
Perl References
Perl 5 introduced a new feature, called references, whereby you can have a scalar variable that refers to the contents of another
object.
For example,
$prez = 'George';
$prezref = $prez;
print "prez is $prez\n";
print "prezref is $prezref\n";
print "this is a reference to $$prezref\n";
The above code will produce the following output.
prez is George
prezref is SCALAR(0x80b7310)
this is a reference to George
Before we get into the explanation of all that, let's take a moment and make sure we agree on some
definitions.
In the next three lessons, we are going to walk through creating three different types of references.
Pay careful attention; this information builds on itself.
How are references used in Perl?
In Perl, references are used to store data structures and objects, allowing for more complex and dynamic data structures to be created and manipulated. A reference is a scalar value that points to another value, rather than storing the value directly.
For example, a reference to an array can be created using the \@ operator, and can be used to access the array elements:
my @array = (1, 2, 3);
my $array_ref = \@array;
print $array_ref->[0]; # prints 1
A reference to a hash can be created using the \% operator:
my %hash = (key1 => "value1", key2 => "value2");
my $hash_ref = \%hash;
print $hash_ref->{key1}; # prints "value1"
Referencing is a powerful feature in Perl, allowing for the creation of complex data structures such as arrays of arrays, hashes of hashes, and more.
Definitions: reference, dereference, object, anonymous
- Reference: noun a reference is not the object itself, but an object that refers to another object, like a pointer in C (only you cannot do pointer arithmetic in Perl).
- Dereference:
- noun an object that is being accessed via a reference.
verb the act of extracting data from a reference.
- Object: In Perl, all data are objects. I know this word has magical meaning in some contexts, that is why I bothered to define it here. It does not mean anything magical in Perl, because most of the common magical connotations of the word already apply to Perl objects anyway.
- Anonymous: An object without a name. Anonymous objects can be accessed only through references.