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
Dereferencing
Wait! To get the data back out of a reference, you must dereference it by using the sigil of the target type.
perl
The Arrow Operator (->)
The cleanest way to access data through a reference is using the
-> operator.perl
Nested Data Structures
References are the only way to nest collections in Perl.
perl
Summary
| Term | Description |
|---|---|
| ** | 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 For | Passing data to subroutines and building complex models |
| Logic | "Pointers for data structures" |
| Safety | Perl handles memory management (garbage collection) automatically |
| Modern | Modern Perl code uses references for almost all structured data |
| Standard | Use -> for all reference lookups |
| Identity | References allow you to share the same data in multiple places |