How to create a reference to an array in Perl?
Question
How to create a reference to an array in Perl?
In Perl, a reference is a scalar variable that holds the location of another variable or data structure, such as arrays, hashes, or subroutines. Creating a reference to an array allows you to pass arrays around without copying them, manage complex data structures (like arrays of arrays), and use indirection to access or modify data.
Creating a Reference to an Array
To create a reference to an array, simply prefix the array variable with a backslash (\\). This returns a scalar value that points to the original array.
my @array = (1, 2, 3);
my $array_ref = \@array;
Here:
@arrayis the ordinary array variable.$array_refis now a scalar variable containing a reference (pointer) to@array.
Accessing Array Elements via Reference
You can access the elements through the reference in two ways:
- Arrow notation:
$array_ref->[0]accesses the first element. - De-referencing:
@{ $array_ref }returns the array itself.
Example: Creating and Using Array References
The following example demonstrates creating an array reference, accessing elements, pushing new values, and printing the results:
use strict;
use warnings;
# Define an array
my @fruits = ('apple', 'banana', 'cherry');
# Create a reference to the array
my $fruits_ref = \@fruits;
print "Original array elements:\n";
print "First fruit: ", $fruits_ref->[0], "\n"; # Access first element via arrow notation
print "Second fruit: ", $fruits_ref->[1], "\n";
# Push a new element through the reference
push @$fruits_ref, 'date';
print "\nArray elements after push:\n";
print join(", ", @$fruits_ref), "\n"; # De-reference to get the full array
# Modify element through reference
$fruits_ref->[1] = 'blueberry';
print "\nModified second fruit:\n";
print join(", ", @$fruits_ref), "\n";
This script outputs:
Original array elements: First fruit: apple Second fruit: banana Array elements after push: apple, banana, cherry, date Modified second fruit: apple, blueberry, cherry, date
Key Concepts and Perl-Specific Notes
-
Sigils: The
@sigil is for arrays, while$is for scalars (including references). This is why the array reference is stored in a scalar variable ($array_ref). -
Dereferencing: Using
@{ $array_ref }converts the reference back into the array. Similarly, single elements use the arrow and index notation$array_ref->[index]. -
TMTOWTDI (There's More Than One Way To Do It): You can also dereference more verbosely:
my @copy = @{$array_ref};or even using$$array_ref[0](less recommended because it is hard to read). - References vs Copies: The reference points to the original array. Changes through the reference affect the original array, as shown in the example.
- Version Notes: Array references and their syntax have been stable since very early Perl versions (Perl 5.x). No special features or modules are needed.
Common Pitfalls
- Confusing the sigil when dereferencing. For example,
$array_ref[0]is incorrect because$array_refis a scalar reference, not an array. - Modifying an array through a copy instead of the reference (using
@{$array_ref}in list context makes a copy). - Forgetting to use the arrow
->or sigils properly when dereferencing multidimensional arrays.
Creating and using array references is a fundamental Perl skill that unlocks powerful data structure manipulation. Once comfortable with references, you can build complex nested structures and pass large arrays efficiently.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 5ms
(empty)(empty)