<P>perlref - Perl references and nested data structures</P>
<P>
<HR>
<H1><A NAME="note">NOTE</A></H1>
<P>This is complete documentation about all aspects of references.
For a shorter, tutorial introduction to just the essential features,
see <A HREF="../../lib/Pod/perlreftut.html">the perlreftut manpage</A>.</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>Before release 5 of Perl it was difficult to represent complex data
structures, because all references had to be symbolic--and even then
it was difficult to refer to a variable instead of a symbol table entry.
Perl now not only makes it easier to use symbolic references to variables,
but also lets you have ``hard'' references to any piece of data or code.
Any scalar may hold a hard reference. Because arrays and hashes contain
scalars, you can now easily build arrays of arrays, arrays of hashes,
hashes of arrays, arrays of hashes of functions, and so on.</P>
<P>Hard references are smart--they keep track of reference counts for you,
automatically freeing the thing referred to when its reference count goes
to zero. (Reference counts for values in self-referential or
cyclic data structures may not go to zero without a little help; see
<A HREF="../../lib/Pod/perlobj.html#twophased garbage collection">Two-Phased Garbage Collection in the perlobj manpage</A> for a detailed explanation.)
If that thing happens to be an object, the object is destructed. See
<A HREF="../../lib/Pod/perlobj.html">the perlobj manpage</A> for more about objects. (In a sense, everything in Perl is an
object, but we usually reserve the word for references to objects that
have been officially ``blessed'' into a class package.)</P>
<P>Symbolic references are names of variables or other objects, just as a
symbolic link in a Unix filesystem contains merely the name of a file.
The <CODE>*glob</CODE> notation is something of a of symbolic reference. (Symbolic
references are sometimes called ``soft references'', but please don't call
them that; references are confusing enough without useless synonyms.)</P>
<P>In contrast, hard references are more like hard links in a Unix file
system: They are used to access an underlying object without concern for
what its (other) name is. When the word ``reference'' is used without an
adjective, as in the following paragraph, it is usually talking about a
hard reference.</P>
<P>References are easy to use in Perl. There is just one overriding
principle: Perl does no implicit referencing or dereferencing. When a
scalar is holding a reference, it always behaves as a simple scalar. It
doesn't magically start being an array or hash or subroutine; you have to
tell it explicitly to do so, by dereferencing it.</P>
By using the backslash operator on a variable, subroutine, or value.
(This works much like the & (address-of) operator in C.)
This typically creates <EM>another</EM> reference to a variable, because
there's already a reference to the variable in the symbol table. But
the symbol table reference might go away, and you'll still have the
reference that the backslash returned. Here are some examples:
<PRE>
$scalarref = \$foo;
$arrayref = \@ARGV;
$hashref = \%ENV;
$coderef = \&handler;
$globref = \*foo;</PRE>
<P>It isn't possible to create a true reference to an IO handle (filehandle
or dirhandle) using the backslash operator. The most you can get is a
reference to a typeglob, which is actually a complete symbol table entry.
But see the explanation of the <CODE>*foo{THING}</CODE> syntax below. However,
you can still use type globs and globrefs as though they were IO handles.</P>
<P></P>
<LI>
A reference to an anonymous array can be created using square
brackets:
<PRE>
$arrayref = [1, 2, ['a', 'b', 'c']];</PRE>
<P>Here we've created a reference to an anonymous array of three elements
whose final element is itself a reference to another anonymous array of three
elements. (The multidimensional syntax described later can be used to
access this. For example, after the above, <CODE>$arrayref->[2][1]</CODE> would have
the value ``b''.)</P>
<P>Taking a reference to an enumerated list is not the same
as using square brackets--instead it's the same as creating
a list of references!</P>
<PRE>
@list = (\$a, \@b, \%c);
@list = \($a, @b, %c); # same thing!</PRE>
<P>As a special case, <CODE>\(@foo)</CODE> returns a list of references to the contents
of <CODE>@foo</CODE>, not a reference to <CODE>@foo</CODE> itself. Likewise for <CODE>%foo</CODE>,
except that the key references are to copies (since the keys are just
strings rather than full-fledged scalars).</P>
<P></P>
<LI>
A reference to an anonymous hash can be created using curly
brackets:
<PRE>
$hashref = {
'Adam' => 'Eve',
'Clyde' => 'Bonnie',
};</PRE>
<P>Anonymous hash and array composers like these can be intermixed freely to
produce as complicated a structure as you want. The multidimensional
syntax described below works for these too. The values above are
literals, but variables and expressions would work just as well, because
assignment operators in Perl (even within <A HREF="../../lib/Pod/perlfunc.html#item_local"><CODE>local()</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_my"><CODE>my())</CODE></A> are executable
statements, not compile-time declarations.</P>
<P>Because curly brackets (braces) are used for several other things
including BLOCKs, you may occasionally have to disambiguate braces at the
beginning of a statement by putting a <CODE>+</CODE> or a <A HREF="../../lib/Pod/perlfunc.html#item_return"><CODE>return</CODE></A> in front so
that Perl realizes the opening brace isn't starting a BLOCK. The economy and
mnemonic value of using curlies is deemed worth this occasional extra
hassle.</P>
<P>For example, if you wanted a function to make a new hash and return a
reference to it, you have these options:</P>
<PRE>
sub hashem { { @_ } } # silently wrong
sub hashem { +{ @_ } } # ok
sub hashem { return { @_ } } # ok</PRE>
<P>On the other hand, if you want the other meaning, you can do this:</P>
<PRE>
sub showem { { @_ } } # ambiguous (currently ok, but may change)
sub showem { {; @_ } } # ok
sub showem { { return @_ } } # ok</PRE>
<P>The leading <CODE>+{</CODE> and <CODE>{;</CODE> always serve to disambiguate
the expression to mean either the HASH reference, or the BLOCK.</P>
<P></P>
<LI>
A reference to an anonymous subroutine can be created by using
<A HREF="../../lib/Pod/perlfunc.html#item_sub"><CODE>sub</CODE></A> without a subname:
<PRE>
$coderef = sub { print "Boink!\n" };</PRE>
<P>Note the semicolon. Except for the code
inside not being immediately executed, a <A HREF="../../lib/Pod/perlfunc.html#item_sub"><CODE>sub {}</CODE></A> is not so much a
declaration as it is an operator, like <A HREF="../../lib/Pod/perlfunc.html#item_do"><CODE>do{}</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_eval"><CODE>eval{}</CODE></A>. (However, no
matter how many times you execute that particular line (unless you're in an
<A HREF="../../lib/Pod/perlfunc.html#item_eval"><CODE>eval("...")</CODE></A>), $coderef will still have a reference to the <EM>same</EM>
anonymous subroutine.)</P>
<P>Anonymous subroutines act as closures with respect to <A HREF="../../lib/Pod/perlfunc.html#item_my"><CODE>my()</CODE></A> variables,
that is, variables lexically visible within the current scope. Closure
is a notion out of the Lisp world that says if you define an anonymous
function in a particular lexical context, it pretends to run in that
context even when it's called outside the context.</P>
<P>In human terms, it's a funny way of passing arguments to a subroutine when
you define it as well as when you call it. It's useful for setting up
little bits of code to run later, such as callbacks. You can even
do object-oriented stuff with it, though Perl already provides a different
mechanism to do that--see <A HREF="../../lib/Pod/perlobj.html">the perlobj manpage</A>.</P>
<P>You might also think of closure as a way to write a subroutine
template without using eval(). Here's a small example of how
closures work:</P>
<PRE>
sub newprint {
my $x = shift;
return sub { my $y = shift; print "$x, $y!\n"; };
}
$h = newprint("Howdy");
$g = newprint("Greetings");</PRE>
<PRE>
# Time passes...</PRE>
<PRE>
&$h("world");
&$g("earthlings");</PRE>
<P>This prints</P>
<PRE>
Howdy, world!
Greetings, earthlings!</PRE>
<P>Note particularly that $x continues to refer to the value passed
into <CODE>newprint()</CODE> <EM>despite</EM> ``my $x'' having gone out of scope by the
time the anonymous subroutine runs. That's what a closure is all
about.</P>
<P>This applies only to lexical variables, by the way. Dynamic variables
continue to work as they have always worked. Closure is not something
that most Perl programmers need trouble themselves about to begin with.</P>
<P></P>
<LI>
References are often returned by special subroutines called constructors.
Perl objects are just references to a special type of object that happens to know
which package it's associated with. Constructors are just special
subroutines that know how to create that association. They do so by
starting with an ordinary reference, and it remains an ordinary reference
even while it's also being an object. Constructors are often
named <CODE>new()</CODE> and called indirectly:
<PRE>
$objref = new Doggie (Tail => 'short', Ears => 'long');</PRE>
References of the appropriate type can spring into existence if you
dereference them in a context that assumes they exist. Because we haven't
talked about dereferencing yet, we can't show you any examples yet.
<P></P>
<LI>
A reference can be created by using a special syntax, lovingly known as
the *foo{THING} syntax. *foo{THING} returns a reference to the THING
slot in *foo (which is the symbol table entry which holds everything
known as foo).
<PRE>
$scalarref = *foo{SCALAR};
$arrayref = *ARGV{ARRAY};
$hashref = *ENV{HASH};
$coderef = *handler{CODE};
$ioref = *STDIN{IO};
$globref = *foo{GLOB};</PRE>
<P>All of these are self-explanatory except for <CODE>*foo{IO}</CODE>. It returns
the IO handle, used for file handles (<A HREF="../../lib/Pod/perlfunc.html#open">open in the perlfunc manpage</A>), sockets
(<A HREF="../../lib/Pod/perlfunc.html#socket">socket in the perlfunc manpage</A> and <A HREF="../../lib/Pod/perlfunc.html#socketpair">socketpair in the perlfunc manpage</A>), and directory
handles (<A HREF="../../lib/Pod/perlfunc.html#opendir">opendir in the perlfunc manpage</A>). For compatibility with previous
versions of Perl, <CODE>*foo{FILEHANDLE}</CODE> is a synonym for <CODE>*foo{IO}</CODE>.</P>
<P><CODE>*foo{THING}</CODE> returns undef if that particular THING hasn't been used yet,
except in the case of scalars. <CODE>*foo{SCALAR}</CODE> returns a reference to an
anonymous scalar if $foo hasn't been used yet. This might change in a
future release.</P>
<P><CODE>*foo{IO}</CODE> is an alternative to the <CODE>*HANDLE</CODE> mechanism given in
<A HREF="../../lib/Pod/perldata.html#typeglobs and filehandles">Typeglobs and Filehandles in the perldata manpage</A> for passing filehandles
into or out of subroutines, or storing into larger data structures.
Its disadvantage is that it won't create a new filehandle for you.
Its advantage is that you have less risk of clobbering more than
you want to with a typeglob assignment. (It still conflates file
and directory handles, though.) However, if you assign the incoming
value to a scalar instead of a typeglob as we do in the examples
below, there's no risk of that happening.</P>
<PRE>
splutter(*STDOUT); # pass the whole glob
splutter(*STDOUT{IO}); # pass both file and dir handles</PRE>
<PRE>
sub splutter {
my $fh = shift;
print $fh "her um well a hmmm\n";
}</PRE>
<PRE>
$rec = get_rec(*STDIN); # pass the whole glob
$rec = get_rec(*STDIN{IO}); # pass both file and dir handles</PRE>
<P>As explained above, a closure is an anonymous function with access to the
lexical variables visible when that function was compiled. It retains
access to those variables even though it doesn't get run until later,
such as in a signal handler or a Tk callback.</P>
<P>Using a closure as a function template allows us to generate many functions
that act similarly. Suppose you wanted functions named after the colors
that generated HTML font changes for the various colors:</P>
<PRE>
print "Be ", red("careful"), "with that ", green("light");</PRE>
<P>The <CODE>red()</CODE> and <CODE>green()</CODE> functions would be similar. To create these,
we'll assign a closure to a typeglob of the name of the function we're
trying to build.</P>
<PRE>
@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs'; # allow symbol table manipulation
*$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };
}</PRE>
<P>Now all those different functions appear to exist independently. You can
call red(), RED(), blue(), BLUE(), green(), etc. This technique saves on
both compile time and memory use, and is less error-prone as well, since
syntax checks happen at compile time. It's critical that any variables in
the anonymous subroutine be lexicals in order to create a proper closure.
That's the reasons for the <A HREF="../../lib/Pod/perlfunc.html#item_my"><CODE>my</CODE></A> on the loop iteration variable.</P>
<P>This is one of the only places where giving a prototype to a closure makes
much sense. If you wanted to impose scalar context on the arguments of
these functions (probably not a wise idea for this particular example),
you could have written it this way instead:</P>
<PRE>
*$name = sub ($) { "<FONT COLOR='$name'>$_[0]</FONT>" };</PRE>
<P>However, since prototype checking happens at compile time, the assignment
above happens too late to be of much use. You could address this by
putting the whole loop of assignments within a BEGIN block, forcing it
to occur during compilation.</P>
<P>Access to lexicals that change over type--like those in the <CODE>for</CODE> loop
above--only works with closures, not general subroutines. In the general
case, then, named subroutines do not nest properly, although anonymous
ones do. If you are accustomed to using nested subroutines in other
programming languages with their own private variables, you'll have to
work at it a bit in Perl. The intuitive coding of this type of thing
incurs mysterious warnings about ``will not stay shared''. For example,
this won't work:</P>
<PRE>
sub outer {
my $x = $_[0] + 35;
sub inner { return $x * 19 } # WRONG
return $x + inner();
}</PRE>
<P>A work-around is the following:</P>
<PRE>
sub outer {
my $x = $_[0] + 35;
local *inner = sub { return $x * 19 };
return $x + inner();
}</PRE>
<P>Now <CODE>inner()</CODE> can only be called from within outer(), because of the
temporary assignments of the closure (anonymous subroutine). But when
it does, it has normal access to the lexical variable $x from the scope
of outer().</P>
<P>This has the interesting effect of creating a function local to another
function, something not normally supported in Perl.</P>
<P>
<HR>
<H1><A NAME="warning">WARNING</A></H1>
<P>You may not (usefully) use a reference as the key to a hash. It will be
converted into a string:</P>
<PRE>
$x{ \$a } = $a;</PRE>
<P>If you try to dereference the key, it won't do a hard dereference, and
you won't accomplish what you're attempting. You might want to do something
more like</P>
<PRE>
$r = \@a;
$x{ $r } = $r;</PRE>
<P>And then at least you can use the values(), which will be
real refs, instead of the keys(), which won't.</P>
<P>The standard Tie::RefHash module provides a convenient workaround to this.</P>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P>Besides the obvious documents, source code can be instructive.
Some pathological examples of the use of references can be found
in the <EM>t/op/ref.t</EM> regression test in the Perl source directory.</P>
<P>See also <A HREF="../../lib/Pod/perldsc.html">the perldsc manpage</A> and <A HREF="../../lib/Pod/perllol.html">the perllol manpage</A> for how to use references to create
complex data structures, and <A HREF="../../lib/Pod/perltoot.html">the perltoot manpage</A>, <A HREF="../../lib/Pod/perlobj.html">the perlobj manpage</A>, and <A HREF="../../lib/Pod/perlbot.html">the perlbot manpage</A>