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

WARNING: Don't use references as hash keys

671       You may not (usefully) use a reference as the key to a hash.  It will
672       be converted into a string:
673
674           $x{ \$a } = $a;
675
676       If you try to dereference the key, it won't do a hard dereference, and
677       you won't accomplish what you're attempting.  You might want to do
678       something more like
679
680           $r = \@a;
681           $x{ $r } = $r;
682
683       And then at least you can use the values(), which will be real refs,
684       instead of the keys(), which won't.
685
686       The standard Tie::RefHash module provides a convenient workaround to
687       this.
688
689   Postfix Dereference Syntax
690       Beginning in v5.20.0, a postfix syntax for using references is
691       available.  It behaves as described in "Using References", but instead
692       of a prefixed sigil, a postfixed sigil-and-star is used.
693
694       For example:
695
696           $r = \@a;
697           @b = $r->@*; # equivalent to @$r or @{ $r }
698
699           $r = [ 1, [ 2, 3 ], 4 ];
700           $r->[1]->@*;  # equivalent to @{ $r->[1] }
701
702       In Perl 5.20 and 5.22, this syntax must be enabled with "use feature
703       'postderef'". As of Perl 5.24, no feature declarations are required to
704       make it available.
705
706       Postfix dereference should work in all circumstances where block
707       (circumfix) dereference worked, and should be entirely equivalent.
708       This syntax allows dereferencing to be written and read entirely left-
709       to-right.  The following equivalencies are defined:
710
711         $sref->$*;  # same as  ${ $sref }
712         $aref->@*;  # same as  @{ $aref }
713         $aref->$#*; # same as $#{ $aref }
714         $href->%*;  # same as  %{ $href }
715         $cref->&*;  # same as  &{ $cref }
716         $gref->**;  # same as  *{ $gref }
717
718       Note especially that "$cref->&*" is not equivalent to "$cref->()", and
719       can serve different purposes.
720
721       Glob elements can be extracted through the postfix dereferencing
722       feature:
723
724         $gref->*{SCALAR}; # same as *{ $gref }{SCALAR}
725
726       Postfix array and scalar dereferencing can be used in interpolating
727       strings (double quotes or the "qq" operator), but only if the
728       "postderef_qq" feature is enabled.
729
730   Postfix Reference Slicing
731       Value slices of arrays and hashes may also be taken with postfix
732       dereferencing notation, with the following equivalencies:
733
734         $aref->@[ ... ];  # same as @$aref[ ... ]
735         $href->@{ ... };  # same as @$href{ ... }
736
737       Postfix key/value pair slicing, added in 5.20.0 and documented in the
738       Key/Value Hash Slices section of perldata, also behaves as expected:
739
740         $aref->%[ ... ];  # same as %$aref[ ... ]
741         $href->%{ ... };  # same as %$href{ ... }
742
743       As with postfix array, postfix value slice dereferencing can be used in
744       interpolating strings (double quotes or the "qq" operator), but only if
745       the "postderef_qq" feature is enabled.
746
747   Assigning to References
748       Beginning in v5.22.0, the referencing operator can be assigned to.  It
749       performs an aliasing operation, so that the variable name referenced on
750       the left-hand side becomes an alias for the thing referenced on the
751       right-hand side:
752
753           \$a = \$b; # $a and $b now point to the same scalar
754           \&foo = \&bar; # foo() now means bar()
755
756       This syntax must be enabled with "use feature 'refaliasing'".  It is
757       experimental, and will warn by default unless "no warnings
758       'experimental::refaliasing'" is in effect.
759
760       These forms may be assigned to, and cause the right-hand side to be
761       evaluated in scalar context:
762
763           \$scalar
764           \@array
765           \%hash
766           \&sub
767           \my $scalar
768           \my @array
769           \my %hash
770           \state $scalar # or @array, etc.
771           \our $scalar   # etc.
772           \local $scalar # etc.
773           \local our $scalar # etc.
774           \$some_array[$index]
775           \$some_hash{$key}
776           \local $some_array[$index]
777           \local $some_hash{$key}
778           condition ? \$this : \$that[0] # etc.
779
780       Slicing operations and parentheses cause the right-hand side to be
781       evaluated in list context:
782
783           \@array[5..7]
784           (\@array[5..7])
785           \(@array[5..7])
786           \@hash{'foo','bar'}
787           (\@hash{'foo','bar'})
788           \(@hash{'foo','bar'})
789           (\$scalar)
790           \($scalar)
791           \(my $scalar)
792           \my($scalar)
793           (\@array)
794           (\%hash)
795           (\&sub)
796           \(&sub)
797           \($foo, @bar, %baz)
798           (\$foo, \@bar, \%baz)
799
800       Each element on the right-hand side must be a reference to a datum of
801       the right type.  Parentheses immediately surrounding an array (and
802       possibly also "my"/"state"/"our"/"local") will make each element of the
803       array an alias to the corresponding scalar referenced on the right-hand
804       side:
805
806           \(@a) = \(@b); # @a and @b now have the same elements
807           \my(@a) = \(@b); # likewise
808           \(my @a) = \(@b); # likewise
809           push @a, 3; # but now @a has an extra element that @b lacks
810           \(@a) = (\$a, \$b, \$c); # @a now contains $a, $b, and $c
811
812       Combining that form with "local" and putting parentheses immediately
813       around a hash are forbidden (because it is not clear what they should
814       do):
815
816           \local(@array) = foo(); # WRONG
817           \(%hash)       = bar(); # WRONG
818
819       Assignment to references and non-references may be combined in lists
820       and conditional ternary expressions, as long as the values on the
821       right-hand side are the right type for each element on the left, though
822       this may make for obfuscated code:
823
824           (my $tom, \my $dick, \my @harry) = (\1, \2, [1..3]);
825           # $tom is now \1
826           # $dick is now 2 (read-only)
827           # @harry is (1,2,3)
828
829           my $type = ref $thingy;
830           ($type ? $type eq 'ARRAY' ? \@foo : \$bar : $baz) = $thingy;
831
832       The "foreach" loop can also take a reference constructor for its loop
833       variable, though the syntax is limited to one of the following, with an
834       optional "my", "state", or "our" after the backslash:
835
836           \$s
837           \@a
838           \%h
839           \&c
840
841       No parentheses are permitted.  This feature is particularly useful for
842       arrays-of-arrays, or arrays-of-hashes:
843
844           foreach \my @a (@array_of_arrays) {
845               frobnicate($a[0], $a[-1]);
846           }
847
848           foreach \my %h (@array_of_hashes) {
849               $h{gelastic}++ if $h{type} eq 'funny';
850           }
851
852       CAVEAT: Aliasing does not work correctly with closures.  If you try to
853       alias lexical variables from an inner subroutine or "eval", the
854       aliasing will only be visible within that inner sub, and will not
855       affect the outer subroutine where the variables are declared.  This
856       bizarre behavior is subject to change.
857

Declaring a Reference to a Variable

859       Beginning in v5.26.0, the referencing operator can come after "my",
860       "state", "our", or "local".  This syntax must be enabled with "use
861       feature 'declared_refs'".  It is experimental, and will warn by default
862       unless "no warnings 'experimental::refaliasing'" is in effect.
863
864       This feature makes these:
865
866           my \$x;
867           our \$y;
868
869       equivalent to:
870
871           \my $x;
872           \our $x;
873
874       It is intended mainly for use in assignments to references (see
875       "Assigning to References", above).  It also allows the backslash to be
876       used on just some items in a list of declared variables:
877
878           my ($foo, \@bar, \%baz); # equivalent to:  my $foo, \my(@bar, %baz);
879

SEE ALSO

881       Besides the obvious documents, source code can be instructive.  Some
882       pathological examples of the use of references can be found in the
883       t/op/ref.t regression test in the Perl source directory.
884
885       See also perldsc and perllol for how to use references to create
886       complex data structures, and perlootut and perlobj for how to use them
887       to create objects.
888
889
890
891perl v5.28.2                      2018-11-01                        PERLREF(1)
Impressum