References
Take a reference
A reference is a scalar that points at another piece of data. Take one with a backslash: a reference to an array, a hash, or a single scalar is still just one value, so it fits in a scalar variable.
my @langs = qw(perl ruby); my %user = (name => "Ada"); my $aref = \@langs; my $href = \%user;
The backslash is what turns a list or a hash into one scalar. Without it, assigning @langs to a scalar would give a count, not the data.
Dereference
To read through a reference, put the sigil in front of it. A scalar reference takes a dollar, an array reference takes an at sign, and a hash reference takes a percent sign.
my @langs = qw(perl ruby); my %user = (name => "Ada"); my ($aref, $href) = (\@langs, \%user); print @$aref, "\n"; # perlruby, print adds no separator print $aref->[0], "\n"; # perl print $href->{name}, "\n"; # Ada
perl demo.pl
perlruby
perl
Ada
The arrow form works for one element and is the one you will read most often. Braces around the reference are also allowed and help when the expression is not a plain name: @{$aref}.
Anonymous structures
Square brackets build an unnamed array and return its reference; curly braces do the same for a hash. This is how nested data starts, because the reference is a scalar and can sit inside another structure.
my $aref2 = [1, 2, 3]; my $href2 = { name => "Ada", age => 36 }; print $aref2->[2], " ", $href2->{age}, "\n"; # 3 36
perl demo.pl
3 36
The brackets and braces here are not the same as the parentheses of a list. A list in parentheses is still a list; only the bracket forms produce a reference.
Ask what it is
ref tells you what a reference points at, and returns an empty string for a plain value. It is the standard guard before dereferencing something that came from outside your code.
my $aref2 = [1, 2, 3]; my $href2 = { name => "Ada", age => 36 }; print ref $aref2, "\n"; # ARRAY print ref $href2, "\n"; # HASH print ref "text", "\n"; # empty line print "---\n"; # marker, so the empty line is visible
perl demo.pl
ARRAY
HASH
---
Pass big data cheaply
Arguments in @_ are aliases, so passing an array is not as expensive as it looks. But returning a big list copies it, and storing two copies becomes a real cost; a reference moves one pointer instead.
my $aref2 = [1, 2, 3]; sub count_items { my ($list) = @_; return scalar @$list; } print count_items($aref2), "\n"; # 3
perl demo.pl
3
References also let a subroutine change the caller data on purpose, for example to fill a results hash that the caller owns. Lesson 4.4 builds the nested shapes that make this technique worth the syntax.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How do you take a reference to an array
@langs?Answer
$aref = \@langs;The backslash turns the array into one scalar; without it the assignment gives the element count.
-
What does
$href->{name}read?Answer
The value of the key name in the hash the reference points at
The arrow dereferences one level and then looks up the key, which is the form you read most often.
-
What does
[1, 2, 3]produce?Answer
A reference to an anonymous array of three numbers
Square brackets build an unnamed array and hand back its reference; parentheses would be a plain list.
-
Do the parentheses of
(1, 2, 3)create a reference?Answer
No: parentheses group, and only brackets and braces produce a reference
A parenthesised list is still a list, so assigning it to a scalar gives a count or the last value, never a reference.
-
What does
ref $xreturn when$xholds a plain string?Answer
The empty string
A plain value has no type to report, so the answer is an empty string, which is false in a test.
-
Why pass a big array to a subroutine by reference rather than as a list?
Answer
A reference is faster because it moves one pointer instead of copying the values
Returning or storing a big list copies it; a reference is one scalar that points at the same data.
-
What does this program print?
my @a = qw(x y); my $r = \@a; print $r->[1], "\n";Answer
y
Index 1 is the second element, and the arrow reaches it through the reference.
-
What does this program print?
my $h = { name => "Ada", age => 36 }; print $h->{age}, "\n";Answer
36
Braces build an anonymous hash and the arrow reads the age key from it.
-
What does this program print?
my $r = [1, 2, 3]; print scalar @$r, "\n";Answer
3
@$rdereferences to the array, andscalarasks it for the element count. -
What does this program print?
print ref([1]), "\n";Answer
ARRAY
refnames the type of the reference, and square brackets make an array reference.
Article author: Arthur Isaev