A reference is a scalar value. It can be stored in a scalar variable, or as an element of an array or hash, as is done with numbers and strings. You can think of a reference as a pointer to some other object in Perl. References can point to any kind of object, including other scalars (even references), arrays, hashes, subroutines, and typeglobs. Aside from a general pointer-like behavior, however, references do not have very much in common with pointers in C or C++. You can only create references to existing objects; you cannot modify them afterward to do something like point to the next element of an array. You can convert references into strings or numbers, but you cannot convert a string or number back into a reference. Although a reference is treated syntactically like any other scalar value, a reference " knows " to what type of object it points. Finally, each reference to a Perl object increments that object's reference count, preventing the object from being scavenged by Perl's garbage collector. Creating referencesReferences can be created in several different ways. The simplest is to use the reference operator \ on a variable:
The effect of the reference operator is to create a reference pointing at the value of its argument:
The reference operator works on any kind of variable:
It also works on array and hash elements, and values:
The reference operator works in a very strange way on a list of values, returning a list of references rather than a reference to a list. It decides what to return references to by using a seemingly arbitrary heuristic: These make a certain amount of sense.
But these are a little weird.
You can understand why I recommend that you avoid using the reference operator in front of lists. The anonymous array constructor [ ] , which looks like an ordinary list except that the contents are enclosed by brackets rather than parentheses, creates an unnamed array in memory and returns a reference to it. The anonymous array constructor is the customary method of creating a reference to a list of items:
The anonymous hash constructor { } , which uses braces rather than brackets, works similarly:
There are many uses for both anonymous arrays and anonymous hashes. See Items Item 32 and Item 33 for more examples. A sub definition without a name returns a reference to an anonymous subroutine. References to subroutines are also called code refs :
References to anonymous subroutines are very useful. They are somewhat like function pointers in C. On the other hand, since anonymous subrou-tines are created dynamically, not statically, they have peculiar properties that are more like something from LISP (see Item 29). There isn't often much need to construct a reference to an anonymous scalar value, but you can do something like the following if need be:
This works through "auto-vivification," discussed later in this Item. Finally, and somewhat mysteriously, you can create references to an undocumented LVALUE type (not exactly the meaning of "lvalue" I give in the Introduction):
Using referencesUsing the value that a reference points to is called dereferencing . There are several different forms of dereferencing syntax. The "canonical" form of dereferencing syntax is to use a block returning a reference in a place where you could otherwise use a variable or subroutine identifier. Whereas using an identifier would give you the value of the variable with that name, using a block that returns a reference gives you the value to which the reference points: Canonical syntax for scalar references.
Canonical syntax for array references.
The code inside the block can be arbitrarily complex, so long as the result of the last expression evaluated yields a reference:
If the reference value is contained in a scalar variable, you can dispense with the braces and just use the name of the scalar variable, with the leading $ , instead. You can use more than one $ if it's a reference to a reference: Scalar variable syntax for references.
Expressions like $$h_ref{'F'} , or the even more awkward equivalent ${$h_ref}{'F'} , occur frequently. There is a more visually appealing " arrow" syntax that you can use to write subscripts for array and hash references:
The arrow syntax also works on code refs:
You can cascade arrows. Furthermore, if the left and right sides of an arrow are both subscripts, you can omit the arrow:
The data structure in this example looks something like this:
Be careful about leaving out too many arrows or braces. For example, if you omit the first arrow, you get an array of hash references, which is different:
There are more examples of complex data structures built from references in the other items in this section. Finally, as noted earlier, all references, no matter what their type, are handled like ordinary scalarsthey have no special "type" that distinguishes them syntactically from other scalars. [1] However, a reference value contains information about the type of object to which it points. You can get to this information with the ref operator:
The ref operator works differently on blessed objects (see Item 49). Auto-vivificationIf you use a scalar lvalue with an undefined value as if it were a reference to another object, Perl will automatically create an object of the appropriate type for you and make that scalar a reference to that object. This is called auto-vivification . For example, the following code creates an array of four elements and makes $ref a reference to it:
A longer example of auto-vivification is discussed in Item 31. Soft referencesIf you dereference a string value, Perl will return the value of the variable with the name given in the string. The variable will be created if necessary. This is called a soft reference .
Such a variable name does not have to be a legal identifier:
Note that soft references have nothing to do with reference counts (see Item 34). Only ordinary "hard" references increment reference counts. Turning on strict refs disables soft references (see Item 36)this is often a good idea. |