Ad space (banner)
Lesson 13 / 20

References

This lesson covers the basics of the reference, which acts rather like a pointer to a variable.

You create a reference with \variable and reach the contents with forms such as $$ref or @$ref. References are the usual way to pass arrays and hashes to a function efficiently.

The sample code creates an array reference — my $ref = \@fruits; for my @fruits = ("apple", "orange"); — and reaches the original array's contents with @$ref.

A common early stumble is keeping the sigils of the reference and the thing it refers to straight. Picturing it as "the mechanism for handling an array or hash without copying it wholesale" helps.

In professional work, references are used constantly to pass large data structures into a function without paying the cost of copying.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Perl directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)