README.vms and related updates (from Peter Prymmer <pvhp@best.com>)
[p5sagit/p5-mst-13.2.git] / pod / perlref.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3perlref - Perl references and nested data structures
4
a1e2a320 5=head1 NOTE
6
7This is complete documentation about all aspects of references.
8For a shorter, tutorial introduction to just the essential features,
9see L<perlreftut>.
10
a0d0e21e 11=head1 DESCRIPTION
12
cb1a09d0 13Before release 5 of Perl it was difficult to represent complex data
5a964f20 14structures, because all references had to be symbolic--and even then
15it was difficult to refer to a variable instead of a symbol table entry.
16Perl now not only makes it easier to use symbolic references to variables,
17but also lets you have "hard" references to any piece of data or code.
18Any scalar may hold a hard reference. Because arrays and hashes contain
19scalars, you can now easily build arrays of arrays, arrays of hashes,
20hashes of arrays, arrays of hashes of functions, and so on.
a0d0e21e 21
22Hard references are smart--they keep track of reference counts for you,
2d24ed35 23automatically freeing the thing referred to when its reference count goes
7c2ea1c7 24to zero. (Reference counts for values in self-referential or
2d24ed35 25cyclic data structures may not go to zero without a little help; see
7b8d334a 26L<perlobj/"Two-Phased Garbage Collection"> for a detailed explanation.)
2d24ed35 27If that thing happens to be an object, the object is destructed. See
28L<perlobj> for more about objects. (In a sense, everything in Perl is an
29object, but we usually reserve the word for references to objects that
30have been officially "blessed" into a class package.)
31
32Symbolic references are names of variables or other objects, just as a
54310121 33symbolic link in a Unix filesystem contains merely the name of a file.
7c2ea1c7 34The C<*glob> notation is something of a of symbolic reference. (Symbolic
2d24ed35 35references are sometimes called "soft references", but please don't call
36them that; references are confusing enough without useless synonyms.)
37
54310121 38In contrast, hard references are more like hard links in a Unix file
2d24ed35 39system: They are used to access an underlying object without concern for
40what its (other) name is. When the word "reference" is used without an
5a964f20 41adjective, as in the following paragraph, it is usually talking about a
2d24ed35 42hard reference.
43
44References are easy to use in Perl. There is just one overriding
45principle: Perl does no implicit referencing or dereferencing. When a
46scalar is holding a reference, it always behaves as a simple scalar. It
47doesn't magically start being an array or hash or subroutine; you have to
48tell it explicitly to do so, by dereferencing it.
a0d0e21e 49
5a964f20 50=head2 Making References
51
52References can be created in several ways.
a0d0e21e 53
54=over 4
55
56=item 1.
57
58By using the backslash operator on a variable, subroutine, or value.
7c2ea1c7 59(This works much like the & (address-of) operator in C.)
60This typically creates I<another> reference to a variable, because
a0d0e21e 61there's already a reference to the variable in the symbol table. But
62the symbol table reference might go away, and you'll still have the
63reference that the backslash returned. Here are some examples:
64
65 $scalarref = \$foo;
66 $arrayref = \@ARGV;
67 $hashref = \%ENV;
68 $coderef = \&handler;
55497cff 69 $globref = \*foo;
cb1a09d0 70
5a964f20 71It isn't possible to create a true reference to an IO handle (filehandle
72or dirhandle) using the backslash operator. The most you can get is a
73reference to a typeglob, which is actually a complete symbol table entry.
74But see the explanation of the C<*foo{THING}> syntax below. However,
75you can still use type globs and globrefs as though they were IO handles.
a0d0e21e 76
77=item 2.
78
5a964f20 79A reference to an anonymous array can be created using square
a0d0e21e 80brackets:
81
82 $arrayref = [1, 2, ['a', 'b', 'c']];
83
5a964f20 84Here we've created a reference to an anonymous array of three elements
54310121 85whose final element is itself a reference to another anonymous array of three
a0d0e21e 86elements. (The multidimensional syntax described later can be used to
184e9718 87access this. For example, after the above, C<$arrayref-E<gt>[2][1]> would have
a0d0e21e 88the value "b".)
89
7c2ea1c7 90Taking a reference to an enumerated list is not the same
cb1a09d0 91as using square brackets--instead it's the same as creating
92a list of references!
93
54310121 94 @list = (\$a, \@b, \%c);
58e0a6ae 95 @list = \($a, @b, %c); # same thing!
96
54310121 97As a special case, C<\(@foo)> returns a list of references to the contents
b6429b1b 98of C<@foo>, not a reference to C<@foo> itself. Likewise for C<%foo>,
99except that the key references are to copies (since the keys are just
100strings rather than full-fledged scalars).
cb1a09d0 101
a0d0e21e 102=item 3.
103
5a964f20 104A reference to an anonymous hash can be created using curly
a0d0e21e 105brackets:
106
107 $hashref = {
108 'Adam' => 'Eve',
109 'Clyde' => 'Bonnie',
110 };
111
5a964f20 112Anonymous hash and array composers like these can be intermixed freely to
a0d0e21e 113produce as complicated a structure as you want. The multidimensional
114syntax described below works for these too. The values above are
115literals, but variables and expressions would work just as well, because
116assignment operators in Perl (even within local() or my()) are executable
117statements, not compile-time declarations.
118
119Because curly brackets (braces) are used for several other things
120including BLOCKs, you may occasionally have to disambiguate braces at the
121beginning of a statement by putting a C<+> or a C<return> in front so
122that Perl realizes the opening brace isn't starting a BLOCK. The economy and
123mnemonic value of using curlies is deemed worth this occasional extra
124hassle.
125
126For example, if you wanted a function to make a new hash and return a
127reference to it, you have these options:
128
129 sub hashem { { @_ } } # silently wrong
130 sub hashem { +{ @_ } } # ok
131 sub hashem { return { @_ } } # ok
132
ebc58f1a 133On the other hand, if you want the other meaning, you can do this:
134
135 sub showem { { @_ } } # ambiguous (currently ok, but may change)
136 sub showem { {; @_ } } # ok
137 sub showem { { return @_ } } # ok
138
7c2ea1c7 139The leading C<+{> and C<{;> always serve to disambiguate
ebc58f1a 140the expression to mean either the HASH reference, or the BLOCK.
141
a0d0e21e 142=item 4.
143
5a964f20 144A reference to an anonymous subroutine can be created by using
a0d0e21e 145C<sub> without a subname:
146
147 $coderef = sub { print "Boink!\n" };
148
7c2ea1c7 149Note the semicolon. Except for the code
150inside not being immediately executed, a C<sub {}> is not so much a
a0d0e21e 151declaration as it is an operator, like C<do{}> or C<eval{}>. (However, no
5a964f20 152matter how many times you execute that particular line (unless you're in an
19799a22 153C<eval("...")>), $coderef will still have a reference to the I<same>
a0d0e21e 154anonymous subroutine.)
155
748a9306 156Anonymous subroutines act as closures with respect to my() variables,
7c2ea1c7 157that is, variables lexically visible within the current scope. Closure
748a9306 158is a notion out of the Lisp world that says if you define an anonymous
159function in a particular lexical context, it pretends to run in that
7c2ea1c7 160context even when it's called outside the context.
748a9306 161
162In human terms, it's a funny way of passing arguments to a subroutine when
163you define it as well as when you call it. It's useful for setting up
164little bits of code to run later, such as callbacks. You can even
54310121 165do object-oriented stuff with it, though Perl already provides a different
166mechanism to do that--see L<perlobj>.
748a9306 167
7c2ea1c7 168You might also think of closure as a way to write a subroutine
169template without using eval(). Here's a small example of how
170closures work:
748a9306 171
172 sub newprint {
173 my $x = shift;
174 return sub { my $y = shift; print "$x, $y!\n"; };
a0d0e21e 175 }
748a9306 176 $h = newprint("Howdy");
177 $g = newprint("Greetings");
178
179 # Time passes...
180
181 &$h("world");
182 &$g("earthlings");
a0d0e21e 183
748a9306 184This prints
185
186 Howdy, world!
187 Greetings, earthlings!
188
7c2ea1c7 189Note particularly that $x continues to refer to the value passed
190into newprint() I<despite> "my $x" having gone out of scope by the
191time the anonymous subroutine runs. That's what a closure is all
192about.
748a9306 193
5a964f20 194This applies only to lexical variables, by the way. Dynamic variables
748a9306 195continue to work as they have always worked. Closure is not something
196that most Perl programmers need trouble themselves about to begin with.
a0d0e21e 197
198=item 5.
199
200References are often returned by special subroutines called constructors.
7c2ea1c7 201Perl objects are just references to a special type of object that happens to know
a0d0e21e 202which package it's associated with. Constructors are just special
203subroutines that know how to create that association. They do so by
204starting with an ordinary reference, and it remains an ordinary reference
5a964f20 205even while it's also being an object. Constructors are often
206named new() and called indirectly:
a0d0e21e 207
208 $objref = new Doggie (Tail => 'short', Ears => 'long');
209
5a964f20 210But don't have to be:
211
212 $objref = Doggie->new(Tail => 'short', Ears => 'long');
213
214 use Term::Cap;
215 $terminal = Term::Cap->Tgetent( { OSPEED => 9600 });
216
217 use Tk;
218 $main = MainWindow->new();
219 $menubar = $main->Frame(-relief => "raised",
220 -borderwidth => 2)
221
a0d0e21e 222=item 6.
223
224References of the appropriate type can spring into existence if you
5f05dabc 225dereference them in a context that assumes they exist. Because we haven't
a0d0e21e 226talked about dereferencing yet, we can't show you any examples yet.
227
cb1a09d0 228=item 7.
229
55497cff 230A reference can be created by using a special syntax, lovingly known as
231the *foo{THING} syntax. *foo{THING} returns a reference to the THING
232slot in *foo (which is the symbol table entry which holds everything
233known as foo).
cb1a09d0 234
55497cff 235 $scalarref = *foo{SCALAR};
236 $arrayref = *ARGV{ARRAY};
237 $hashref = *ENV{HASH};
238 $coderef = *handler{CODE};
36477c24 239 $ioref = *STDIN{IO};
55497cff 240 $globref = *foo{GLOB};
241
7c2ea1c7 242All of these are self-explanatory except for C<*foo{IO}>. It returns
243the IO handle, used for file handles (L<perlfunc/open>), sockets
244(L<perlfunc/socket> and L<perlfunc/socketpair>), and directory
245handles (L<perlfunc/opendir>). For compatibility with previous
246versions of Perl, C<*foo{FILEHANDLE}> is a synonym for C<*foo{IO}>.
55497cff 247
7c2ea1c7 248C<*foo{THING}> returns undef if that particular THING hasn't been used yet,
249except in the case of scalars. C<*foo{SCALAR}> returns a reference to an
5f05dabc 250anonymous scalar if $foo hasn't been used yet. This might change in a
251future release.
252
7c2ea1c7 253C<*foo{IO}> is an alternative to the C<*HANDLE> mechanism given in
5a964f20 254L<perldata/"Typeglobs and Filehandles"> for passing filehandles
255into or out of subroutines, or storing into larger data structures.
256Its disadvantage is that it won't create a new filehandle for you.
7c2ea1c7 257Its advantage is that you have less risk of clobbering more than
258you want to with a typeglob assignment. (It still conflates file
259and directory handles, though.) However, if you assign the incoming
260value to a scalar instead of a typeglob as we do in the examples
261below, there's no risk of that happening.
36477c24 262
7c2ea1c7 263 splutter(*STDOUT); # pass the whole glob
264 splutter(*STDOUT{IO}); # pass both file and dir handles
5a964f20 265
cb1a09d0 266 sub splutter {
267 my $fh = shift;
268 print $fh "her um well a hmmm\n";
269 }
270
7c2ea1c7 271 $rec = get_rec(*STDIN); # pass the whole glob
272 $rec = get_rec(*STDIN{IO}); # pass both file and dir handles
5a964f20 273
cb1a09d0 274 sub get_rec {
275 my $fh = shift;
276 return scalar <$fh>;
277 }
278
a0d0e21e 279=back
280
5a964f20 281=head2 Using References
282
a0d0e21e 283That's it for creating references. By now you're probably dying to
284know how to use references to get back to your long-lost data. There
285are several basic methods.
286
287=over 4
288
289=item 1.
290
6309d9d9 291Anywhere you'd put an identifier (or chain of identifiers) as part
292of a variable or subroutine name, you can replace the identifier with
293a simple scalar variable containing a reference of the correct type:
a0d0e21e 294
295 $bar = $$scalarref;
296 push(@$arrayref, $filename);
297 $$arrayref[0] = "January";
298 $$hashref{"KEY"} = "VALUE";
299 &$coderef(1,2,3);
cb1a09d0 300 print $globref "output\n";
a0d0e21e 301
19799a22 302It's important to understand that we are specifically I<not> dereferencing
a0d0e21e 303C<$arrayref[0]> or C<$hashref{"KEY"}> there. The dereference of the
19799a22 304scalar variable happens I<before> it does any key lookups. Anything more
a0d0e21e 305complicated than a simple scalar variable must use methods 2 or 3 below.
306However, a "simple scalar" includes an identifier that itself uses method
3071 recursively. Therefore, the following prints "howdy".
308
309 $refrefref = \\\"howdy";
310 print $$$$refrefref;
311
312=item 2.
313
6309d9d9 314Anywhere you'd put an identifier (or chain of identifiers) as part of a
315variable or subroutine name, you can replace the identifier with a
316BLOCK returning a reference of the correct type. In other words, the
317previous examples could be written like this:
a0d0e21e 318
319 $bar = ${$scalarref};
320 push(@{$arrayref}, $filename);
321 ${$arrayref}[0] = "January";
322 ${$hashref}{"KEY"} = "VALUE";
323 &{$coderef}(1,2,3);
36477c24 324 $globref->print("output\n"); # iff IO::Handle is loaded
a0d0e21e 325
326Admittedly, it's a little silly to use the curlies in this case, but
327the BLOCK can contain any arbitrary expression, in particular,
328subscripted expressions:
329
54310121 330 &{ $dispatch{$index} }(1,2,3); # call correct routine
a0d0e21e 331
332Because of being able to omit the curlies for the simple case of C<$$x>,
333people often make the mistake of viewing the dereferencing symbols as
334proper operators, and wonder about their precedence. If they were,
5f05dabc 335though, you could use parentheses instead of braces. That's not the case.
a0d0e21e 336Consider the difference below; case 0 is a short-hand version of case 1,
19799a22 337I<not> case 2:
a0d0e21e 338
339 $$hashref{"KEY"} = "VALUE"; # CASE 0
340 ${$hashref}{"KEY"} = "VALUE"; # CASE 1
341 ${$hashref{"KEY"}} = "VALUE"; # CASE 2
342 ${$hashref->{"KEY"}} = "VALUE"; # CASE 3
343
344Case 2 is also deceptive in that you're accessing a variable
345called %hashref, not dereferencing through $hashref to the hash
346it's presumably referencing. That would be case 3.
347
348=item 3.
349
6da72b64 350Subroutine calls and lookups of individual array elements arise often
351enough that it gets cumbersome to use method 2. As a form of
352syntactic sugar, the examples for method 2 may be written:
a0d0e21e 353
6da72b64 354 $arrayref->[0] = "January"; # Array element
355 $hashref->{"KEY"} = "VALUE"; # Hash element
356 $coderef->(1,2,3); # Subroutine call
a0d0e21e 357
6da72b64 358The left side of the arrow can be any expression returning a reference,
19799a22 359including a previous dereference. Note that C<$array[$x]> is I<not> the
a0d0e21e 360same thing as C<$array-E<gt>[$x]> here:
361
362 $array[$x]->{"foo"}->[0] = "January";
363
364This is one of the cases we mentioned earlier in which references could
365spring into existence when in an lvalue context. Before this
366statement, C<$array[$x]> may have been undefined. If so, it's
367automatically defined with a hash reference so that we can look up
368C<{"foo"}> in it. Likewise C<$array[$x]-E<gt>{"foo"}> will automatically get
369defined with an array reference so that we can look up C<[0]> in it.
5a964f20 370This process is called I<autovivification>.
a0d0e21e 371
19799a22 372One more thing here. The arrow is optional I<between> brackets
a0d0e21e 373subscripts, so you can shrink the above down to
374
375 $array[$x]{"foo"}[0] = "January";
376
377Which, in the degenerate case of using only ordinary arrays, gives you
378multidimensional arrays just like C's:
379
380 $score[$x][$y][$z] += 42;
381
382Well, okay, not entirely like C's arrays, actually. C doesn't know how
383to grow its arrays on demand. Perl does.
384
385=item 4.
386
387If a reference happens to be a reference to an object, then there are
388probably methods to access the things referred to, and you should probably
389stick to those methods unless you're in the class package that defines the
390object's methods. In other words, be nice, and don't violate the object's
391encapsulation without a very good reason. Perl does not enforce
392encapsulation. We are not totalitarians here. We do expect some basic
393civility though.
394
395=back
396
7c2ea1c7 397Using a string or number as a reference produces a symbolic reference,
398as explained above. Using a reference as a number produces an
399integer representing its storage location in memory. The only
400useful thing to be done with this is to compare two references
401numerically to see whether they refer to the same location.
402
403 if ($ref1 == $ref2) { # cheap numeric compare of references
404 print "refs 1 and 2 refer to the same thing\n";
405 }
406
407Using a reference as a string produces both its referent's type,
408including any package blessing as described in L<perlobj>, as well
409as the numeric address expressed in hex. The ref() operator returns
410just the type of thing the reference is pointing to, without the
411address. See L<perlfunc/ref> for details and examples of its use.
a0d0e21e 412
5a964f20 413The bless() operator may be used to associate the object a reference
414points to with a package functioning as an object class. See L<perlobj>.
a0d0e21e 415
5f05dabc 416A typeglob may be dereferenced the same way a reference can, because
7c2ea1c7 417the dereference syntax always indicates the type of reference desired.
a0d0e21e 418So C<${*foo}> and C<${\$foo}> both indicate the same scalar variable.
419
420Here's a trick for interpolating a subroutine call into a string:
421
cb1a09d0 422 print "My sub returned @{[mysub(1,2,3)]} that time.\n";
423
424The way it works is that when the C<@{...}> is seen in the double-quoted
425string, it's evaluated as a block. The block creates a reference to an
426anonymous array containing the results of the call to C<mysub(1,2,3)>. So
427the whole block returns a reference to an array, which is then
428dereferenced by C<@{...}> and stuck into the double-quoted string. This
429chicanery is also useful for arbitrary expressions:
a0d0e21e 430
184e9718 431 print "That yields @{[$n + 5]} widgets\n";
a0d0e21e 432
433=head2 Symbolic references
434
435We said that references spring into existence as necessary if they are
436undefined, but we didn't say what happens if a value used as a
19799a22 437reference is already defined, but I<isn't> a hard reference. If you
7c2ea1c7 438use it as a reference, it'll be treated as a symbolic
19799a22 439reference. That is, the value of the scalar is taken to be the I<name>
a0d0e21e 440of a variable, rather than a direct link to a (possibly) anonymous
441value.
442
443People frequently expect it to work like this. So it does.
444
445 $name = "foo";
446 $$name = 1; # Sets $foo
447 ${$name} = 2; # Sets $foo
448 ${$name x 2} = 3; # Sets $foofoo
449 $name->[0] = 4; # Sets $foo[0]
450 @$name = (); # Clears @foo
451 &$name(); # Calls &foo() (as in Perl 4)
452 $pack = "THAT";
453 ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval
454
7c2ea1c7 455This is powerful, and slightly dangerous, in that it's possible
a0d0e21e 456to intend (with the utmost sincerity) to use a hard reference, and
457accidentally use a symbolic reference instead. To protect against
458that, you can say
459
460 use strict 'refs';
461
462and then only hard references will be allowed for the rest of the enclosing
54310121 463block. An inner block may countermand that with
a0d0e21e 464
465 no strict 'refs';
466
5a964f20 467Only package variables (globals, even if localized) are visible to
468symbolic references. Lexical variables (declared with my()) aren't in
469a symbol table, and thus are invisible to this mechanism. For example:
a0d0e21e 470
5a964f20 471 local $value = 10;
b0c35547 472 $ref = "value";
a0d0e21e 473 {
474 my $value = 20;
475 print $$ref;
54310121 476 }
a0d0e21e 477
478This will still print 10, not 20. Remember that local() affects package
479variables, which are all "global" to the package.
480
748a9306 481=head2 Not-so-symbolic references
482
a6006777 483A new feature contributing to readability in perl version 5.001 is that the
484brackets around a symbolic reference behave more like quotes, just as they
748a9306 485always have within a string. That is,
486
487 $push = "pop on ";
488 print "${push}over";
489
7c2ea1c7 490has always meant to print "pop on over", even though push is
748a9306 491a reserved word. This has been generalized to work the same outside
492of quotes, so that
493
494 print ${push} . "over";
495
496and even
497
498 print ${ push } . "over";
499
500will have the same effect. (This would have been a syntax error in
7c2ea1c7 501Perl 5.000, though Perl 4 allowed it in the spaceless form.) This
748a9306 502construct is I<not> considered to be a symbolic reference when you're
503using strict refs:
504
505 use strict 'refs';
506 ${ bareword }; # Okay, means $bareword.
507 ${ "bareword" }; # Error, symbolic reference.
508
509Similarly, because of all the subscripting that is done using single
510words, we've applied the same rule to any bareword that is used for
511subscripting a hash. So now, instead of writing
512
513 $array{ "aaa" }{ "bbb" }{ "ccc" }
514
5f05dabc 515you can write just
748a9306 516
517 $array{ aaa }{ bbb }{ ccc }
518
519and not worry about whether the subscripts are reserved words. In the
520rare event that you do wish to do something like
521
522 $array{ shift }
523
524you can force interpretation as a reserved word by adding anything that
525makes it more than a bareword:
526
527 $array{ shift() }
528 $array{ +shift }
529 $array{ shift @_ }
530
531The B<-w> switch will warn you if it interprets a reserved word as a string.
5f05dabc 532But it will no longer warn you about using lowercase words, because the
748a9306 533string is effectively quoted.
534
49399b3f 535=head2 Pseudo-hashes: Using an array as a hash
536
7c2ea1c7 537B<WARNING>: This section describes an experimental feature. Details may
49399b3f 538change without notice in future versions.
539
7c2ea1c7 540Beginning with release 5.005 of Perl, you may use an array reference
49399b3f 541in some contexts that would normally require a hash reference. This
542allows you to access array elements using symbolic names, as if they
543were fields in a structure.
544
545For this to work, the array must contain extra information. The first
546element of the array has to be a hash reference that maps field names
547to array indices. Here is an example:
548
549 $struct = [{foo => 1, bar => 2}, "FOO", "BAR"];
550
551 $struct->{foo}; # same as $struct->[1], i.e. "FOO"
552 $struct->{bar}; # same as $struct->[2], i.e. "BAR"
553
554 keys %$struct; # will return ("foo", "bar") in some order
555 values %$struct; # will return ("FOO", "BAR") in same some order
556
557 while (my($k,$v) = each %$struct) {
558 print "$k => $v\n";
559 }
560
01020589 561Perl will raise an exception if you try to access nonexistent fields.
562For better performance, Perl can also
49399b3f 563do the translation from field names to array indices at compile time for
564typed object references. See L<fields>.
565
01020589 566There are two ways to check for the existence of a key in a
e0478e5a 567pseudo-hash. The first is to use exists(). This checks to see if the
01020589 568given field has ever been set. It acts this way to match the behavior
e0478e5a 569of a regular hash. For instance:
570
c99cdc19 571 $phash = [{foo =>1, bar => 2, pants => 3}, 'FOO'];
e0478e5a 572 $phash->{pants} = undef;
573
01020589 574 print exists $phash->{foo}; # true, 'foo' was set in the declaration
575 print exists $phash->{bar}; # false, 'bar' has not been used.
576 print exists $phash->{pants}; # true, your 'pants' have been touched
e0478e5a 577
578The second is to use exists() on the hash reference sitting in the
579first array element. This checks to see if the given key is a valid
580field in the pseudo-hash.
581
01020589 582 print exists $phash->[0]{bar}; # true, 'bar' is a valid field
583 print exists $phash->[0]{shoes};# false, 'shoes' can't be used
584
585delete() on a pseudo-hash element only deletes the value corresponding
586to the key, not the key itself. To delete the key, you'll have to
587explicitly delete it from the first hash element.
588
589 print delete $phash->{foo}; # prints $phash->[1], "FOO"
590 print exists $phash->{foo}; # false
591 print exists $phash->[0]{foo}; # true, key still exists
592 print delete $phash->[0]{foo}; # now key is gone
593 print $phash->{foo}; # runtime exception
e0478e5a 594
5a964f20 595=head2 Function Templates
596
597As explained above, a closure is an anonymous function with access to the
598lexical variables visible when that function was compiled. It retains
599access to those variables even though it doesn't get run until later,
600such as in a signal handler or a Tk callback.
601
602Using a closure as a function template allows us to generate many functions
c2611fb3 603that act similarly. Suppose you wanted functions named after the colors
5a964f20 604that generated HTML font changes for the various colors:
605
606 print "Be ", red("careful"), "with that ", green("light");
607
7c2ea1c7 608The red() and green() functions would be similar. To create these,
5a964f20 609we'll assign a closure to a typeglob of the name of the function we're
610trying to build.
611
612 @colors = qw(red blue green yellow orange purple violet);
613 for my $name (@colors) {
614 no strict 'refs'; # allow symbol table manipulation
615 *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };
616 }
617
618Now all those different functions appear to exist independently. You can
619call red(), RED(), blue(), BLUE(), green(), etc. This technique saves on
620both compile time and memory use, and is less error-prone as well, since
621syntax checks happen at compile time. It's critical that any variables in
622the anonymous subroutine be lexicals in order to create a proper closure.
623That's the reasons for the C<my> on the loop iteration variable.
624
625This is one of the only places where giving a prototype to a closure makes
626much sense. If you wanted to impose scalar context on the arguments of
627these functions (probably not a wise idea for this particular example),
628you could have written it this way instead:
629
630 *$name = sub ($) { "<FONT COLOR='$name'>$_[0]</FONT>" };
631
632However, since prototype checking happens at compile time, the assignment
633above happens too late to be of much use. You could address this by
634putting the whole loop of assignments within a BEGIN block, forcing it
635to occur during compilation.
636
637Access to lexicals that change over type--like those in the C<for> loop
638above--only works with closures, not general subroutines. In the general
639case, then, named subroutines do not nest properly, although anonymous
640ones do. If you are accustomed to using nested subroutines in other
641programming languages with their own private variables, you'll have to
7c2ea1c7 642work at it a bit in Perl. The intuitive coding of this type of thing
5a964f20 643incurs mysterious warnings about ``will not stay shared''. For example,
644this won't work:
645
646 sub outer {
647 my $x = $_[0] + 35;
648 sub inner { return $x * 19 } # WRONG
649 return $x + inner();
650 }
651
652A work-around is the following:
653
654 sub outer {
655 my $x = $_[0] + 35;
656 local *inner = sub { return $x * 19 };
657 return $x + inner();
658 }
659
660Now inner() can only be called from within outer(), because of the
661temporary assignments of the closure (anonymous subroutine). But when
662it does, it has normal access to the lexical variable $x from the scope
663of outer().
664
665This has the interesting effect of creating a function local to another
666function, something not normally supported in Perl.
667
cb1a09d0 668=head1 WARNING
748a9306 669
670You may not (usefully) use a reference as the key to a hash. It will be
671converted into a string:
672
673 $x{ \$a } = $a;
674
54310121 675If you try to dereference the key, it won't do a hard dereference, and
184e9718 676you won't accomplish what you're attempting. You might want to do something
cb1a09d0 677more like
748a9306 678
cb1a09d0 679 $r = \@a;
680 $x{ $r } = $r;
681
682And then at least you can use the values(), which will be
683real refs, instead of the keys(), which won't.
684
5a964f20 685The standard Tie::RefHash module provides a convenient workaround to this.
686
cb1a09d0 687=head1 SEE ALSO
a0d0e21e 688
689Besides the obvious documents, source code can be instructive.
7c2ea1c7 690Some pathological examples of the use of references can be found
a0d0e21e 691in the F<t/op/ref.t> regression test in the Perl source directory.
cb1a09d0 692
693See also L<perldsc> and L<perllol> for how to use references to create
5a964f20 694complex data structures, and L<perltoot>, L<perlobj>, and L<perlbot>
695for how to use them to create objects.