Perl Reference

Summary: in this tutorial, you will learn how to create Perl references including scalar reference, array reference, and the hash reference. We will also show you how to manipulate data through references using dereferencing techniques.

What is Perl reference?

Perl Reference

A reference is a scalar variable that “points”  or refers to another object which can be a scalar, an array, a hash, etc.  A reference holds a memory address of the object that it points to.

When a reference is dereferenced, you can manipulate data of the object that the reference refers to. The act of retrieving data through a reference is called dereferencing.

Why do you need Perl references?

Because a reference is a scalar variable, so you can put a reference inside arrays and hashes. You can create complex data structures such as arrays of arrays, arrays of hashes, hashes of hashes, etc.

With references, you can create not only two-dimensional but also multidimensional data structures.

General syntax of references

There are two important rules you should remember when you work with references:

To create a reference to a variable or subroutine, you use the unary backslash operator (\) in front of the variable or subroutine name. For example, to create a reference $foo that refers to scalar $bar, you do it as follows:

$foo = \$bar;
Code language: Perl (perl)

To dereference a reference, you prefix $ to a scalar, @ to an array, % to a hash, and & to a subroutine. For example, to dereference a scalar reference $foo, you use:

$$foo;
Code language: Perl (perl)

Let’s examine scalar reference, array reference and hash reference in more detail.

Perl scalar references

Suppose you have a scalar variable:

my $x = 10;
Code language: Perl (perl)

If you want to create a reference that refers to $x, you use the following syntax:

my $xr = \$x;
Code language: Perl (perl)

Noticed that backslash is used in front of the scalar variable.  If you want to dereference the reference $xr, you put curly braces around the reference as follows:

${$xr}
Code language: Perl (perl)

To make it simpler, you can omit the curly braces:

$$xr
Code language: Perl (perl)

Now, you can manipulate the scalar $x via the reference $xr.  Let’s put everything together:

#!/usr/bin/perl use warnings; use strict; my $x = 10; my $xr = \$x; # change $x via $xr $$xr = $$xr * 2; print("$x\n"); # 20 print("$$xr \n"); # 20 print("$xr\n"); # SCALAR(0x1d2e6e4)
Code language: Perl (perl)

How program works:

  • First, we defined a scalar variable $x and initialize its value to 10;
  • Second, we defined a reference $xr and pointed it to the scalar $x.
  • Third, we dereferenced the reference $xr and multiplied its value with 2. The change has reflected the scalar $x as well.

The reference $xr has value  SCALAR(0x1d2e6e4)There are two parts:

  • The SCALAR means a scalar reference. If you have an array or a hash reference, you will get  ARRAY or HASH.
  • A reference stores memory location of the object it refers to so  0x1d2e6e4 is the memory location of the scalar $x. You may get a different value in your system.

Perl array references

Let’s take a look at the following example:

#!/usr/bin/perl use warnings; use strict; my @a = (1..5); my $ar = \@a; my $i = 0; for(@$ar){ print("$ar->[$i++] \n"); }
Code language: Perl (perl)

How program works.

  • First, we defined an array of integer numbers from 1 to 5.
  • Second, we defined a reference that refers to the array @a. Notice that the backslash operator ( \) is also used in front of array variable like the scalar variable.
  • Third, in the for loop, we dereferenced the reference $ar by using @$arYou can use curly braces @{$ar} if you want. However, shorter is better.
  • Fourth, to refer to each element of the array, we used $ar->[0],   $ar->[1]… There is another syntax to refer to array element which is ${$ar}[0].  
You can also apply array functions such as pop(), push(), shift(), unshift()sort(), etc to the array reference as usual.

Perl hash references

Creating a hash reference is similar to creating an array reference, for example, suppose you have a hash:

my %months= ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12);
Code language: Perl (perl)

You can create a reference to the %month hash as follows:

my $monthr = \%months;
Code language: Perl (perl)

To dereference a hash reference you use:

%$monthr
Code language: Perl (perl)

Because a hash element is a scalar, to access hash elements through the reference you use operator -> as follows:

$monthr->{$key}
Code language: Perl (perl)

The following program demonstrates how to manipulate a hash reference.

#!/usr/bin/perl use warnings; use strict; my %months= ( Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12); my $monthr = \%months; for(keys %$monthr){ print("$_ = $monthr->{$_}\n"); }
Code language: Perl (perl)

The script accessed all hash elements via the reference and displayed them.

In this tutorial, you have learned basic Perl reference including scalar reference, array reference, and hash reference. You’ve also learned how to create references and dereference references to get the data.

We will show you various types of references in Perl in the next tutorial so make sure that you understand the concepts in this tutorial before moving on.

Was this tutorial helpful ?