4.2 Creating Hard ReferencesThere are several ways to compose references, most of which we will describe before explaining how to use (dereference) the resulting references. 4.2.1 The Backslash Operator
You can create a reference to any named variable or subroutine by using
the unary backslash operator. (You may also use it on an anonymous
scalar value.) This works much like the Here are some examples:
$scalarref = \$foo; $constref = \186_282.42; $arrayref = \@ARGV; $hashref = \%ENV; $code_ref = \&handler; $globref = \*STDOUT; 4.2.2 The Anonymous Array ComposerYou can create a reference to an anonymous array by using brackets:
$arrayref = [1, 2, ['a', 'b', 'c']]; Here we've composed a reference to an anonymous array of three elements whose final element is a reference to another anonymous array of three elements. These square brackets work like this only where the Perl parser is expecting a term in an expression, and should not be confused with the brackets that are functioning as operators when used to subscript an array (though there is an obvious mnemonic association with arrays). Square brackets inside a quoted string do not result in the interpolation of a reference to an anonymous array. Rather, such brackets become literal elements in the string. (However, if you're interpolating something into the string, and the expression defining the interpolation contains brackets, they have their normal meaning within the expression, since they are, after all, in an expression.) Note that taking a reference to an enumerated list is not the same as using brackets - instead it's treated as a shorthand for creating a list of references:
@list = (\$a, \$b, \$c); @list = \($a, $b, $c); # same thing! 4.2.3 The Anonymous Hash ComposerYou can create a reference to an anonymous hash by using braces:
$hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', }; The values above are literal strings; variables and expressions would work as well. Also, for the values (but not the keys) of the hash, you can freely mix anonymous hash and array composers to produce as complicated a structure as you want. These braces work like this only where the Perl parser is expecting a term in an expression, and should not be confused with the braces that are functioning as operators when used to subscript a hash (though there is an obvious mnemonic association with hashes). Braces inside a quoted string do not result in the interpolation of a reference to an anonymous hash. Rather, such braces become literal elements in the string. (However, the same caveat about interpolating expressions applies to braces as it does to brackets.)
Since braces are also used for several other things including
sub hashem { { @_ } } # silently WRONG sub hashem { +{ @_ } } # ok sub hashem { return { @_ } } # ok 4.2.4 The Anonymous Subroutine ComposerYou can create a reference to an anonymous subroutine by using sub without a subroutine name:
$coderef = sub { print "Boink!\n" };
Note the presence of the semicolon, which is required here to
terminate the expression. (It wouldn't be required after the
declaration of a named subroutine.) A nameless
4.2.5 Object Constructors
Subroutines can also return references. That may sound trite, but
sometimes you are
supposed
to use a subroutine to create a
reference rather than creating the reference yourself. In particular,
special subroutines called
constructors
return references to objects.
An object is simply a special kind of thingy that happens to know which
class it's associated with. Constructors know how to create that
association. They do so by taking an ordinary thingy and turning it
into an object (which remains a thingy even while it's also being an
object). The operator that a constructor uses to do this is called
bless
, so we can speak of an object as a blessed thingy.
Constructors are
customarily named
$objref = new Doggie Tail => 'short', Ears => 'long'; # same as $objref = Doggie->new(Tail => 'short', Ears => 'long'); See Chapter 5, Packages, Modules, and Object Classes , for a discussion of Perl objects. 4.2.6 Filehandle ReferencersReferences to filehandles can be created by taking a reference to a typeglob. This is currently the best way to pass named filehandles into or out of subroutines, or to store them in larger data structures.
splutter(\*STDOUT); sub splutter { my $fh = shift; print $fh "her um well a hmmm\n"; } $rec = get_rec(\*STDIN); sub get_rec { my $fh = shift; return scalar <$fh>; } However, if you don't need to refer to existing named filehandles, you should consider using one of the newer, object-oriented library modules that provide filehandle objects via a constructor (see the previous section). In either case, you won't use filehandle names directly, but rather you'll use scalars (as above) to hold a reference to something that will (one way or another) be interpreted as a filehandle. As we admitted earlier, there is some implicit dereferencing magic going on here. 4.2.7 Implicit Creation of ReferencesA final method for creating references is not really a method at all. References of an appropriate type simply spring into existence if you dereference them in a context that assumes they exist. This is extremely useful, and is also What You Expect. This topic is covered in the next section. |
|