4.8 References and Complex Data StructuresA 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. 4.8.1 Creating ReferencesYou 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 4.8.1.1 Referencing anonymous dataIt 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" }; 4.8.2 Dereferencing
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
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 SCALAR ARRAY HASH CODE GLOB REF 4.8.2.1 Arrow dereferencing
References to arrays, hashes, and subroutines can be dereferenced using the
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. |
|