A Perl reference is a fundamental data type that "points" to another piece of data or code. A reference knows the location of the information and what type of data is stored there.
A reference is a scalar and can be used anywhere a scalar can be used. Any array element or hash value can contain a reference (a hash key cannot contain a reference), and this is how nested data structures are built in Perl. You can construct lists containing references to other lists, which can contain references to hashes, and so on.
You can create a reference to an existing variable or subroutine by prefixing it with a backslash:
References to scalar constants are created similarly:$a = "fondue"; @alist = ("pitt", "hanks", "cage", "cruise"); %song = ("mother" => "crying", "brother" => "dying"); sub freaky_friday { s/mother/daughter/ } # Create references $ra = \$a; $ralist = \@alist; $rsong = \%song; $rsub = \&freaky_friday; # '&' required for subroutine names
Note that all references are prefixed by a$pi = \3.14159; $myname = \"Charlie";
$
, even
if they refer to an array or hash. All references are scalars, thus you
can copy a reference to another scalar or even reference
another reference:
Because arrays and hashes are collections of scalars, you can create references to individual elements by prefixing their names with backslashes:$aref = \@names; $bref = $aref; # both refer to @names $cref = \$aref; # $cref is a reference to $aref
$star = \$alist[2]; # refers to third element of @alist $action = \$song{mother}; # refers to the 'mother' value of %song
It is also possible to take references to literal data not stored in a variable. This data is called anonymous because it is not bound to any named variable.
To create a reference to a scalar constant, simply backslash the literal string or number.
To create a reference to an anonymous array, place the list of values in square brackets:
This creates a reference to an array, but the array is only available through the reference$shortbread = [ "flour", "butter", "eggs", "sugar" ];
$shortbread
. A reference to an anonymous hash uses curly braces around the list of elements:
$cast = { host => "Space Ghost", musician => "Zorak", director => "Moltar" };
Dereferencing returns the value a reference points to. The
general method of dereferencing uses the reference variable
substituted for the regular name part of a variable. If
$r
is a reference, then $$r
, @$r
,
or %$r
retrieve the value being referred to, depending
on whether $r
is pointing to a scalar, array, or hash.
A reference can be used in all the
places where an ordinary data type can be used.
When a reference is accidentally evaluated as a plain scalar, it returns a string that indicates what type of data it points to and the memory address of the data.
If you just want to know which type of data is being referenced,
use ref
, which
returns one of the following strings if
its argument is a reference. Otherwise, it returns false.
SCALAR ARRAY HASH CODE GLOB REF
References to arrays, hashes, and subroutines can be dereferenced using the
->
operator. This operator dereferences the
expression to its left, which must resolve to an array or hash, and accesses
the element represented by the subscripted expression on its right. For
example, these three statement are equivalent:
The first statement dereferences$$arrayref[0] = "man"; ${$arrayref}[0] = "man"; $arrayref->[0] = "man";
$arrayref
first and then finds the
first element of that array. The second uses braces to clarify this procedure.
The third statement uses the arrow notation to do the same thing. The arrow dereferencing notation can only be used to access a single scalar value. You cannot use arrow operators in expressions that return either slices or whole arrays or hashes.