Summary: in this tutorial, we will show you how to use various kinds of references in Perl including anonymous and symbolic references. We will also introduce you to the autovivification concept.
If you don’t know anything about references, you should follow the Perl reference tutorial first before going forward with this tutorial.
Let’s start with a new concept called anonymous references.
Perl anonymous references
As you’ve seen in the Perl Reference tutorial, we created references that refer to existing variables such as scalars, arrays and hashes. However, Perl allows you to create references that refer directly to the data without creating variables. These types of references are called anonymous references. The rules for creating anonymous references are as follows:
- To get an array reference, use square brackets
[]
instead of parentheses. - To get a hash reference, use curly brackets
{}
instead of parentheses.
Anonymous array references
The following example illustrates how to create an anonymous array reference:
#!/usr/bin/perl
use warnings;
use strict;
my $ar = [1..5];
# loop over the array elements
for(@$ar){
print("$_ "); # 1 2 3 4 5
}
print("\n");
Code language: PHP (php)
We put an anonymous array reference [6..9]
inside array reference. So the sixth element of the array is a reference, we handled it differently from other elements inside the loop by dereferencing it by using the @$
prefix.
Anonymous hash references
To create anonymous hash references, you use curly brackets {}
. Let’s take a look at the following example:
#!/usr/bin/perl
use warnings;
use strict;
my $address = { "building" => 1000,
"street" => "ABC street",
"city" => "Headquarter",
"state" => "CA",
"zipcode" => 95134,
"country" => "USA"
};
print $address->{building};
print $address->{street};
Code language: Perl (perl)
you use the operator ->
to refer to each hash element.
Perl symbolic references
The symbolic reference is defined as the use of a scalar value as a string, which in turn gives a variable its name.
Let’s take a look at the following example:
#!/usr/bin/perl
use warnings;
my $foo = "bar";
$$foo = "whatever";
print $$foo , "\n"; # whatever
print $bar, "\n"; # whatever
Code language: Perl (perl)
How the program works.
- First, we declared a scalar variable
$foo
and set it value to the string “bar
“. - Second, we tried to dereference a non-existent reference using
$foo
, and set its value to “whatever
” string. Behind the scenes, Perl created a new reference variable for us called$bar
. That’s why you see we can display the value of$bar
in theprint()
function.
The problem with the symbolic reference is that Perl creates a new symbolic reference in cases that you’re not expecting.
If you use the strict pragma, Perl will prevent you from using symbolic references. If you take a look at the example above carefully again, you will see that we removed the use strict
statement for the sake of symbolic reference demonstration.
Now, it’s time to introduce a very important concept in Perl called autovivification.
Autovivification
Autovivification is a unique feature of Perl that creates a reference variable auto-magically when you dereference an undefined value.
If you dereference a defined value, which is not a reference to the proper type, Perl will create a symbolic reference. In other words, autovification creates and expands data structure at the first use. The autovivification applies to not only scalars but also arrays and hashes.
#!/usr/bin/perl
use warnings;
use strict;
my $foo->{address}->{building} = 1000;
print $foo, "\n"; # HASH(0x3bad24)
print $foo->{address}, , "\n"; # HASH(0x3bae04)
print $foo->{address}->{building},"\n"; # 1000
Code language: Perl (perl)
Because $foo
was being used a hash reference, creating a hash with a key as the address so Perl created a new hash reference variable $foo
.
In addition, the $foo->{address}
was also being used as a hash reference so Perl also created a new hash reference variable.
In this tutorial, you’ve learned two kinds of references: anonymous reference and symbolic reference. We’ve also introduced you to the autovification concept.