1PERLREF(1)             Perl Programmers Reference Guide             PERLREF(1)
2
3
4

NAME

6       perlref - Perl references and nested data structures
7

NOTE

9       This is complete documentation about all aspects of references.  For a
10       shorter, tutorial introduction to just the essential features, see
11       perlreftut.
12

DESCRIPTION

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

WARNING

611       You may not (usefully) use a reference as the key to a hash.  It will
612       be converted into a string:
613
614           $x{ \$a } = $a;
615
616       If you try to dereference the key, it won't do a hard dereference, and
617       you won't accomplish what you're attempting.  You might want to do
618       something more like
619
620           $r = \@a;
621           $x{ $r } = $r;
622
623       And then at least you can use the values(), which will be real refs,
624       instead of the keys(), which won't.
625
626       The standard Tie::RefHash module provides a convenient workaround to
627       this.
628

SEE ALSO

630       Besides the obvious documents, source code can be instructive.  Some
631       pathological examples of the use of references can be found in the
632       t/op/ref.t regression test in the Perl source directory.
633
634       See also perldsc and perllol for how to use references to create
635       complex data structures, and perltoot, perlobj, and perlbot for how to
636       use them to create objects.
637
638
639
640perl v5.10.1                      2009-02-12                        PERLREF(1)
Impressum