LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1918 min read

References

Learn how to create references with the backslash operator, dereference arrays and hashes, and why references let Perl build nested data structures.

Introduction

So far, every array and hash you have used has been a flat, standalone variable. Real-world data is rarely that simple — think of a list of employees, each with their own set of properties, or a hash of departments, each containing a list of employees. Perl's answer to this is references: a reference is a scalar value that points at another piece of data, similar to a pointer in C or a reference in other languages.

What You Will Learn
  • What a reference is and how it differs from the original data.
  • How to create a reference with the backslash operator.
  • How to dereference array and hash references.
  • Why references are essential for nested data structures.

What Is a Reference?

A reference is a scalar value that stores the location of another piece of data — an array, a hash, a scalar, or even a subroutine — rather than a copy of that data itself. Because a reference is just a scalar, you can store it in a normal $variable, pass it to subroutines cheaply (no copying a huge array), and store several of them inside another array or hash to build nested structures.

Creating a Reference

You create a reference to an existing variable by putting a backslash (\) in front of it. This works for arrays, hashes, and scalars alike.

use strict;
use warnings;
my @colors = ('red', 'green', 'blue');
my %capital = (France => 'Paris', Japan => 'Tokyo');
my $number = 42;
my $array_ref = \@colors;
my $hash_ref = \%capital;
my $scalar_ref = \$number;
print "Array ref: $array_ref\n";
print "Hash ref: $hash_ref\n";
print "Scalar ref: $scalar_ref\n";
Program Output

Click Run to see what this code prints.

The exact hexadecimal address will differ every time you run the program — what matters is that Perl labels the reference with its type (ARRAY, HASH, SCALAR), which is how you can tell what kind of data a reference points to.

Dereferencing an Array Reference

To get back to the original data from a reference, you dereference it. The classic syntax wraps the reference in curly braces and prefixes it with the sigil that matches the data type: @{$array_ref} for the whole array, or ${$array_ref}[0] for a single element. Modern Perl also allows a much cleaner arrow syntax: $array_ref->[0].

use strict;
use warnings;
my @colors = ('red', 'green', 'blue');
my $array_ref = \@colors;
print "Whole array: @{$array_ref}\n";
print "First color (arrow syntax): $array_ref->[0]\n";
print "Second color (classic syntax): ${$array_ref}[1]\n";
foreach my $color (@{$array_ref}) {
print "Color: $color\n";
}
Program Output

Click Run to see what this code prints.

Dereferencing a Hash Reference

Hash references follow the same pattern: %{$hash_ref} for the whole hash, or the much more common $hash_ref->{key} arrow syntax to access a single value.

use strict;
use warnings;
my %capital = (France => 'Paris', Japan => 'Tokyo');
my $hash_ref = \%capital;
print "France's capital: $hash_ref->{France}\n";
foreach my $country (keys %{$hash_ref}) {
print "$country -> $hash_ref->{$country}\n";
}
Program Output

Click Run to see what this code prints.

Anonymous References

You do not need a named array or hash before you can reference it. Square brackets [] create an anonymous array reference directly, and curly braces {} create an anonymous hash reference directly — with no separate @array or %hash variable involved at all.

use strict;
use warnings;
my $fruits_ref = ['apple', 'banana', 'cherry'];
my $person_ref = { name => 'Alice', age => 30 };
print "Second fruit: $fruits_ref->[1]\n";
print "Person's name: $person_ref->{name}\n";
Program Output

Click Run to see what this code prints.

Why References Matter

Without references, an array or hash can only hold scalars — you could never put "an array" directly inside another array, because a value in a Perl list is always flattened into individual scalars. A reference to an array, however, IS a single scalar, so it can happily live inside another array or hash. This is precisely how Perl builds nested structures like an array of hashes or a hash of arrays, which the next lesson covers in detail.

use strict;
use warnings;
my @team_a = ('Alice', 'Bob');
my @team_b = ('Carol', 'Dave');
my @all_teams = (\@team_a, \@team_b);
foreach my $team_ref (@all_teams) {
print "Team: @{$team_ref}\n";
}
Program Output

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Forgetting the arrow (->) between chained dereferences, like $data->{list}[0] versus mistakenly writing $data{list}[0].
  • Printing a reference directly and being confused by the ARRAY(0x...) output instead of dereferencing it first.
  • Mixing up when to use @{...} (whole array) versus ->[...] (single element).
  • Storing an array or hash directly inside another array/hash instead of a reference to it, which flattens the data unexpectedly.
  • Assuming a reference is a deep copy — modifying data through a reference changes the original data too, since they point at the same memory.

Best Practices

  • Prefer the arrow syntax ($ref->[0], $ref->{key}) over the classic ${$ref}[0] syntax — it is far more readable, especially when chained.
  • Use anonymous references ([...] and {...}) directly when you do not need a separately named array or hash.
  • Remember that consecutive arrows can be omitted between subscripts: $data->{list}[0] is the same as $data->{list}->[0].
  • Be deliberate about when you want shared references versus independent copies of data.
  • Use Data::Dumper (a core module) to print the structure of complex references while debugging: print Dumper($ref);

Frequently Asked Questions

An array (@array) is the actual list of values. An array reference ($ref = \@array) is a single scalar that points to that array, and can be stored inside other arrays or hashes, unlike the array itself.

Use the built-in ref() function: ref($var) returns "ARRAY", "HASH", "SCALAR", "CODE", or an empty string if it is not a reference at all.

Yes — my $code_ref = \&some_sub; creates a code reference, and you call it with $code_ref->(@args).

No. Square brackets [...] always create an anonymous array reference, and curly braces {...} always create an anonymous hash reference, independent of any parentheses.

Key Takeaways

  • A reference is a scalar that points to another piece of data (array, hash, scalar, or subroutine).
  • The backslash operator (\) creates a reference to an existing variable.
  • Arrow syntax ($ref->[0], $ref->{key}) is the standard, readable way to dereference.
  • Square brackets [...] and curly braces {...} create anonymous array/hash references directly.
  • References are what allow Perl to build nested data structures, since a reference (unlike an array or hash) is itself a single scalar.

Summary

References are the key that unlocks complex, real-world data modeling in Perl. Now that you understand how they work, the next lesson puts them to use building arrays of hashes, hashes of arrays, and other nested structures you will see constantly in production Perl code.

Next Lesson →

Complex Data Structures