Lesson 33 of 55
10 mins readPython References & Dereferencing Operators
In Plain English
A reference in Perl is a scalar pointing to another variable or data structure, created with the backslash operator `\`. Dereference using the target sigil or arrow operator `->`.
Deep Dive: How It Works
Creating References: `\$scalar`, `\@array`, `\%hash`.
Dereferencing with Sigils: `$$ref` (scalar), `@$ref` (array), `%$ref` (hash).
Arrow Operator (->): `$arr_ref->[0]`, `$hash_ref->{key}`.
Pass by Reference: Prevents costly flattening and copying of large arrays/hashes.
Core Rules to Remember

Backslash (\) Reference: Creates a scalar holding the memory address of a data structure.

Pass by Reference: Essential for passing arrays and hashes to subroutines without flattening.
Live Interactive Example
Hit Run Code to see it liveCreating and Dereferencing Arrays & Hashes
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Output Console
Click "Run Code" to view the rendered output.
How it works: $arr_ref->[1] and $hash_ref->{role} accessed referenced elements directly.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Dereference Array Reference
Declare `my @data = (100, 200, 300);` and `my $ref = \@data;`
Print `"First: " . $ref->[0] . ", Count: " . scalar(@$ref) . "\n"`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.