Perl Tutorial

References

Understanding pointers, memory addresses, and nested data structures in Perl

What is a Reference?

In Perl, a Reference is a scalar value that "points" to another variable (a scalar, an array, or a hash). This is essential for building complex data structures like "an array of hashes" or "a hash of arrays."
You create a reference by putting a \ (backslash) before a variable.
perl
my @fruits = ("Apple", "Banana");
my $ref = \@fruits; # $ref now points to @fruits

Dereferencing

Wait! To get the data back out of a reference, you must dereference it by using the sigil of the target type.
perl
my @copy = @$ref; # Get the whole array
print $$ref[0];  # Get the first item ("Apple")

The Arrow Operator (->)

The cleanest way to access data through a reference is using the -> operator.
perl
print $ref->[0]; # Much cleaner than $$ref[0]

Nested Data Structures

References are the only way to nest collections in Perl.
perl
# A Hash of Arrays
my %users = (
    "Alpha" => ["Admin", "Editor"],
    "Beta"  => ["User"],
);

print $users{"Alpha"}->[0]; # "Admin"

Summary

TermDescription
**The reference operator (Address-of)
$Sigil for a reference (because it's a scalar)
->The arrow operator for dereferencing
[ ]Square brackets create an anonymous array reference
{ }Curly braces create an anonymous hash reference
Best ForPassing data to subroutines and building complex models
Logic"Pointers for data structures"
SafetyPerl handles memory management (garbage collection) automatically
ModernModern Perl code uses references for almost all structured data
StandardUse -> for all reference lookups
IdentityReferences allow you to share the same data in multiple places