Debian Codegraph @

upstream/opensuse-tumbleweed/man1/perlref.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PERLREF 1"
61 .TH PERLREF 1 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 perlref \- Perl references and nested data structures
68 .IX Xref "reference pointer data structure structure struct"
69 .SH NOTE
70 .IX Header "NOTE"
71 This is complete documentation about all aspects of references.
72 For a shorter, tutorial introduction to just the essential features,
73 see perlreftut.
74 .SH DESCRIPTION
75 .IX Header "DESCRIPTION"
76 Before release 5 of Perl it was difficult to represent complex data
77 structures, because all references had to be symbolic\-\-and even then
78 it was difficult to refer to a variable instead of a symbol table entry.
79 Perl now not only makes it easier to use symbolic references to variables,
80 but also lets you have "hard" references to any piece of data or code.
81 Any scalar may hold a hard reference. Because arrays and hashes contain
82 scalars, you can now easily build arrays of arrays, arrays of hashes,
83 hashes of arrays, arrays of hashes of functions, and so on.
84 .PP
85 Hard references are smart\-\-they keep track of reference counts for you,
86 automatically freeing the thing referred to when its reference count goes
87 to zero. (Reference counts for values in self\-referential or
88 cyclic data structures may not go to zero without a little help; see
89 "Circular References" for a detailed explanation.)
90 If that thing happens to be an object, the object is destructed. See
91 perlobj for more about objects. (In a sense, everything in Perl is an
92 object, but we usually reserve the word for references to objects that
93 have been officially "blessed" into a class package.)
94 .PP
95 Symbolic references are names of variables or other objects, just as a
96 symbolic link in a Unix filesystem contains merely the name of a file.
97 The \f(CW*glob\fR notation is something of a symbolic reference. (Symbolic
98 references are sometimes called "soft references", but please don\*(Aqt call
99 them that; references are confusing enough without useless synonyms.)
100 .IX Xref "reference, symbolic reference, soft symbolic reference soft reference"
101 .PP
102 In contrast, hard references are more like hard links in a Unix file
103 system: They are used to access an underlying object without concern for
104 what its (other) name is. When the word "reference" is used without an
105 adjective, as in the following paragraph, it is usually talking about a
106 hard reference.
107 .IX Xref "reference, hard hard reference"
108 .PP
109 References are easy to use in Perl. There is just one overriding
110 principle: in general, Perl does no implicit referencing or dereferencing.
111 When a scalar is holding a reference, it always behaves as a simple scalar.
112 It doesn\*(Aqt magically start being an array or hash or subroutine; you have to
113 tell it explicitly to do so, by dereferencing it.
114 .SS "Making References"
115 .IX Xref "reference, creation referencing"
116 .IX Subsection "Making References"
117 References can be created in several ways.
118 .PP
119 \fIBackslash Operator\fR
120 .IX Xref "\\ backslash"
121 .IX Subsection "Backslash Operator"
122 .PP
123 By using the backslash operator on a variable, subroutine, or value.
124 (This works much like the & (address\-of) operator in C.)
125 This typically creates \fIanother\fR reference to a variable, because
126 there\*(Aqs already a reference to the variable in the symbol table. But
127 the symbol table reference might go away, and you\*(Aqll still have the
128 reference that the backslash returned. Here are some examples:
129 .PP
130 .Vb 5
131 \& $scalarref = \e$foo;
132 \& $arrayref = \e@ARGV;
133 \& $hashref = \e%ENV;
134 \& $coderef = \e&handler;
135 \& $globref = \e*foo;
136 .Ve
137 .PP
138 It isn\*(Aqt possible to create a true reference to an IO handle (filehandle
139 or dirhandle) using the backslash operator. The most you can get is a
140 reference to a typeglob, which is actually a complete symbol table entry.
141 But see the explanation of the \f(CW*foo{THING}\fR syntax below. However,
142 you can still use type globs and globrefs as though they were IO handles.
143 .PP
144 \fISquare Brackets\fR
145 .IX Xref "array, anonymous [ [] square bracket bracket, square arrayref array reference reference, array"
146 .IX Subsection "Square Brackets"
147 .PP
148 A reference to an anonymous array can be created using square
149 brackets:
150 .PP
151 .Vb 1
152 \& $arrayref = [1, 2, [\*(Aqa\*(Aq, \*(Aqb\*(Aq, \*(Aqc\*(Aq]];
153 .Ve
154 .PP
155 Here we\*(Aqve created a reference to an anonymous array of three elements
156 whose final element is itself a reference to another anonymous array of three
157 elements. (The multidimensional syntax described later can be used to
158 access this. For example, after the above, \f(CW\*(C`$arrayref\->[2][1]\*(C'\fR would have
159 the value "b".)
160 .PP
161 Taking a reference to an enumerated list is not the same
162 as using square brackets\-\-instead it\*(Aqs the same as creating
163 a list of references!
164 .PP
165 .Vb 2
166 \& @list = (\e$x, \e@y, \e%z);
167 \& @list = \e($x, @y, %z); # same thing!
168 .Ve
169 .PP
170 As a special case, \f(CW\*(C`\e(@foo)\*(C'\fR returns a list of references to the contents
171 of \f(CW@foo\fR, not a reference to \f(CW@foo\fR itself. Likewise for \f(CW%foo\fR,
172 except that the key references are to copies (since the keys are just
173 strings rather than full\-fledged scalars).
174 .PP
175 \fICurly Brackets\fR
176 .IX Xref "hash, anonymous { {} curly bracket bracket, curly brace hashref hash reference reference, hash"
177 .IX Subsection "Curly Brackets"
178 .PP
179 A reference to an anonymous hash can be created using curly
180 brackets:
181 .PP
182 .Vb 4
183 \& $hashref = {
184 \& \*(AqAdam\*(Aq => \*(AqEve\*(Aq,
185 \& \*(AqClyde\*(Aq => \*(AqBonnie\*(Aq,
186 \& };
187 .Ve
188 .PP
189 Anonymous hash and array composers like these can be intermixed freely to
190 produce as complicated a structure as you want. The multidimensional
191 syntax described below works for these too. The values above are
192 literals, but variables and expressions would work just as well, because
193 assignment operators in Perl (even within \fBlocal()\fR or \fBmy()\fR) are executable
194 statements, not compile\-time declarations.
195 .PP
196 Because curly brackets (braces) are used for several other things
197 including BLOCKs, you may occasionally have to disambiguate braces at the
198 beginning of a statement by putting a \f(CW\*(C`+\*(C'\fR or a \f(CW\*(C`return\*(C'\fR in front so
199 that Perl realizes the opening brace isn\*(Aqt starting a BLOCK. The economy and
200 mnemonic value of using curlies is deemed worth this occasional extra
201 hassle.
202 .PP
203 For example, if you wanted a function to make a new hash and return a
204 reference to it, you have these options:
205 .PP
206 .Vb 3
207 \& sub hashem { { @_ } } # silently wrong
208 \& sub hashem { +{ @_ } } # ok
209 \& sub hashem { return { @_ } } # ok
210 .Ve
211 .PP
212 On the other hand, if you want the other meaning, you can do this:
213 .PP
214 .Vb 4
215 \& sub showem { { @_ } } # ambiguous (currently ok,
216 \& # but may change)
217 \& sub showem { {; @_ } } # ok
218 \& sub showem { { return @_ } } # ok
219 .Ve
220 .PP
221 The leading \f(CW\*(C`+{\*(C'\fR and \f(CW\*(C`{;\*(C'\fR always serve to disambiguate
222 the expression to mean either the HASH reference, or the BLOCK.
223 .PP
224 \fIAnonymous Subroutines\fR
225 .IX Xref "subroutine, anonymous subroutine, reference reference, subroutine scope, lexical closure lexical lexical scope"
226 .IX Subsection "Anonymous Subroutines"
227 .PP
228 A reference to an anonymous subroutine can be created by using
229 \&\f(CW\*(C`sub\*(C'\fR without a subname:
230 .PP
231 .Vb 1
232 \& $coderef = sub { print "Boink!\en" };
233 .Ve
234 .PP
235 Note the semicolon. Except for the code
236 inside not being immediately executed, a \f(CW\*(C`sub {}\*(C'\fR is not so much a
237 declaration as it is an operator, like \f(CW\*(C`do{}\*(C'\fR or \f(CW\*(C`eval{}\*(C'\fR. (However, no
238 matter how many times you execute that particular line (unless you\*(Aqre in an
239 \&\f(CWeval("...")\fR), \f(CW$coderef\fR will still have a reference to the \fIsame\fR
240 anonymous subroutine.)
241 .PP
242 Anonymous subroutines act as closures with respect to \fBmy()\fR variables,
243 that is, variables lexically visible within the current scope. Closure
244 is a notion out of the Lisp world that says if you define an anonymous
245 function in a particular lexical context, it pretends to run in that
246 context even when it\*(Aqs called outside the context.
247 .PP
248 In human terms, it\*(Aqs a funny way of passing arguments to a subroutine when
249 you define it as well as when you call it. It\*(Aqs useful for setting up
250 little bits of code to run later, such as callbacks. You can even
251 do object\-oriented stuff with it, though Perl already provides a different
252 mechanism to do that\-\-see perlobj.
253 .PP
254 You might also think of closure as a way to write a subroutine
255 template without using \fBeval()\fR. Here\*(Aqs a small example of how
256 closures work:
257 .PP
258 .Vb 6
259 \& sub newprint {
260 \& my $x = shift;
261 \& return sub { my $y = shift; print "$x, $y!\en"; };
262 \& }
263 \& $h = newprint("Howdy");
264 \& $g = newprint("Greetings");
265 \&
266 \& # Time passes...
267 \&
268 \& &$h("world");
269 \& &$g("earthlings");
270 .Ve
271 .PP
272 This prints
273 .PP
274 .Vb 2
275 \& Howdy, world!
276 \& Greetings, earthlings!
277 .Ve
278 .PP
279 Note particularly that \f(CW$x\fR continues to refer to the value passed
280 into \fBnewprint()\fR \fIdespite\fR "my \f(CW$x\fR" having gone out of scope by the
281 time the anonymous subroutine runs. That\*(Aqs what a closure is all
282 about.
283 .PP
284 This applies only to lexical variables, by the way. Dynamic variables
285 continue to work as they have always worked. Closure is not something
286 that most Perl programmers need trouble themselves about to begin with.
287 .PP
288 \fIConstructors\fR
289 .IX Xref "constructor new"
290 .IX Subsection "Constructors"
291 .PP
292 References are often returned by special subroutines called constructors. Perl
293 objects are just references to a special type of object that happens to know
294 which package it\*(Aqs associated with. Constructors are just special subroutines
295 that know how to create that association. They do so by starting with an
296 ordinary reference, and it remains an ordinary reference even while it\*(Aqs also
297 being an object. Constructors are often named \f(CWnew()\fR. You \fIcan\fR call them
298 indirectly:
299 .PP
300 .Vb 1
301 \& $objref = new Doggie( Tail => \*(Aqshort\*(Aq, Ears => \*(Aqlong\*(Aq );
302 .Ve
303 .PP
304 But that can produce ambiguous syntax in certain cases, so it\*(Aqs often
305 better to use the direct method invocation approach:
306 .PP
307 .Vb 1
308 \& $objref = Doggie\->new(Tail => \*(Aqshort\*(Aq, Ears => \*(Aqlong\*(Aq);
309 \&
310 \& use Term::Cap;
311 \& $terminal = Term::Cap\->Tgetent( { OSPEED => 9600 });
312 \&
313 \& use Tk;
314 \& $main = MainWindow\->new();
315 \& $menubar = $main\->Frame(\-relief => "raised",
316 \& \-borderwidth => 2)
317 .Ve
318 .PP
319 This indirect object syntax is only available when
320 \&\f(CW\*(C`use feature "indirect"\*(C'\fR is in effect,
321 and that is not the case when \f(CW\*(C`use v5.36\*(C'\fR (or
322 higher) is requested, it is best to avoid indirect object syntax entirely.
323 .PP
324 \fIAutovivification\fR
325 .IX Xref "autovivification"
326 .IX Subsection "Autovivification"
327 .PP
328 References of the appropriate type can spring into existence if you
329 dereference them in a context that assumes they exist. Because we haven\*(Aqt
330 talked about dereferencing yet, we can\*(Aqt show you any examples yet.
331 .PP
332 \fITypeglob Slots\fR
333 .IX Xref "*foo{THING} *"
334 .IX Subsection "Typeglob Slots"
335 .PP
336 A reference can be created by using a special syntax, lovingly known as
337 the *foo{THING} syntax. *foo{THING} returns a reference to the THING
338 slot in *foo (which is the symbol table entry which holds everything
339 known as foo).
340 .PP
341 .Vb 9
342 \& $scalarref = *foo{SCALAR};
343 \& $arrayref = *ARGV{ARRAY};
344 \& $hashref = *ENV{HASH};
345 \& $coderef = *handler{CODE};
346 \& $ioref = *STDIN{IO};
347 \& $globref = *foo{GLOB};
348 \& $formatref = *foo{FORMAT};
349 \& $globname = *foo{NAME}; # "foo"
350 \& $pkgname = *foo{PACKAGE}; # "main"
351 .Ve
352 .PP
353 Most of these are self\-explanatory, but \f(CW*foo{IO}\fR
354 deserves special attention. It returns
355 the IO handle, used for file handles ("open" in perlfunc), sockets
356 ("socket" in perlfunc and "socketpair" in perlfunc), and directory
357 handles ("opendir" in perlfunc). For compatibility with previous
358 versions of Perl, \f(CW*foo{FILEHANDLE}\fR is a synonym for \f(CW*foo{IO}\fR, though it
359 is discouraged, to encourage a consistent use of one name: IO. On perls
360 between v5.8 and v5.22, it will issue a deprecation warning, but this
361 deprecation has since been rescinded.
362 .PP
363 \&\f(CW*foo{THING}\fR returns undef if that particular THING hasn\*(Aqt been used yet,
364 except in the case of scalars. \f(CW*foo{SCALAR}\fR returns a reference to an
365 anonymous scalar if \f(CW$foo\fR hasn\*(Aqt been used yet. This might change in a
366 future release.
367 .PP
368 \&\f(CW*foo{NAME}\fR and \f(CW*foo{PACKAGE}\fR are the exception, in that they return
369 strings, rather than references. These return the package and name of the
370 typeglob itself, rather than one that has been assigned to it. So, after
371 \&\f(CW\*(C`*foo=*Foo::bar\*(C'\fR, \f(CW*foo\fR will become "*Foo::bar" when used as a string,
372 but \f(CW*foo{PACKAGE}\fR and \f(CW*foo{NAME}\fR will continue to produce "main" and
373 "foo", respectively.
374 .PP
375 \&\f(CW*foo{IO}\fR is an alternative to the \f(CW*HANDLE\fR mechanism given in
376 "Typeglobs and Filehandles" in perldata for passing filehandles
377 into or out of subroutines, or storing into larger data structures.
378 Its disadvantage is that it won\*(Aqt create a new filehandle for you.
379 Its advantage is that you have less risk of clobbering more than
380 you want to with a typeglob assignment. (It still conflates file
381 and directory handles, though.) However, if you assign the incoming
382 value to a scalar instead of a typeglob as we do in the examples
383 below, there\*(Aqs no risk of that happening.
384 .PP
385 .Vb 2
386 \& splutter(*STDOUT); # pass the whole glob
387 \& splutter(*STDOUT{IO}); # pass both file and dir handles
388 \&
389 \& sub splutter {
390 \& my $fh = shift;
391 \& print $fh "her um well a hmmm\en";
392 \& }
393 \&
394 \& $rec = get_rec(*STDIN); # pass the whole glob
395 \& $rec = get_rec(*STDIN{IO}); # pass both file and dir handles
396 \&
397 \& sub get_rec {
398 \& my $fh = shift;
399 \& return scalar <$fh>;
400 \& }
401 .Ve
402 .SS "Using References"
403 .IX Xref "reference, use dereferencing dereference"
404 .IX Subsection "Using References"
405 That\*(Aqs it for creating references. By now you\*(Aqre probably dying to
406 know how to use references to get back to your long\-lost data. There
407 are several basic methods.
408 .PP
409 \fISimple Scalar\fR
410 .IX Subsection "Simple Scalar"
411 .PP
412 Anywhere you\*(Aqd put an identifier (or chain of identifiers) as part
413 of a variable or subroutine name, you can replace the identifier with
414 a simple scalar variable containing a reference of the correct type:
415 .PP
416 .Vb 6
417 \& $bar = $$scalarref;
418 \& push(@$arrayref, $filename);
419 \& $$arrayref[0] = "January";
420 \& $$hashref{"KEY"} = "VALUE";
421 \& &$coderef(1,2,3);
422 \& print $globref "output\en";
423 .Ve
424 .PP
425 It\*(Aqs important to understand that we are specifically \fInot\fR dereferencing
426 \&\f(CW$arrayref[0]\fR or \f(CW$hashref{"KEY"}\fR there. The dereference of the
427 scalar variable happens \fIbefore\fR it does any key lookups. Anything more
428 complicated than a simple scalar variable must use methods 2 or 3 below.
429 However, a "simple scalar" includes an identifier that itself uses method
430 1 recursively. Therefore, the following prints "howdy".
431 .PP
432 .Vb 2
433 \& $refrefref = \e\e\e"howdy";
434 \& print $$$$refrefref;
435 .Ve
436 .PP
437 \fIBlock\fR
438 .IX Subsection "Block"
439 .PP
440 Anywhere you\*(Aqd put an identifier (or chain of identifiers) as part of a
441 variable or subroutine name, you can replace the identifier with a
442 BLOCK returning a reference of the correct type. In other words, the
443 previous examples could be written like this:
444 .PP
445 .Vb 6
446 \& $bar = ${$scalarref};
447 \& push(@{$arrayref}, $filename);
448 \& ${$arrayref}[0] = "January";
449 \& ${$hashref}{"KEY"} = "VALUE";
450 \& &{$coderef}(1,2,3);
451 \& $globref\->print("output\en"); # iff IO::Handle is loaded
452 .Ve
453 .PP
454 Admittedly, it\*(Aqs a little silly to use the curlies in this case, but
455 the BLOCK can contain any arbitrary expression, in particular,
456 subscripted expressions:
457 .PP
458 .Vb 1
459 \& &{ $dispatch{$index} }(1,2,3); # call correct routine
460 .Ve
461 .PP
462 Because of being able to omit the curlies for the simple case of \f(CW$$x\fR,
463 people often make the mistake of viewing the dereferencing symbols as
464 proper operators, and wonder about their precedence. If they were,
465 though, you could use parentheses instead of braces. That\*(Aqs not the case.
466 Consider the difference below; case 0 is a short\-hand version of case 1,
467 \&\fInot\fR case 2:
468 .PP
469 .Vb 4
470 \& $$hashref{"KEY"} = "VALUE"; # CASE 0
471 \& ${$hashref}{"KEY"} = "VALUE"; # CASE 1
472 \& ${$hashref{"KEY"}} = "VALUE"; # CASE 2
473 \& ${$hashref\->{"KEY"}} = "VALUE"; # CASE 3
474 .Ve
475 .PP
476 Case 2 is also deceptive in that you\*(Aqre accessing a variable
477 called \f(CW%hashref\fR, not dereferencing through \f(CW$hashref\fR to the hash
478 it\*(Aqs presumably referencing. That would be case 3.
479 .PP
480 \fIArrow Notation\fR
481 .IX Subsection "Arrow Notation"
482 .PP
483 Subroutine calls and lookups of individual array elements arise often
484 enough that it gets cumbersome to use method 2. As a form of
485 syntactic sugar, the examples for method 2 may be written:
486 .PP
487 .Vb 3
488 \& $arrayref\->[0] = "January"; # Array element
489 \& $hashref\->{"KEY"} = "VALUE"; # Hash element
490 \& $coderef\->(1,2,3); # Subroutine call
491 .Ve
492 .PP
493 The left side of the arrow can be any expression returning a reference,
494 including a previous dereference. Note that \f(CW$array[$x]\fR is \fInot\fR the
495 same thing as \f(CW\*(C`$array\->[$x]\*(C'\fR here:
496 .PP
497 .Vb 1
498 \& $array[$x]\->{"foo"}\->[0] = "January";
499 .Ve
500 .PP
501 This is one of the cases we mentioned earlier in which references could
502 spring into existence when in an lvalue context. Before this
503 statement, \f(CW$array[$x]\fR may have been undefined. If so, it\*(Aqs
504 automatically defined with a hash reference so that we can look up
505 \&\f(CW\*(C`{"foo"}\*(C'\fR in it. Likewise \f(CW\*(C`$array[$x]\->{"foo"}\*(C'\fR will automatically get
506 defined with an array reference so that we can look up \f(CW\*(C`[0]\*(C'\fR in it.
507 This process is called \fIautovivification\fR.
508 .PP
509 One more thing here. The arrow is optional \fIbetween\fR brackets
510 subscripts, so you can shrink the above down to
511 .PP
512 .Vb 1
513 \& $array[$x]{"foo"}[0] = "January";
514 .Ve
515 .PP
516 Which, in the degenerate case of using only ordinary arrays, gives you
517 multidimensional arrays just like C\*(Aqs:
518 .PP
519 .Vb 1
520 \& $score[$x][$y][$z] += 42;
521 .Ve
522 .PP
523 Well, okay, not entirely like C\*(Aqs arrays, actually. C doesn\*(Aqt know how
524 to grow its arrays on demand. Perl does.
525 .PP
526 \fIObjects\fR
527 .IX Subsection "Objects"
528 .PP
529 If a reference happens to be a reference to an object, then there are
530 probably methods to access the things referred to, and you should probably
531 stick to those methods unless you\*(Aqre in the class package that defines the
532 object\*(Aqs methods. In other words, be nice, and don\*(Aqt violate the object\*(Aqs
533 encapsulation without a very good reason. Perl does not enforce
534 encapsulation. We are not totalitarians here. We do expect some basic
535 civility though.
536 .PP
537 \fIMiscellaneous Usage\fR
538 .IX Subsection "Miscellaneous Usage"
539 .PP
540 Using a string or number as a reference produces a symbolic reference,
541 as explained above. Using a reference as a number produces an
542 integer representing its storage location in memory. The only
543 useful thing to be done with this is to compare two references
544 numerically to see whether they refer to the same location.
545 .IX Xref "reference, numeric context"
546 .PP
547 .Vb 3
548 \& if ($ref1 == $ref2) { # cheap numeric compare of references
549 \& print "refs 1 and 2 refer to the same thing\en";
550 \& }
551 .Ve
552 .PP
553 Using a reference as a string produces both its referent\*(Aqs type,
554 including any package blessing as described in perlobj, as well
555 as the numeric address expressed in hex. The \fBref()\fR operator returns
556 just the type of thing the reference is pointing to, without the
557 address. See "ref" in perlfunc for details and examples of its use.
558 .IX Xref "reference, string context"
559 .PP
560 The \fBbless()\fR operator may be used to associate the object a reference
561 points to with a package functioning as an object class. See perlobj.
562 .PP
563 A typeglob may be dereferenced the same way a reference can, because
564 the dereference syntax always indicates the type of reference desired.
565 So \f(CW\*(C`${*foo}\*(C'\fR and \f(CW\*(C`${\e$foo}\*(C'\fR both indicate the same scalar variable.
566 .PP
567 Here\*(Aqs a trick for interpolating a subroutine call into a string:
568 .PP
569 .Vb 1
570 \& print "My sub returned @{[mysub(1,2,3)]} that time.\en";
571 .Ve
572 .PP
573 The way it works is that when the \f(CW\*(C`@{...}\*(C'\fR is seen in the double\-quoted
574 string, it\*(Aqs evaluated as a block. The block creates a reference to an
575 anonymous array containing the results of the call to \f(CW\*(C`mysub(1,2,3)\*(C'\fR. So
576 the whole block returns a reference to an array, which is then
577 dereferenced by \f(CW\*(C`@{...}\*(C'\fR and stuck into the double\-quoted string. This
578 chicanery is also useful for arbitrary expressions:
579 .PP
580 .Vb 1
581 \& print "That yields @{[$n + 5]} widgets\en";
582 .Ve
583 .PP
584 Similarly, an expression that returns a reference to a scalar can be
585 dereferenced via \f(CW\*(C`${...}\*(C'\fR. Thus, the above expression may be written
586 as:
587 .PP
588 .Vb 1
589 \& print "That yields ${\e($n + 5)} widgets\en";
590 .Ve
591 .SS "Circular References"
592 .IX Xref "circular reference reference, circular"
593 .IX Subsection "Circular References"
594 It is possible to create a "circular reference" in Perl, which can lead
595 to memory leaks. A circular reference occurs when two references
596 contain a reference to each other, like this:
597 .PP
598 .Vb 3
599 \& my $foo = {};
600 \& my $bar = { foo => $foo };
601 \& $foo\->{bar} = $bar;
602 .Ve
603 .PP
604 You can also create a circular reference with a single variable:
605 .PP
606 .Vb 2
607 \& my $foo;
608 \& $foo = \e$foo;
609 .Ve
610 .PP
611 In this case, the reference count for the variables will never reach 0,
612 and the references will never be garbage\-collected. This can lead to
613 memory leaks.
614 .PP
615 Because objects in Perl are implemented as references, it\*(Aqs possible to
616 have circular references with objects as well. Imagine a TreeNode class
617 where each node references its parent and child nodes. Any node with a
618 parent will be part of a circular reference.
619 .PP
620 You can break circular references by creating a "weak reference". A
621 weak reference does not increment the reference count for a variable,
622 which means that the object can go out of scope and be destroyed. You
623 can weaken a reference with the \f(CW\*(C`weaken\*(C'\fR function exported by the
624 Scalar::Util module, or available as \f(CW\*(C`builtin::weaken\*(C'\fR directly in
625 Perl version 5.35.7 or later.
626 .PP
627 Here\*(Aqs how we can make the first example safer:
628 .PP
629 .Vb 1
630 \& use Scalar::Util \*(Aqweaken\*(Aq;
631 \&
632 \& my $foo = {};
633 \& my $bar = { foo => $foo };
634 \& $foo\->{bar} = $bar;
635 \&
636 \& weaken $foo\->{bar};
637 .Ve
638 .PP
639 The reference from \f(CW$foo\fR to \f(CW$bar\fR has been weakened. When the
640 \&\f(CW$bar\fR variable goes out of scope, it will be garbage\-collected. The
641 next time you look at the value of the \f(CW\*(C`$foo\->{bar}\*(C'\fR key, it will
642 be \f(CW\*(C`undef\*(C'\fR.
643 .PP
644 This action at a distance can be confusing, so you should be careful
645 with your use of weaken. You should weaken the reference in the
646 variable that will go out of scope \fIfirst\fR. That way, the longer\-lived
647 variable will contain the expected reference until it goes out of
648 scope.
649 .SS "Symbolic references"
650 .IX Xref "reference, symbolic reference, soft symbolic reference soft reference"
651 .IX Subsection "Symbolic references"
652 We said that references spring into existence as necessary if they are
653 undefined, but we didn\*(Aqt say what happens if a value used as a
654 reference is already defined, but \fIisn\*(Aqt\fR a hard reference. If you
655 use it as a reference, it\*(Aqll be treated as a symbolic
656 reference. That is, the value of the scalar is taken to be the \fIname\fR
657 of a variable, rather than a direct link to a (possibly) anonymous
658 value.
659 .PP
660 People frequently expect it to work like this. So it does.
661 .PP
662 .Vb 9
663 \& $name = "foo";
664 \& $$name = 1; # Sets $foo
665 \& ${$name} = 2; # Sets $foo
666 \& ${$name x 2} = 3; # Sets $foofoo
667 \& $name\->[0] = 4; # Sets $foo[0]
668 \& @$name = (); # Clears @foo
669 \& &$name(); # Calls &foo()
670 \& $pack = "THAT";
671 \& ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval
672 .Ve
673 .PP
674 This is powerful, and slightly dangerous, in that it\*(Aqs possible
675 to intend (with the utmost sincerity) to use a hard reference, and
676 accidentally use a symbolic reference instead. To protect against
677 that, you can say
678 .PP
679 .Vb 1
680 \& use strict \*(Aqrefs\*(Aq;
681 .Ve
682 .PP
683 and then only hard references will be allowed for the rest of the enclosing
684 block. An inner block may countermand that with
685 .PP
686 .Vb 1
687 \& no strict \*(Aqrefs\*(Aq;
688 .Ve
689 .PP
690 Only package variables (globals, even if localized) are visible to
691 symbolic references. Lexical variables (declared with \fBmy()\fR) aren\*(Aqt in
692 a symbol table, and thus are invisible to this mechanism. For example:
693 .PP
694 .Vb 6
695 \& local $value = 10;
696 \& $ref = "value";
697 \& {
698 \& my $value = 20;
699 \& print $$ref;
700 \& }
701 .Ve
702 .PP
703 This will still print 10, not 20. Remember that \fBlocal()\fR affects package
704 variables, which are all "global" to the package.
705 .SS "Not\-so\-symbolic references"
706 .IX Subsection "Not-so-symbolic references"
707 Brackets around a symbolic reference can simply
708 serve to isolate an identifier or variable name from the rest of an
709 expression, just as they always have within a string. For example,
710 .PP
711 .Vb 2
712 \& $push = "pop on ";
713 \& print "${push}over";
714 .Ve
715 .PP
716 has always meant to print "pop on over", even though push is
717 a reserved word. This is generalized to work the same
718 without the enclosing double quotes, so that
719 .PP
720 .Vb 1
721 \& print ${push} . "over";
722 .Ve
723 .PP
724 and even
725 .PP
726 .Vb 1
727 \& print ${ push } . "over";
728 .Ve
729 .PP
730 will have the same effect. This
731 construct is \fInot\fR considered to be a symbolic reference when you\*(Aqre
732 using strict refs:
733 .PP
734 .Vb 3
735 \& use strict \*(Aqrefs\*(Aq;
736 \& ${ bareword }; # Okay, means $bareword.
737 \& ${ "bareword" }; # Error, symbolic reference.
738 .Ve
739 .PP
740 Similarly, because of all the subscripting that is done using single words,
741 the same rule applies to any bareword that is used for subscripting a hash.
742 So now, instead of writing
743 .PP
744 .Vb 1
745 \& $hash{ "aaa" }{ "bbb" }{ "ccc" }
746 .Ve
747 .PP
748 you can write just
749 .PP
750 .Vb 1
751 \& $hash{ aaa }{ bbb }{ ccc }
752 .Ve
753 .PP
754 and not worry about whether the subscripts are reserved words. In the
755 rare event that you do wish to do something like
756 .PP
757 .Vb 1
758 \& $hash{ shift }
759 .Ve
760 .PP
761 you can force interpretation as a reserved word by adding anything that
762 makes it more than a bareword:
763 .PP
764 .Vb 3
765 \& $hash{ shift() }
766 \& $hash{ +shift }
767 \& $hash{ shift @_ }
768 .Ve
769 .PP
770 The \f(CW\*(C`use warnings\*(C'\fR pragma or the \fB\-w\fR switch will warn you if it
771 interprets a reserved word as a string.
772 But it will no longer warn you about using lowercase words, because the
773 string is effectively quoted.
774 .SS "Pseudo\-hashes: Using an array as a hash"
775 .IX Xref "pseudo-hash pseudo hash pseudohash"
776 .IX Subsection "Pseudo-hashes: Using an array as a hash"
777 Pseudo\-hashes have been removed from Perl. The fields pragma
778 remains available.
779 .SS "Function Templates"
780 .IX Xref "scope, lexical closure lexical lexical scope subroutine, nested sub, nested subroutine, local sub, local"
781 .IX Subsection "Function Templates"
782 As explained above, an anonymous function with access to the lexical
783 variables visible when that function was compiled, creates a closure. It
784 retains access to those variables even though it doesn\*(Aqt get run until
785 later, such as in a signal handler or a Tk callback.
786 .PP
787 Using a closure as a function template allows us to generate many functions
788 that act similarly. Suppose you wanted functions named after the colors
789 that generated HTML font changes for the various colors:
790 .PP
791 .Vb 1
792 \& print "Be ", red("careful"), "with that ", green("light");
793 .Ve
794 .PP
795 The \fBred()\fR and \fBgreen()\fR functions would be similar. To create these,
796 we\*(Aqll assign a closure to a typeglob of the name of the function we\*(Aqre
797 trying to build.
798 .PP
799 .Vb 5
800 \& @colors = qw(red blue green yellow orange purple violet);
801 \& for my $name (@colors) {
802 \& no strict \*(Aqrefs\*(Aq; # allow symbol table manipulation
803 \& *$name = *{uc $name} = sub { "<FONT COLOR=\*(Aq$name\*(Aq>@_</FONT>" };
804 \& }
805 .Ve
806 .PP
807 Now all those different functions appear to exist independently. You can
808 call \fBred()\fR, \fBRED()\fR, \fBblue()\fR, \fBBLUE()\fR, \fBgreen()\fR, etc. This technique saves on
809 both compile time and memory use, and is less error\-prone as well, since
810 syntax checks happen at compile time. It\*(Aqs critical that any variables in
811 the anonymous subroutine be lexicals in order to create a proper closure.
812 That\*(Aqs the reasons for the \f(CW\*(C`my\*(C'\fR on the loop iteration variable.
813 .PP
814 This is one of the only places where giving a prototype to a closure makes
815 much sense. If you wanted to impose scalar context on the arguments of
816 these functions (probably not a wise idea for this particular example),
817 you could have written it this way instead:
818 .PP
819 .Vb 1
820 \& *$name = sub ($) { "<FONT COLOR=\*(Aq$name\*(Aq>$_[0]</FONT>" };
821 .Ve
822 .PP
823 However, since prototype checking happens at compile time, the assignment
824 above happens too late to be of much use. You could address this by
825 putting the whole loop of assignments within a BEGIN block, forcing it
826 to occur during compilation.
827 .PP
828 Access to lexicals that change over time\-\-like those in the \f(CW\*(C`for\*(C'\fR loop
829 above, basically aliases to elements from the surrounding lexical scopes\-\-
830 only works with anonymous subs, not with named subroutines. Generally
831 said, named subroutines do not nest properly and should only be declared
832 in the main package scope.
833 .PP
834 This is because named subroutines are created at compile time so their
835 lexical variables get assigned to the parent lexicals from the first
836 execution of the parent block. If a parent scope is entered a second
837 time, its lexicals are created again, while the nested subs still
838 reference the old ones.
839 .PP
840 Anonymous subroutines get to capture each time you execute the \f(CW\*(C`sub\*(C'\fR
841 operator, as they are created on the fly. If you are accustomed to using
842 nested subroutines in other programming languages with their own private
843 variables, you\*(Aqll have to work at it a bit in Perl. The intuitive coding
844 of this type of thing incurs mysterious warnings about "will not stay
845 shared" due to the reasons explained above.
846 For example, this won\*(Aqt work:
847 .PP
848 .Vb 5
849 \& sub outer {
850 \& my $x = $_[0] + 35;
851 \& sub inner { return $x * 19 } # WRONG
852 \& return $x + inner();
853 \& }
854 .Ve
855 .PP
856 A work\-around is the following:
857 .PP
858 .Vb 5
859 \& sub outer {
860 \& my $x = $_[0] + 35;
861 \& local *inner = sub { return $x * 19 };
862 \& return $x + inner();
863 \& }
864 .Ve
865 .PP
866 Now \fBinner()\fR can only be called from within \fBouter()\fR, because of the
867 temporary assignments of the anonymous subroutine. But when it does,
868 it has normal access to the lexical variable \f(CW$x\fR from the scope of
869 \&\fBouter()\fR at the time outer is invoked.
870 .PP
871 This has the interesting effect of creating a function local to another
872 function, something not normally supported in Perl.
873 .SS "Postfix Dereference Syntax"
874 .IX Subsection "Postfix Dereference Syntax"
875 Beginning in v5.20.0, a postfix syntax for using references is
876 available. It behaves as described in "Using References", but instead
877 of a prefixed sigil, a postfixed sigil\-and\-star is used.
878 .PP
879 For example:
880 .PP
881 .Vb 2
882 \& $r = \e@a;
883 \& @b = $r\->@*; # equivalent to @$r or @{ $r }
884 \&
885 \& $r = [ 1, [ 2, 3 ], 4 ];
886 \& $r\->[1]\->@*; # equivalent to @{ $r\->[1] }
887 \&
888 \& $aref\->@* = (1, 2, 3); # same as @{ $aref } = (1, 2, 3)
889 .Ve
890 .PP
891 In Perl 5.20 and 5.22, this syntax must be enabled with \f(CWuse feature
892 \&\*(Aqpostderef\*(Aq\fR. As of Perl 5.24, no feature declarations are required to make
893 it available.
894 .PP
895 Postfix dereference should work in all circumstances where block
896 (circumfix) dereference worked, and should be entirely equivalent. This
897 syntax allows dereferencing to be written and read entirely
898 left\-to\-right. The following equivalencies are defined:
899 .PP
900 .Vb 6
901 \& $sref\->$*; # same as ${ $sref }
902 \& $aref\->@*; # same as @{ $aref }
903 \& $aref\->$#*; # same as $#{ $aref }
904 \& $href\->%*; # same as %{ $href }
905 \& $cref\->&*; # same as &{ $cref }
906 \& $gref\->**; # same as *{ $gref }
907 .Ve
908 .PP
909 Note especially that \f(CW\*(C`$cref\->&*\*(C'\fR is \fInot\fR equivalent to \f(CW$cref\->()\fR, and can serve different purposes.
910 .PP
911 Glob elements can be extracted through the postfix dereferencing feature:
912 .PP
913 .Vb 1
914 \& $gref\->*{SCALAR}; # same as *{ $gref }{SCALAR}
915 .Ve
916 .PP
917 Postfix array and scalar dereferencing \fIcan\fR be used in interpolating
918 strings (double quotes or the \f(CW\*(C`qq\*(C'\fR operator), but only if the
919 \&\f(CW\*(C`postderef_qq\*(C'\fR feature is enabled. Interpolation of postfix array highest index
920 access (\f(CW\*(C`\->$#*\*(C'\fR) is also supported when the \f(CW\*(C`postderef_qq\*(C'\fR feature is
921 enabled.
922 .SS "Postfix Reference Slicing"
923 .IX Subsection "Postfix Reference Slicing"
924 Value slices of arrays and hashes may also be taken with postfix
925 dereferencing notation, with the following equivalencies:
926 .PP
927 .Vb 2
928 \& $aref\->@[ ... ]; # same as @$aref[ ... ]
929 \& $href\->@{ ... }; # same as @$href{ ... }
930 .Ve
931 .PP
932 Postfix key/value pair slicing, added in 5.20.0 and documented in
933 the Key/Value Hash Slices section of perldata, also behaves as expected:
934 .PP
935 .Vb 2
936 \& $aref\->%[ ... ]; # same as %$aref[ ... ]
937 \& $href\->%{ ... }; # same as %$href{ ... }
938 .Ve
939 .PP
940 As with postfix array, postfix value slice dereferencing \fIcan\fR be used
941 in interpolating strings (double quotes or the \f(CW\*(C`qq\*(C'\fR operator), but only
942 if the \f(CW\*(C`postderef_qq\*(C'\fR feature is enabled.
943 .SS "Assigning to References"
944 .IX Subsection "Assigning to References"
945 Beginning in v5.22.0, the referencing operator can be assigned to. It
946 performs an aliasing operation, so that the variable name referenced on the
947 left\-hand side becomes an alias for the thing referenced on the right\-hand
948 side:
949 .PP
950 .Vb 2
951 \& \e$x = \e$y; # $x and $y now point to the same scalar
952 \& \e&foo = \e&bar; # foo() now means bar()
953 .Ve
954 .PP
955 This syntax must be enabled with \f(CW\*(C`use feature \*(Aqrefaliasing\*(Aq\*(C'\fR. It is
956 experimental, and will warn by default unless \f(CWno warnings
957 \&\*(Aqexperimental::refaliasing\*(Aq\fR is in effect.
958 .PP
959 These forms may be assigned to, and cause the right\-hand side to be
960 evaluated in scalar context:
961 .PP
962 .Vb 10
963 \& \e$scalar
964 \& \e@array
965 \& \e%hash
966 \& \e&sub
967 \& \emy $scalar
968 \& \emy @array
969 \& \emy %hash
970 \& \estate $scalar # or @array, etc.
971 \& \eour $scalar # etc.
972 \& \elocal $scalar # etc.
973 \& \elocal our $scalar # etc.
974 \& \e$some_array[$index]
975 \& \e$some_hash{$key}
976 \& \elocal $some_array[$index]
977 \& \elocal $some_hash{$key}
978 \& condition ? \e$this : \e$that[0] # etc.
979 .Ve
980 .PP
981 Slicing operations and parentheses cause
982 the right\-hand side to be evaluated in
983 list context:
984 .PP
985 .Vb 10
986 \& \e@array[5..7]
987 \& (\e@array[5..7])
988 \& \e(@array[5..7])
989 \& \e@hash{\*(Aqfoo\*(Aq,\*(Aqbar\*(Aq}
990 \& (\e@hash{\*(Aqfoo\*(Aq,\*(Aqbar\*(Aq})
991 \& \e(@hash{\*(Aqfoo\*(Aq,\*(Aqbar\*(Aq})
992 \& (\e$scalar)
993 \& \e($scalar)
994 \& \e(my $scalar)
995 \& \emy($scalar)
996 \& (\e@array)
997 \& (\e%hash)
998 \& (\e&sub)
999 \& \e(&sub)
1000 \& \e($foo, @bar, %baz)
1001 \& (\e$foo, \e@bar, \e%baz)
1002 .Ve
1003 .PP
1004 Each element on the right\-hand side must be a reference to a datum of the
1005 right type. Parentheses immediately surrounding an array (and possibly
1006 also \f(CW\*(C`my\*(C'\fR/\f(CW\*(C`state\*(C'\fR/\f(CW\*(C`our\*(C'\fR/\f(CW\*(C`local\*(C'\fR) will make each element of the array an
1007 alias to the corresponding scalar referenced on the right\-hand side:
1008 .PP
1009 .Vb 5
1010 \& \e(@x) = \e(@y); # @x and @y now have the same elements
1011 \& \emy(@x) = \e(@y); # likewise
1012 \& \e(my @x) = \e(@y); # likewise
1013 \& push @x, 3; # but now @x has an extra element that @y lacks
1014 \& \e(@x) = (\e$x, \e$y, \e$z); # @x now contains $x, $y, and $z
1015 .Ve
1016 .PP
1017 Combining that form with \f(CW\*(C`local\*(C'\fR and putting parentheses immediately
1018 around a hash are forbidden (because it is not clear what they should do):
1019 .PP
1020 .Vb 2
1021 \& \elocal(@array) = foo(); # WRONG
1022 \& \e(%hash) = bar(); # WRONG
1023 .Ve
1024 .PP
1025 Assignment to references and non\-references may be combined in lists and
1026 conditional ternary expressions, as long as the values on the right\-hand
1027 side are the right type for each element on the left, though this may make
1028 for obfuscated code:
1029 .PP
1030 .Vb 4
1031 \& (my $tom, \emy $dick, \emy @harry) = (\e1, \e2, [1..3]);
1032 \& # $tom is now \e1
1033 \& # $dick is now 2 (read\-only)
1034 \& # @harry is (1,2,3)
1035 \&
1036 \& my $type = ref $thingy;
1037 \& ($type ? $type eq \*(AqARRAY\*(Aq ? \e@foo : \e$bar : $baz) = $thingy;
1038 .Ve
1039 .PP
1040 A reference assignment in list context returns a list of references to
1041 each left\-hand value. So for example
1042 .PP
1043 .Vb 1
1044 \& @b = ((\e$l1, \e$l2, \e(@a)) = (\e$r1, \e$r2, $\er3, \e$4));
1045 .Ve
1046 .PP
1047 is equivalent to
1048 .PP
1049 .Vb 2
1050 \& (\e$l1, \e$l2, \e(@a)) = (\e$r1, \e$r2, $\er3, \e$4);
1051 \& @b = (\e$l1, \e$l2, \e$a[0], \e$a[1]);
1052 .Ve
1053 .PP
1054 The \f(CW\*(C`foreach\*(C'\fR loop can also take a reference constructor for its loop
1055 variable, though the syntax is limited to one of the following, with an
1056 optional \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`state\*(C'\fR, or \f(CW\*(C`our\*(C'\fR after the backslash:
1057 .PP
1058 .Vb 4
1059 \& \e$s
1060 \& \e@a
1061 \& \e%h
1062 \& \e&c
1063 .Ve
1064 .PP
1065 No parentheses are permitted. This feature is particularly useful for
1066 arrays\-of\-arrays, or arrays\-of\-hashes:
1067 .PP
1068 .Vb 3
1069 \& foreach \emy @a (@array_of_arrays) {
1070 \& frobnicate($a[0], $a[\-1]);
1071 \& }
1072 \&
1073 \& foreach \emy %h (@array_of_hashes) {
1074 \& $h{gelastic}++ if $h{type} eq \*(Aqfunny\*(Aq;
1075 \& }
1076 .Ve
1077 .PP
1078 \&\fBCAVEAT:\fR Aliasing does not work correctly with closures. If you try to
1079 alias lexical variables from an inner subroutine or \f(CW\*(C`eval\*(C'\fR, the aliasing
1080 will only be visible within that inner sub, and will not affect the outer
1081 subroutine where the variables are declared. This bizarre behavior is
1082 subject to change.
1083 .SS "Declaring a Reference to a Variable"
1084 .IX Subsection "Declaring a Reference to a Variable"
1085 Beginning in v5.26.0, the referencing operator can come after \f(CW\*(C`my\*(C'\fR,
1086 \&\f(CW\*(C`state\*(C'\fR, \f(CW\*(C`our\*(C'\fR, or \f(CW\*(C`local\*(C'\fR. This syntax must be enabled with \f(CW\*(C`use
1087 feature \*(Aqdeclared_refs\*(Aq\*(C'\fR. It is experimental, and will warn by default
1088 unless \f(CW\*(C`no warnings \*(Aqexperimental::refaliasing\*(Aq\*(C'\fR is in effect.
1089 .PP
1090 This feature makes these:
1091 .PP
1092 .Vb 2
1093 \& my \e$x;
1094 \& our \e$y;
1095 .Ve
1096 .PP
1097 equivalent to:
1098 .PP
1099 .Vb 2
1100 \& \emy $x;
1101 \& \eour $x;
1102 .Ve
1103 .PP
1104 It is intended mainly for use in assignments to references (see
1105 "Assigning to References", above). It also allows the backslash to be
1106 used on just some items in a list of declared variables:
1107 .PP
1108 .Vb 1
1109 \& my ($foo, \e@bar, \e%baz); # equivalent to: my $foo, \emy(@bar, %baz);
1110 .Ve
1111 .SH "WARNING: Don\*(Aqt use references as hash keys"
1112 .IX Xref "reference, string context reference, use as hash key"
1113 .IX Header "WARNING: Don't use references as hash keys"
1114 You may not (usefully) use a reference as the key to a hash. It will be
1115 converted into a string:
1116 .PP
1117 .Vb 1
1118 \& $x{ \e$x } = $x;
1119 .Ve
1120 .PP
1121 If you try to dereference the key, it won\*(Aqt do a hard dereference, and
1122 you won\*(Aqt accomplish what you\*(Aqre attempting. You might want to do something
1123 more like
1124 .PP
1125 .Vb 2
1126 \& $r = \e@a;
1127 \& $x{ $r } = $r;
1128 .Ve
1129 .PP
1130 And then at least you can use the \fBvalues()\fR, which will be
1131 real refs, instead of the \fBkeys()\fR, which won\*(Aqt.
1132 .PP
1133 The standard Tie::RefHash module provides a convenient workaround to this.
1134 .SH "SEE ALSO"
1135 .IX Header "SEE ALSO"
1136 Besides the obvious documents, source code can be instructive.
1137 Some pathological examples of the use of references can be found
1138 in the \fIt/op/ref.t\fR regression test in the Perl source directory.
1139 .PP
1140 See also perldsc and perllol for how to use references to create
1141 complex data structures, and perlootut and perlobj
1142 for how to use them to create objects.