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       Backslash Operator
58
59       By using the backslash operator on a variable, subroutine, or value.
60       (This works much like the & (address-of) operator in C.)  This
61       typically creates another reference to a variable, because there's
62       already a reference to the variable in the symbol table.  But the
63       symbol table reference might go away, and you'll still have the
64       reference that the backslash returned.  Here are some examples:
65
66           $scalarref = \$foo;
67           $arrayref  = \@ARGV;
68           $hashref   = \%ENV;
69           $coderef   = \&handler;
70           $globref   = \*foo;
71
72       It isn't possible to create a true reference to an IO handle
73       (filehandle or dirhandle) using the backslash operator.  The most you
74       can get is a reference to a typeglob, which is actually a complete
75       symbol table entry.  But see the explanation of the *foo{THING} syntax
76       below.  However, you can still use type globs and globrefs as though
77       they were IO handles.
78
79       Square Brackets
80
81       A reference to an anonymous array can be created using square brackets:
82
83           $arrayref = [1, 2, ['a', 'b', 'c']];
84
85       Here we've created a reference to an anonymous array of three elements
86       whose final element is itself a reference to another anonymous array of
87       three elements.  (The multidimensional syntax described later can be
88       used to access this.  For example, after the above, "$arrayref->[2][1]"
89       would have the value "b".)
90
91       Taking a reference to an enumerated list is not the same as using
92       square brackets--instead it's the same as creating a list of
93       references!
94
95           @list = (\$a, \@b, \%c);
96           @list = \($a, @b, %c);      # same thing!
97
98       As a special case, "\(@foo)" returns a list of references to the
99       contents of @foo, not a reference to @foo itself.  Likewise for %foo,
100       except that the key references are to copies (since the keys are just
101       strings rather than full-fledged scalars).
102
103       Curly Brackets
104
105       A reference to an anonymous hash can be created using curly brackets:
106
107           $hashref = {
108               'Adam'  => 'Eve',
109               'Clyde' => 'Bonnie',
110           };
111
112       Anonymous hash and array composers like these can be intermixed freely
113       to produce as complicated a structure as you want.  The
114       multidimensional syntax described below works for these too.  The
115       values above are literals, but variables and expressions would work
116       just as well, because assignment operators in Perl (even within local()
117       or my()) are executable statements, not compile-time declarations.
118
119       Because curly brackets (braces) are used for several other things
120       including BLOCKs, you may occasionally have to disambiguate braces at
121       the beginning of a statement by putting a "+" or a "return" in front so
122       that Perl realizes the opening brace isn't starting a BLOCK.  The
123       economy and mnemonic value of using curlies is deemed worth this
124       occasional extra hassle.
125
126       For example, if you wanted a function to make a new hash and return a
127       reference to it, you have these options:
128
129           sub hashem {        { @_ } }   # silently wrong
130           sub hashem {       +{ @_ } }   # ok
131           sub hashem { return { @_ } }   # ok
132
133       On the other hand, if you want the other meaning, you can do this:
134
135           sub showem {        { @_ } }   # ambiguous (currently ok,
136                                          # but may change)
137           sub showem {       {; @_ } }   # ok
138           sub showem { { return @_ } }   # ok
139
140       The leading "+{" and "{;" always serve to disambiguate the expression
141       to mean either the HASH reference, or the BLOCK.
142
143       Anonymous Subroutines
144
145       A reference to an anonymous subroutine can be created by using "sub"
146       without a subname:
147
148           $coderef = sub { print "Boink!\n" };
149
150       Note the semicolon.  Except for the code inside not being immediately
151       executed, a "sub {}" is not so much a declaration as it is an operator,
152       like "do{}" or "eval{}".  (However, no matter how many times you
153       execute that particular line (unless you're in an "eval("...")"),
154       $coderef will still have a reference to the same anonymous subroutine.)
155
156       Anonymous subroutines act as closures with respect to my() variables,
157       that is, variables lexically visible within the current scope.  Closure
158       is a notion out of the Lisp world that says if you define an anonymous
159       function in a particular lexical context, it pretends to run in that
160       context even when it's called outside the context.
161
162       In human terms, it's a funny way of passing arguments to a subroutine
163       when you define it as well as when you call it.  It's useful for
164       setting up little bits of code to run later, such as callbacks.  You
165       can even do object-oriented stuff with it, though Perl already provides
166       a different mechanism to do that--see perlobj.
167
168       You might also think of closure as a way to write a subroutine template
169       without using eval().  Here's a small example of how closures work:
170
171           sub newprint {
172               my $x = shift;
173               return sub { my $y = shift; print "$x, $y!\n"; };
174           }
175           $h = newprint("Howdy");
176           $g = newprint("Greetings");
177
178           # Time passes...
179
180           &$h("world");
181           &$g("earthlings");
182
183       This prints
184
185           Howdy, world!
186           Greetings, earthlings!
187
188       Note particularly that $x continues to refer to the value passed into
189       newprint() despite "my $x" having gone out of scope by the time the
190       anonymous subroutine runs.  That's what a closure is all about.
191
192       This applies only to lexical variables, by the way.  Dynamic variables
193       continue to work as they have always worked.  Closure is not something
194       that most Perl programmers need trouble themselves about to begin with.
195
196       Constructors
197
198       References are often returned by special subroutines called
199       constructors.  Perl objects are just references to a special type of
200       object that happens to know which package it's associated with.
201       Constructors are just special subroutines that know how to create that
202       association.  They do so by starting with an ordinary reference, and it
203       remains an ordinary reference even while it's also being an object.
204       Constructors are often named "new()".  You 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 often
209       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       Autovivification
222
223       References of the appropriate type can spring into existence if you
224       dereference them in a context that assumes they exist.  Because we
225       haven't talked about dereferencing yet, we can't show you any examples
226       yet.
227
228       Typeglob Slots
229
230       A reference can be created by using a special syntax, lovingly known as
231       the *foo{THING} syntax.  *foo{THING} returns a reference to the THING
232       slot in *foo (which is the symbol table entry which holds everything
233       known as foo).
234
235           $scalarref = *foo{SCALAR};
236           $arrayref  = *ARGV{ARRAY};
237           $hashref   = *ENV{HASH};
238           $coderef   = *handler{CODE};
239           $ioref     = *STDIN{IO};
240           $globref   = *foo{GLOB};
241           $formatref = *foo{FORMAT};
242           $globname  = *foo{NAME};    # "foo"
243           $pkgname   = *foo{PACKAGE}; # "main"
244
245       Most of these are self-explanatory, but *foo{IO} deserves special
246       attention.  It returns the IO handle, used for file handles ("open" in
247       perlfunc), sockets ("socket" in perlfunc and "socketpair" in perlfunc),
248       and directory handles ("opendir" in perlfunc).  For compatibility with
249       previous versions of Perl, *foo{FILEHANDLE} is a synonym for *foo{IO},
250       though it is discouraged, to encourage a consistent use of one name:
251       IO.  On perls between v5.8 and v5.22, it will issue a deprecation
252       warning, but this deprecation has since been rescinded.
253
254       *foo{THING} returns undef if that particular THING hasn't been used
255       yet, except in the case of scalars.  *foo{SCALAR} returns a reference
256       to an anonymous scalar if $foo hasn't been used yet.  This might change
257       in a future release.
258
259       *foo{NAME} and *foo{PACKAGE} are the exception, in that they return
260       strings, rather than references.  These return the package and name of
261       the typeglob itself, rather than one that has been assigned to it.  So,
262       after "*foo=*Foo::bar", *foo will become "*Foo::bar" when used as a
263       string, but *foo{PACKAGE} and *foo{NAME} will continue to produce
264       "main" and "foo", respectively.
265
266       *foo{IO} is an alternative to the *HANDLE mechanism given in "Typeglobs
267       and Filehandles" in perldata for passing filehandles into or out of
268       subroutines, or storing into larger data structures.  Its disadvantage
269       is that it won't create a new filehandle for you.  Its advantage is
270       that you have less risk of clobbering more than you want to with a
271       typeglob assignment.  (It still conflates file and directory handles,
272       though.)  However, if you assign the incoming value to a scalar instead
273       of a typeglob as we do in the examples below, there's no risk of that
274       happening.
275
276           splutter(*STDOUT);          # pass the whole glob
277           splutter(*STDOUT{IO});      # pass both file and dir handles
278
279           sub splutter {
280               my $fh = shift;
281               print $fh "her um well a hmmm\n";
282           }
283
284           $rec = get_rec(*STDIN);     # pass the whole glob
285           $rec = get_rec(*STDIN{IO}); # pass both file and dir handles
286
287           sub get_rec {
288               my $fh = shift;
289               return scalar <$fh>;
290           }
291
292   Using References
293       That's it for creating references.  By now you're probably dying to
294       know how to use references to get back to your long-lost data.  There
295       are several basic methods.
296
297       Simple Scalar
298
299       Anywhere you'd put an identifier (or chain of identifiers) as part of a
300       variable or subroutine name, you can replace the identifier with a
301       simple scalar variable containing a reference of the correct type:
302
303           $bar = $$scalarref;
304           push(@$arrayref, $filename);
305           $$arrayref[0] = "January";
306           $$hashref{"KEY"} = "VALUE";
307           &$coderef(1,2,3);
308           print $globref "output\n";
309
310       It's important to understand that we are specifically not dereferencing
311       $arrayref[0] or $hashref{"KEY"} there.  The dereference of the scalar
312       variable happens before it does any key lookups.  Anything more
313       complicated than a simple scalar variable must use methods 2 or 3
314       below.  However, a "simple scalar" includes an identifier that itself
315       uses method 1 recursively.  Therefore, the following prints "howdy".
316
317           $refrefref = \\\"howdy";
318           print $$$$refrefref;
319
320       Block
321
322       Anywhere you'd put an identifier (or chain of identifiers) as part of a
323       variable or subroutine name, you can replace the identifier with a
324       BLOCK returning a reference of the correct type.  In other words, the
325       previous examples could be written like this:
326
327           $bar = ${$scalarref};
328           push(@{$arrayref}, $filename);
329           ${$arrayref}[0] = "January";
330           ${$hashref}{"KEY"} = "VALUE";
331           &{$coderef}(1,2,3);
332           $globref->print("output\n");  # iff IO::Handle is loaded
333
334       Admittedly, it's a little silly to use the curlies in this case, but
335       the BLOCK can contain any arbitrary expression, in particular,
336       subscripted expressions:
337
338           &{ $dispatch{$index} }(1,2,3);      # call correct routine
339
340       Because of being able to omit the curlies for the simple case of $$x,
341       people often make the mistake of viewing the dereferencing symbols as
342       proper operators, and wonder about their precedence.  If they were,
343       though, you could use parentheses instead of braces.  That's not the
344       case.  Consider the difference below; case 0 is a short-hand version of
345       case 1, not case 2:
346
347           $$hashref{"KEY"}   = "VALUE";       # CASE 0
348           ${$hashref}{"KEY"} = "VALUE";       # CASE 1
349           ${$hashref{"KEY"}} = "VALUE";       # CASE 2
350           ${$hashref->{"KEY"}} = "VALUE";     # CASE 3
351
352       Case 2 is also deceptive in that you're accessing a variable called
353       %hashref, not dereferencing through $hashref to the hash it's
354       presumably referencing.  That would be case 3.
355
356       Arrow Notation
357
358       Subroutine calls and lookups of individual array elements arise often
359       enough that it gets cumbersome to use method 2.  As a form of syntactic
360       sugar, the examples for method 2 may be written:
361
362           $arrayref->[0] = "January";   # Array element
363           $hashref->{"KEY"} = "VALUE";  # Hash element
364           $coderef->(1,2,3);            # Subroutine call
365
366       The left side of the arrow can be any expression returning a reference,
367       including a previous dereference.  Note that $array[$x] is not the same
368       thing as "$array->[$x]" here:
369
370           $array[$x]->{"foo"}->[0] = "January";
371
372       This is one of the cases we mentioned earlier in which references could
373       spring into existence when in an lvalue context.  Before this
374       statement, $array[$x] may have been undefined.  If so, it's
375       automatically defined with a hash reference so that we can look up
376       "{"foo"}" in it.  Likewise "$array[$x]->{"foo"}" will automatically get
377       defined with an array reference so that we can look up "[0]" in it.
378       This process is called autovivification.
379
380       One more thing here.  The arrow is optional between brackets
381       subscripts, so you can shrink the above down to
382
383           $array[$x]{"foo"}[0] = "January";
384
385       Which, in the degenerate case of using only ordinary arrays, gives you
386       multidimensional arrays just like C's:
387
388           $score[$x][$y][$z] += 42;
389
390       Well, okay, not entirely like C's arrays, actually.  C doesn't know how
391       to grow its arrays on demand.  Perl does.
392
393       Objects
394
395       If a reference happens to be a reference to an object, then there are
396       probably methods to access the things referred to, and you should
397       probably stick to those methods unless you're in the class package that
398       defines the object's methods.  In other words, be nice, and don't
399       violate the object's encapsulation without a very good reason.  Perl
400       does not enforce encapsulation.  We are not totalitarians here.  We do
401       expect some basic civility though.
402
403       Miscellaneous Usage
404
405       Using a string or number as a reference produces a symbolic reference,
406       as explained above.  Using a reference as a number produces an integer
407       representing its storage location in memory.  The only useful thing to
408       be done with this is to compare two references numerically to see
409       whether they refer to the same location.
410
411           if ($ref1 == $ref2) {  # cheap numeric compare of references
412               print "refs 1 and 2 refer to the same thing\n";
413           }
414
415       Using a reference as a string produces both its referent's type,
416       including any package blessing as described in perlobj, as well as the
417       numeric address expressed in hex.  The ref() operator returns just the
418       type of thing the reference is pointing to, without the address.  See
419       "ref" in perlfunc for details and examples of its use.
420
421       The bless() operator may be used to associate the object a reference
422       points to with a package functioning as an object class.  See perlobj.
423
424       A typeglob may be dereferenced the same way a reference can, because
425       the dereference syntax always indicates the type of reference desired.
426       So "${*foo}" and "${\$foo}" both indicate the same scalar variable.
427
428       Here's a trick for interpolating a subroutine call into a string:
429
430           print "My sub returned @{[mysub(1,2,3)]} that time.\n";
431
432       The way it works is that when the "@{...}" is seen in the double-quoted
433       string, it's evaluated as a block.  The block creates a reference to an
434       anonymous array containing the results of the call to "mysub(1,2,3)".
435       So the whole block returns a reference to an array, which is then
436       dereferenced by "@{...}" and stuck into the double-quoted string. This
437       chicanery is also useful for arbitrary expressions:
438
439           print "That yields @{[$n + 5]} widgets\n";
440
441       Similarly, an expression that returns a reference to a scalar can be
442       dereferenced via "${...}". Thus, the above expression may be written
443       as:
444
445           print "That yields ${\($n + 5)} widgets\n";
446
447   Circular References
448       It is possible to create a "circular reference" in Perl, which can lead
449       to memory leaks. A circular reference occurs when two references
450       contain a reference to each other, like this:
451
452           my $foo = {};
453           my $bar = { foo => $foo };
454           $foo->{bar} = $bar;
455
456       You can also create a circular reference with a single variable:
457
458           my $foo;
459           $foo = \$foo;
460
461       In this case, the reference count for the variables will never reach 0,
462       and the references will never be garbage-collected. This can lead to
463       memory leaks.
464
465       Because objects in Perl are implemented as references, it's possible to
466       have circular references with objects as well. Imagine a TreeNode class
467       where each node references its parent and child nodes. Any node with a
468       parent will be part of a circular reference.
469
470       You can break circular references by creating a "weak reference". A
471       weak reference does not increment the reference count for a variable,
472       which means that the object can go out of scope and be destroyed. You
473       can weaken a reference with the "weaken" function exported by the
474       Scalar::Util module.
475
476       Here's how we can make the first example safer:
477
478           use Scalar::Util 'weaken';
479
480           my $foo = {};
481           my $bar = { foo => $foo };
482           $foo->{bar} = $bar;
483
484           weaken $foo->{bar};
485
486       The reference from $foo to $bar has been weakened. When the $bar
487       variable goes out of scope, it will be garbage-collected. The next time
488       you look at the value of the "$foo->{bar}" key, it will be "undef".
489
490       This action at a distance can be confusing, so you should be careful
491       with your use of weaken. You should weaken the reference in the
492       variable that will go out of scope first. That way, the longer-lived
493       variable will contain the expected reference until it goes out of
494       scope.
495
496   Symbolic references
497       We said that references spring into existence as necessary if they are
498       undefined, but we didn't say what happens if a value used as a
499       reference is already defined, but isn't a hard reference.  If you use
500       it as a reference, it'll be treated as a symbolic reference.  That is,
501       the value of the scalar is taken to be the name of a variable, rather
502       than a direct link to a (possibly) anonymous value.
503
504       People frequently expect it to work like this.  So it does.
505
506           $name = "foo";
507           $$name = 1;                 # Sets $foo
508           ${$name} = 2;               # Sets $foo
509           ${$name x 2} = 3;           # Sets $foofoo
510           $name->[0] = 4;             # Sets $foo[0]
511           @$name = ();                # Clears @foo
512           &$name();                   # Calls &foo()
513           $pack = "THAT";
514           ${"${pack}::$name"} = 5;    # Sets $THAT::foo without eval
515
516       This is powerful, and slightly dangerous, in that it's possible to
517       intend (with the utmost sincerity) to use a hard reference, and
518       accidentally use a symbolic reference instead.  To protect against
519       that, you can say
520
521           use strict 'refs';
522
523       and then only hard references will be allowed for the rest of the
524       enclosing block.  An inner block may countermand that with
525
526           no strict 'refs';
527
528       Only package variables (globals, even if localized) are visible to
529       symbolic references.  Lexical variables (declared with my()) aren't in
530       a symbol table, and thus are invisible to this mechanism.  For example:
531
532           local $value = 10;
533           $ref = "value";
534           {
535               my $value = 20;
536               print $$ref;
537           }
538
539       This will still print 10, not 20.  Remember that local() affects
540       package variables, which are all "global" to the package.
541
542   Not-so-symbolic references
543       Brackets around a symbolic reference can simply serve to isolate an
544       identifier or variable name from the rest of an expression, just as
545       they always have within a string.  For example,
546
547           $push = "pop on ";
548           print "${push}over";
549
550       has always meant to print "pop on over", even though push is a reserved
551       word.  This is generalized to work the same without the enclosing
552       double quotes, so that
553
554           print ${push} . "over";
555
556       and even
557
558           print ${ push } . "over";
559
560       will have the same effect.  This construct is not considered to be a
561       symbolic reference when you're using strict refs:
562
563           use strict 'refs';
564           ${ bareword };      # Okay, means $bareword.
565           ${ "bareword" };    # Error, symbolic reference.
566
567       Similarly, because of all the subscripting that is done using single
568       words, the same rule applies to any bareword that is used for
569       subscripting a hash.  So now, instead of writing
570
571           $hash{ "aaa" }{ "bbb" }{ "ccc" }
572
573       you can write just
574
575           $hash{ aaa }{ bbb }{ ccc }
576
577       and not worry about whether the subscripts are reserved words.  In the
578       rare event that you do wish to do something like
579
580           $hash{ shift }
581
582       you can force interpretation as a reserved word by adding anything that
583       makes it more than a bareword:
584
585           $hash{ shift() }
586           $hash{ +shift }
587           $hash{ shift @_ }
588
589       The "use warnings" pragma or the -w switch will warn you if it
590       interprets a reserved word as a string.  But it will no longer warn you
591       about using lowercase words, because the string is effectively quoted.
592
593   Pseudo-hashes: Using an array as a hash
594       Pseudo-hashes have been removed from Perl.  The 'fields' pragma remains
595       available.
596
597   Function Templates
598       As explained above, an anonymous function with access to the lexical
599       variables visible when that function was compiled, creates a closure.
600       It retains access to those variables even though it doesn't get run
601       until later, such as in a signal handler or a Tk callback.
602
603       Using a closure as a function template allows us to generate many
604       functions that act similarly.  Suppose you wanted functions named after
605       the colors that generated HTML font changes for the various colors:
606
607           print "Be ", red("careful"), "with that ", green("light");
608
609       The red() and green() functions would be similar.  To create these,
610       we'll assign a closure to a typeglob of the name of the function we're
611       trying to build.
612
613           @colors = qw(red blue green yellow orange purple violet);
614           for my $name (@colors) {
615               no strict 'refs';       # allow symbol table manipulation
616               *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" };
617           }
618
619       Now all those different functions appear to exist independently.  You
620       can call red(), RED(), blue(), BLUE(), green(), etc.  This technique
621       saves on both compile time and memory use, and is less error-prone as
622       well, since syntax checks happen at compile time.  It's critical that
623       any variables in the anonymous subroutine be lexicals in order to
624       create a proper closure.  That's the reasons for the "my" on the loop
625       iteration variable.
626
627       This is one of the only places where giving a prototype to a closure
628       makes much sense.  If you wanted to impose scalar context on the
629       arguments of these functions (probably not a wise idea for this
630       particular example), you could have written it this way instead:
631
632           *$name = sub ($) { "<FONT COLOR='$name'>$_[0]</FONT>" };
633
634       However, since prototype checking happens at compile time, the
635       assignment above happens too late to be of much use.  You could address
636       this by putting the whole loop of assignments within a BEGIN block,
637       forcing it to occur during compilation.
638
639       Access to lexicals that change over time--like those in the "for" loop
640       above, basically aliases to elements from the surrounding lexical
641       scopes-- only works with anonymous subs, not with named subroutines.
642       Generally said, named subroutines do not nest properly and should only
643       be declared in the main package scope.
644
645       This is because named subroutines are created at compile time so their
646       lexical variables get assigned to the parent lexicals from the first
647       execution of the parent block. If a parent scope is entered a second
648       time, its lexicals are created again, while the nested subs still
649       reference the old ones.
650
651       Anonymous subroutines get to capture each time you execute the "sub"
652       operator, as they are created on the fly. If you are accustomed to
653       using nested subroutines in other programming languages with their own
654       private variables, you'll have to work at it a bit in Perl.  The
655       intuitive coding of this type of thing incurs mysterious warnings about
656       "will not stay shared" due to the reasons explained above.  For
657       example, this won't work:
658
659           sub outer {
660               my $x = $_[0] + 35;
661               sub inner { return $x * 19 }   # WRONG
662               return $x + inner();
663           }
664
665       A work-around is the following:
666
667           sub outer {
668               my $x = $_[0] + 35;
669               local *inner = sub { return $x * 19 };
670               return $x + inner();
671           }
672
673       Now inner() can only be called from within outer(), because of the
674       temporary assignments of the anonymous subroutine. But when it does, it
675       has normal access to the lexical variable $x from the scope of outer()
676       at the time outer is invoked.
677
678       This has the interesting effect of creating a function local to another
679       function, something not normally supported in Perl.
680
681   Postfix Dereference Syntax
682       Beginning in v5.20.0, a postfix syntax for using references is
683       available.  It behaves as described in "Using References", but instead
684       of a prefixed sigil, a postfixed sigil-and-star is used.
685
686       For example:
687
688           $r = \@a;
689           @b = $r->@*; # equivalent to @$r or @{ $r }
690
691           $r = [ 1, [ 2, 3 ], 4 ];
692           $r->[1]->@*;  # equivalent to @{ $r->[1] }
693
694       In Perl 5.20 and 5.22, this syntax must be enabled with "use feature
695       'postderef'". As of Perl 5.24, no feature declarations are required to
696       make it available.
697
698       Postfix dereference should work in all circumstances where block
699       (circumfix) dereference worked, and should be entirely equivalent.
700       This syntax allows dereferencing to be written and read entirely left-
701       to-right.  The following equivalencies are defined:
702
703         $sref->$*;  # same as  ${ $sref }
704         $aref->@*;  # same as  @{ $aref }
705         $aref->$#*; # same as $#{ $aref }
706         $href->%*;  # same as  %{ $href }
707         $cref->&*;  # same as  &{ $cref }
708         $gref->**;  # same as  *{ $gref }
709
710       Note especially that "$cref->&*" is not equivalent to "$cref->()", and
711       can serve different purposes.
712
713       Glob elements can be extracted through the postfix dereferencing
714       feature:
715
716         $gref->*{SCALAR}; # same as *{ $gref }{SCALAR}
717
718       Postfix array and scalar dereferencing can be used in interpolating
719       strings (double quotes or the "qq" operator), but only if the
720       "postderef_qq" feature is enabled.
721
722   Postfix Reference Slicing
723       Value slices of arrays and hashes may also be taken with postfix
724       dereferencing notation, with the following equivalencies:
725
726         $aref->@[ ... ];  # same as @$aref[ ... ]
727         $href->@{ ... };  # same as @$href{ ... }
728
729       Postfix key/value pair slicing, added in 5.20.0 and documented in the
730       Key/Value Hash Slices section of perldata, also behaves as expected:
731
732         $aref->%[ ... ];  # same as %$aref[ ... ]
733         $href->%{ ... };  # same as %$href{ ... }
734
735       As with postfix array, postfix value slice dereferencing can be used in
736       interpolating strings (double quotes or the "qq" operator), but only if
737       the "postderef_qq" feature is enabled.
738
739   Assigning to References
740       Beginning in v5.22.0, the referencing operator can be assigned to.  It
741       performs an aliasing operation, so that the variable name referenced on
742       the left-hand side becomes an alias for the thing referenced on the
743       right-hand side:
744
745           \$a = \$b; # $a and $b now point to the same scalar
746           \&foo = \&bar; # foo() now means bar()
747
748       This syntax must be enabled with "use feature 'refaliasing'".  It is
749       experimental, and will warn by default unless "no warnings
750       'experimental::refaliasing'" is in effect.
751
752       These forms may be assigned to, and cause the right-hand side to be
753       evaluated in scalar context:
754
755           \$scalar
756           \@array
757           \%hash
758           \&sub
759           \my $scalar
760           \my @array
761           \my %hash
762           \state $scalar # or @array, etc.
763           \our $scalar   # etc.
764           \local $scalar # etc.
765           \local our $scalar # etc.
766           \$some_array[$index]
767           \$some_hash{$key}
768           \local $some_array[$index]
769           \local $some_hash{$key}
770           condition ? \$this : \$that[0] # etc.
771
772       Slicing operations and parentheses cause the right-hand side to be
773       evaluated in list context:
774
775           \@array[5..7]
776           (\@array[5..7])
777           \(@array[5..7])
778           \@hash{'foo','bar'}
779           (\@hash{'foo','bar'})
780           \(@hash{'foo','bar'})
781           (\$scalar)
782           \($scalar)
783           \(my $scalar)
784           \my($scalar)
785           (\@array)
786           (\%hash)
787           (\&sub)
788           \(&sub)
789           \($foo, @bar, %baz)
790           (\$foo, \@bar, \%baz)
791
792       Each element on the right-hand side must be a reference to a datum of
793       the right type.  Parentheses immediately surrounding an array (and
794       possibly also "my"/"state"/"our"/"local") will make each element of the
795       array an alias to the corresponding scalar referenced on the right-hand
796       side:
797
798           \(@a) = \(@b); # @a and @b now have the same elements
799           \my(@a) = \(@b); # likewise
800           \(my @a) = \(@b); # likewise
801           push @a, 3; # but now @a has an extra element that @b lacks
802           \(@a) = (\$a, \$b, \$c); # @a now contains $a, $b, and $c
803
804       Combining that form with "local" and putting parentheses immediately
805       around a hash are forbidden (because it is not clear what they should
806       do):
807
808           \local(@array) = foo(); # WRONG
809           \(%hash)       = bar(); # WRONG
810
811       Assignment to references and non-references may be combined in lists
812       and conditional ternary expressions, as long as the values on the
813       right-hand side are the right type for each element on the left, though
814       this may make for obfuscated code:
815
816           (my $tom, \my $dick, \my @harry) = (\1, \2, [1..3]);
817           # $tom is now \1
818           # $dick is now 2 (read-only)
819           # @harry is (1,2,3)
820
821           my $type = ref $thingy;
822           ($type ? $type eq 'ARRAY' ? \@foo : \$bar : $baz) = $thingy;
823
824       The "foreach" loop can also take a reference constructor for its loop
825       variable, though the syntax is limited to one of the following, with an
826       optional "my", "state", or "our" after the backslash:
827
828           \$s
829           \@a
830           \%h
831           \&c
832
833       No parentheses are permitted.  This feature is particularly useful for
834       arrays-of-arrays, or arrays-of-hashes:
835
836           foreach \my @a (@array_of_arrays) {
837               frobnicate($a[0], $a[-1]);
838           }
839
840           foreach \my %h (@array_of_hashes) {
841               $h{gelastic}++ if $h{type} eq 'funny';
842           }
843
844       CAVEAT: Aliasing does not work correctly with closures.  If you try to
845       alias lexical variables from an inner subroutine or "eval", the
846       aliasing will only be visible within that inner sub, and will not
847       affect the outer subroutine where the variables are declared.  This
848       bizarre behavior is subject to change.
849
850   Declaring a Reference to a Variable
851       Beginning in v5.26.0, the referencing operator can come after "my",
852       "state", "our", or "local".  This syntax must be enabled with "use
853       feature 'declared_refs'".  It is experimental, and will warn by default
854       unless "no warnings 'experimental::refaliasing'" is in effect.
855
856       This feature makes these:
857
858           my \$x;
859           our \$y;
860
861       equivalent to:
862
863           \my $x;
864           \our $x;
865
866       It is intended mainly for use in assignments to references (see
867       "Assigning to References", above).  It also allows the backslash to be
868       used on just some items in a list of declared variables:
869
870           my ($foo, \@bar, \%baz); # equivalent to:  my $foo, \my(@bar, %baz);
871

WARNING: Don't use references as hash keys

873       You may not (usefully) use a reference as the key to a hash.  It will
874       be converted into a string:
875
876           $x{ \$a } = $a;
877
878       If you try to dereference the key, it won't do a hard dereference, and
879       you won't accomplish what you're attempting.  You might want to do
880       something more like
881
882           $r = \@a;
883           $x{ $r } = $r;
884
885       And then at least you can use the values(), which will be real refs,
886       instead of the keys(), which won't.
887
888       The standard Tie::RefHash module provides a convenient workaround to
889       this.
890

SEE ALSO

892       Besides the obvious documents, source code can be instructive.  Some
893       pathological examples of the use of references can be found in the
894       t/op/ref.t regression test in the Perl source directory.
895
896       See also perldsc and perllol for how to use references to create
897       complex data structures, and perlootut and perlobj for how to use them
898       to create objects.
899
900
901
902perl v5.34.0                      2021-10-18                        PERLREF(1)
Impressum