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