home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


4.8 References and Complex Data Structures

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.

4.8.1 Creating References

You can create a reference to an existing variable or subroutine by prefixing it with a backslash:

$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
References to scalar constants are created similarly:
$pi = \3.14159;
$myname = \"Charlie";
Note that all references are prefixed by a $ , 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:
$aref = \@names;
$bref = $aref;  # both refer to @names
$cref = \$aref; # $cref is a reference to $aref
Because arrays and hashes are collections of scalars, you can create references to individual elements by prefixing their names with backslashes:
$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 data

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:

$shortbread = [ "flour", "butter", "eggs", "sugar" ];
This creates a reference to an array, but the array is only available through the reference $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 $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

4.8.2.1 Arrow dereferencing

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:

$$arrayref[0] = "man";
${$arrayref}[0] = "man";
$arrayref->[0] = "man";
The first statement dereferences $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.


Previous: 4.7 Subroutines Perl in a Nutshell Next: 4.9 Filehandles
4.7 Subroutines Book Index 4.9 Filehandles

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.