ZetCode

Perl references

last modified October 8, 2026

In this article we work with references in Perl. A reference is a scalar value that points to another value, such as a scalar, an array, a hash, or a subroutine.

References let us pass large data structures to functions cheaply, build nested data structures, and create data on the fly with anonymous arrays ([ ]) and anonymous hashes ({ }).

Backslash operator and scalar references

The backslash operator (\) creates a reference to a variable. A scalar reference is dereferenced with ${$ref}.

scalar.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my $age = 35;
my $ref = \$age;

say "Value via reference: ${$ref}";

${$ref} = 36;
say "Original variable now: $age";

We create a reference to the $age variable. Changing the value through the reference changes the original variable, because both names point to the same memory.

$ ./scalar.pl
Value via reference: 35
Original variable now: 36

Array references

An array reference is created with the backslash operator applied to an array. Elements are accessed with the arrow syntax (->), and the whole array is dereferenced with @{$ref}.

array.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my @langs = ('Perl', 'Python', 'Go');
my $aref = \@langs;

say "First element: $aref->[0]";
say "Second element: ${$aref}[1]";
say "All elements: @{$aref}";
say "Last index: $#{$aref}";

push @{$aref}, 'Rust';
say "Count: ", scalar @langs;

The arrow syntax and the ${$aref}[1] form are two ways to access an element through a reference. $#{$aref} gives the last index of the array. Since the reference points to @langs, pushing onto @{$aref} adds an element to the original array.

push @{$aref}, 'Rust';
$ ./array.pl
First element: Perl
Second element: Python
All elements: Perl Python Go
Last index: 2
Count: 4

Hash references

A hash reference is created with the backslash operator applied to a hash. Values are accessed with $href->{key}, and the whole hash is dereferenced with %{$href}.

hash.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my %city = (name => 'Bratislava', country => 'Slovakia');
my $href = \%city;

say "City: $href->{name}";
say "Country: ${$href}{country}";

for my $key (sort keys %{$href}) {

    say "$key => $href->{$key}";
}

We create a hash reference and read its values. The keys function works on the dereferenced hash %{$href}; we sort the keys to get a stable output order.

$ ./hash.pl
City: Bratislava
Country: Slovakia
country => Slovakia
name => Bratislava

Nested data structures

Anonymous arrays and hashes can be nested inside each other. The arrow syntax can be omitted between subscripts, so $person->{languages}[1] is the same as $person->{languages}->[1].

nested.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my $person = {
    name => 'Jan',
    languages => ['Perl', 'Python'],
    address => { city => 'Bratislava', zip => '81101' },
};

say "Name: $person->{name}";
say "Second language: $person->{languages}[1]";
say "City: $person->{address}{city}";

say "Number of languages: ", scalar @{ $person->{languages} };

The $person variable is a hash reference. Its languages value is an anonymous array reference, and its address value is an anonymous hash reference. The { } braces create a hash and the [ ] brackets create an array.

$ ./nested.pl
Name: Jan
Second language: Python
City: Bratislava
Number of languages: 2

Code references

A code reference points to a subroutine. It is created with \&name for a named subroutine, or with sub { ... } for an anonymous one. It is called with $cref->(...) or &$cref(...).

code.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

sub greet {
    my ($name) = @_;
    return "Hello, $name!";
}

my $cref = \&greet;
say $cref->('Alice');
say &$cref('Bob');

my $square = sub {
    my ($n) = @_;
    return $n * $n;
};

say "Square of 7: ", $square->(7);

We store a reference to the named greet subroutine and call it in two equivalent ways. The anonymous subroutine stored in $square is called through its reference in the same manner.

$ ./code.pl
Hello, Alice!
Hello, Bob!
Square of 7: 49

The ref function

The built-in ref function returns the type of the value a reference points to. It returns an empty string when its argument is not a reference.

ref.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my $num = 10;
my @arr = (1, 2, 3);
my %h = (a => 1);

say "ref(\\\$num): ", ref(\$num);
say "ref(\\\@arr): ", ref(\@arr);
say "ref(\\\%h): ", ref(\%h);
say "ref(sub {}): ", ref(sub { 1 });
say "ref(\\\\\$num): ", ref(\\$num);
say "ref('text'): '", ref('text'), "'";

The ref function reports SCALAR, ARRAY, HASH, CODE, and REF for a reference to a reference. A plain string gives an empty result.

$ ./ref.pl
ref(\$num): SCALAR
ref(\@arr): ARRAY
ref(\%h): HASH
ref(sub {}): CODE
ref(\\$num): REF
ref('text'): ''

Array of hashes

A common structure is an array of hashes, where each element is a hash reference describing one record. We iterate over it with a for loop.

aoh.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my @books = (
    { title => 'Learning Perl', year => 2011 },
    { title => 'Modern Perl', year => 2016 },
    { title => 'Intermediate Perl', year => 2019 },
);

for my $book (@books) {

    say "$book->{title} ($book->{year})";
}

say "Total books: ", scalar @books;

Each loop variable $book holds a hash reference, so we access its fields with the arrow syntax.

$ ./aoh.pl
Learning Perl (2011)
Modern Perl (2016)
Intermediate Perl (2019)
Total books: 3

Copying vs referencing

Assigning an array to another array creates an independent copy. Assigning a reference only creates a second name for the same data, so changes made through one name are visible through the other.

copy.pl
#!/usr/bin/perl

use v5.34.0;
use warnings;

my @orig = (1, 2, 3);

my @copy = @orig;
my $ref = \@orig;

$orig[0] = 100;
$ref->[1] = 200;

say "orig: @orig";
say "copy: @copy";
say "ref:  @{$ref}";
say "same array? ", ($ref == \@orig ? 'yes' : 'no');

The @copy array keeps the original values, because it was filled with a copy of the elements. The $ref variable points to @orig, so the change through $ref->[1] is visible in @orig. Comparing two references with == checks whether they point to the same thing.

$ ./copy.pl
orig: 100 200 3
copy: 1 2 3
ref:  100 200 3
same array? yes

Autovivification is a related feature. When we use an undefined variable as a reference, Perl creates the structure automatically. For example, reading $h{a}{b} from an empty hash also creates $h{a}, and push @$r, 1 turns an undefined $r into an array reference. To avoid creating entries by accident, check with exists or test the reference before using it.

In this article we have covered references in Perl, including scalar, array, hash, and code references, nested data structures, and the ref function.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.