Commit | Line | Data |
a0d0e21e |
1 | =head1 NAME |
2 | |
3 | perlref - Perl references and nested data structures |
4 | |
5 | =head1 DESCRIPTION |
6 | |
cb1a09d0 |
7 | Before release 5 of Perl it was difficult to represent complex data |
8 | structures, because all references had to be symbolic, and even that was |
9 | difficult to do when you wanted to refer to a variable rather than a |
5f05dabc |
10 | symbol table entry. Perl not only makes it easier to use symbolic |
cb1a09d0 |
11 | references to variables, but lets you have "hard" references to any piece |
5f05dabc |
12 | of data. Any scalar may hold a hard reference. Because arrays and hashes |
cb1a09d0 |
13 | contain scalars, you can now easily build arrays of arrays, arrays of |
14 | hashes, hashes of arrays, arrays of hashes of functions, and so on. |
a0d0e21e |
15 | |
16 | Hard references are smart--they keep track of reference counts for you, |
17 | automatically freeing the thing referred to when its reference count |
6309d9d9 |
18 | goes to zero. (Note: The reference counts for values in self-referential |
19 | or cyclic data structures may not go to zero without a little help; see |
20 | L<perlobj/"Two-Phased Garbage Collection"> for a detailed explanation. |
21 | If that thing happens to be an object, the object is |
a0d0e21e |
22 | destructed. See L<perlobj> for more about objects. (In a sense, |
23 | everything in Perl is an object, but we usually reserve the word for |
24 | references to objects that have been officially "blessed" into a class package.) |
25 | |
6309d9d9 |
26 | |
a0d0e21e |
27 | A symbolic reference contains the name of a variable, just as a |
5f05dabc |
28 | symbolic link in the filesystem contains merely the name of a file. |
a0d0e21e |
29 | The C<*glob> notation is a kind of symbolic reference. Hard references |
30 | are more like hard links in the file system: merely another way |
31 | at getting at the same underlying object, irrespective of its name. |
32 | |
33 | "Hard" references are easy to use in Perl. There is just one |
34 | overriding principle: Perl does no implicit referencing or |
35 | dereferencing. When a scalar is holding a reference, it always behaves |
36 | as a scalar. It doesn't magically start being an array or a hash |
37 | unless you tell it so explicitly by dereferencing it. |
38 | |
39 | References can be constructed several ways. |
40 | |
41 | =over 4 |
42 | |
43 | =item 1. |
44 | |
45 | By using the backslash operator on a variable, subroutine, or value. |
46 | (This works much like the & (address-of) operator works in C.) Note |
5f05dabc |
47 | that this typically creates I<ANOTHER> reference to a variable, because |
a0d0e21e |
48 | there's already a reference to the variable in the symbol table. But |
49 | the symbol table reference might go away, and you'll still have the |
50 | reference that the backslash returned. Here are some examples: |
51 | |
52 | $scalarref = \$foo; |
53 | $arrayref = \@ARGV; |
54 | $hashref = \%ENV; |
55 | $coderef = \&handler; |
55497cff |
56 | $globref = \*foo; |
cb1a09d0 |
57 | |
5f05dabc |
58 | It isn't possible to create a true reference to an IO handle (filehandle or |
36477c24 |
59 | dirhandle) using the backslash operator. See the explanation of the |
5f05dabc |
60 | *foo{THING} syntax below. (However, you're apt to find Perl code |
61 | out there using globrefs as though they were IO handles, which is |
62 | grandfathered into continued functioning.) |
a0d0e21e |
63 | |
64 | =item 2. |
65 | |
66 | A reference to an anonymous array can be constructed using square |
67 | brackets: |
68 | |
69 | $arrayref = [1, 2, ['a', 'b', 'c']]; |
70 | |
71 | Here we've constructed a reference to an anonymous array of three elements |
72 | whose final element is itself reference to another anonymous array of three |
73 | elements. (The multidimensional syntax described later can be used to |
184e9718 |
74 | access this. For example, after the above, C<$arrayref-E<gt>[2][1]> would have |
a0d0e21e |
75 | the value "b".) |
76 | |
cb1a09d0 |
77 | Note that taking a reference to an enumerated list is not the same |
78 | as using square brackets--instead it's the same as creating |
79 | a list of references! |
80 | |
58e0a6ae |
81 | @list = (\$a, \@b, \%c); |
82 | @list = \($a, @b, %c); # same thing! |
83 | |
84 | As a special case, C<\(@foo)> returns a list of references to the contents |
85 | of C<@foo>, not a reference to C<@foo> itself. Likewise for C<%foo>. |
cb1a09d0 |
86 | |
a0d0e21e |
87 | =item 3. |
88 | |
89 | A reference to an anonymous hash can be constructed using curly |
90 | brackets: |
91 | |
92 | $hashref = { |
93 | 'Adam' => 'Eve', |
94 | 'Clyde' => 'Bonnie', |
95 | }; |
96 | |
97 | Anonymous hash and array constructors can be intermixed freely to |
98 | produce as complicated a structure as you want. The multidimensional |
99 | syntax described below works for these too. The values above are |
100 | literals, but variables and expressions would work just as well, because |
101 | assignment operators in Perl (even within local() or my()) are executable |
102 | statements, not compile-time declarations. |
103 | |
104 | Because curly brackets (braces) are used for several other things |
105 | including BLOCKs, you may occasionally have to disambiguate braces at the |
106 | beginning of a statement by putting a C<+> or a C<return> in front so |
107 | that Perl realizes the opening brace isn't starting a BLOCK. The economy and |
108 | mnemonic value of using curlies is deemed worth this occasional extra |
109 | hassle. |
110 | |
111 | For example, if you wanted a function to make a new hash and return a |
112 | reference to it, you have these options: |
113 | |
114 | sub hashem { { @_ } } # silently wrong |
115 | sub hashem { +{ @_ } } # ok |
116 | sub hashem { return { @_ } } # ok |
117 | |
118 | =item 4. |
119 | |
120 | A reference to an anonymous subroutine can be constructed by using |
121 | C<sub> without a subname: |
122 | |
123 | $coderef = sub { print "Boink!\n" }; |
124 | |
125 | Note the presence of the semicolon. Except for the fact that the code |
126 | inside isn't executed immediately, a C<sub {}> is not so much a |
127 | declaration as it is an operator, like C<do{}> or C<eval{}>. (However, no |
128 | matter how many times you execute that line (unless you're in an |
129 | C<eval("...")>), C<$coderef> will still have a reference to the I<SAME> |
130 | anonymous subroutine.) |
131 | |
748a9306 |
132 | Anonymous subroutines act as closures with respect to my() variables, |
133 | that is, variables visible lexically within the current scope. Closure |
134 | is a notion out of the Lisp world that says if you define an anonymous |
135 | function in a particular lexical context, it pretends to run in that |
136 | context even when it's called outside of the context. |
137 | |
138 | In human terms, it's a funny way of passing arguments to a subroutine when |
139 | you define it as well as when you call it. It's useful for setting up |
140 | little bits of code to run later, such as callbacks. You can even |
141 | do object-oriented stuff with it, though Perl provides a different |
142 | mechanism to do that already--see L<perlobj>. |
143 | |
144 | You can also think of closure as a way to write a subroutine template without |
145 | using eval. (In fact, in version 5.000, eval was the I<only> way to get |
146 | closures. You may wish to use "require 5.001" if you use closures.) |
147 | |
148 | Here's a small example of how closures works: |
149 | |
150 | sub newprint { |
151 | my $x = shift; |
152 | return sub { my $y = shift; print "$x, $y!\n"; }; |
a0d0e21e |
153 | } |
748a9306 |
154 | $h = newprint("Howdy"); |
155 | $g = newprint("Greetings"); |
156 | |
157 | # Time passes... |
158 | |
159 | &$h("world"); |
160 | &$g("earthlings"); |
a0d0e21e |
161 | |
748a9306 |
162 | This prints |
163 | |
164 | Howdy, world! |
165 | Greetings, earthlings! |
166 | |
167 | Note particularly that $x continues to refer to the value passed into |
cb1a09d0 |
168 | newprint() I<despite> the fact that the "my $x" has seemingly gone out of |
748a9306 |
169 | scope by the time the anonymous subroutine runs. That's what closure |
170 | is all about. |
171 | |
5f05dabc |
172 | This applies to only lexical variables, by the way. Dynamic variables |
748a9306 |
173 | continue to work as they have always worked. Closure is not something |
174 | that most Perl programmers need trouble themselves about to begin with. |
a0d0e21e |
175 | |
176 | =item 5. |
177 | |
178 | References are often returned by special subroutines called constructors. |
748a9306 |
179 | Perl objects are just references to a special kind of object that happens to know |
a0d0e21e |
180 | which package it's associated with. Constructors are just special |
181 | subroutines that know how to create that association. They do so by |
182 | starting with an ordinary reference, and it remains an ordinary reference |
183 | even while it's also being an object. Constructors are customarily |
184 | named new(), but don't have to be: |
185 | |
186 | $objref = new Doggie (Tail => 'short', Ears => 'long'); |
187 | |
188 | =item 6. |
189 | |
190 | References of the appropriate type can spring into existence if you |
5f05dabc |
191 | dereference them in a context that assumes they exist. Because we haven't |
a0d0e21e |
192 | talked about dereferencing yet, we can't show you any examples yet. |
193 | |
cb1a09d0 |
194 | =item 7. |
195 | |
55497cff |
196 | A reference can be created by using a special syntax, lovingly known as |
197 | the *foo{THING} syntax. *foo{THING} returns a reference to the THING |
198 | slot in *foo (which is the symbol table entry which holds everything |
199 | known as foo). |
cb1a09d0 |
200 | |
55497cff |
201 | $scalarref = *foo{SCALAR}; |
202 | $arrayref = *ARGV{ARRAY}; |
203 | $hashref = *ENV{HASH}; |
204 | $coderef = *handler{CODE}; |
36477c24 |
205 | $ioref = *STDIN{IO}; |
55497cff |
206 | $globref = *foo{GLOB}; |
207 | |
36477c24 |
208 | All of these are self-explanatory except for *foo{IO}. It returns the |
209 | IO handle, used for file handles (L<perlfunc/open>), sockets |
210 | (L<perlfunc/socket> and L<perlfunc/socketpair>), and directory handles |
211 | (L<perlfunc/opendir>). For compatibility with previous versions of |
212 | Perl, *foo{FILEHANDLE} is a synonym for *foo{IO}. |
55497cff |
213 | |
5f05dabc |
214 | *foo{THING} returns undef if that particular THING hasn't been used yet, |
215 | except in the case of scalars. *foo{SCALAR} returns a reference to an |
216 | anonymous scalar if $foo hasn't been used yet. This might change in a |
217 | future release. |
218 | |
219 | The use of *foo{IO} is the best way to pass bareword filehandles into or |
220 | out of subroutines, or to store them in larger data structures. |
36477c24 |
221 | |
222 | splutter(*STDOUT{IO}); |
cb1a09d0 |
223 | sub splutter { |
224 | my $fh = shift; |
225 | print $fh "her um well a hmmm\n"; |
226 | } |
227 | |
36477c24 |
228 | $rec = get_rec(*STDIN{IO}); |
cb1a09d0 |
229 | sub get_rec { |
230 | my $fh = shift; |
231 | return scalar <$fh>; |
232 | } |
233 | |
5f05dabc |
234 | Beware, though, that you can't do this with a routine which is going to |
235 | open the filehandle for you, because *HANDLE{IO} will be undef if HANDLE |
236 | hasn't been used yet. Use \*HANDLE for that sort of thing instead. |
237 | |
238 | Using \*HANDLE (or *HANDLE) is another way to use and store non-bareword |
239 | filehandles (before 5.002 it was the only way). The two methods are |
240 | largely interchangeable, you can do |
241 | |
242 | splutter(\*STDOUT); |
243 | $rec = get_rec(\*STDIN); |
244 | |
245 | with the above subroutine definitions. |
55497cff |
246 | |
a0d0e21e |
247 | =back |
248 | |
249 | That's it for creating references. By now you're probably dying to |
250 | know how to use references to get back to your long-lost data. There |
251 | are several basic methods. |
252 | |
253 | =over 4 |
254 | |
255 | =item 1. |
256 | |
6309d9d9 |
257 | Anywhere you'd put an identifier (or chain of identifiers) as part |
258 | of a variable or subroutine name, you can replace the identifier with |
259 | a simple scalar variable containing a reference of the correct type: |
a0d0e21e |
260 | |
261 | $bar = $$scalarref; |
262 | push(@$arrayref, $filename); |
263 | $$arrayref[0] = "January"; |
264 | $$hashref{"KEY"} = "VALUE"; |
265 | &$coderef(1,2,3); |
cb1a09d0 |
266 | print $globref "output\n"; |
a0d0e21e |
267 | |
268 | It's important to understand that we are specifically I<NOT> dereferencing |
269 | C<$arrayref[0]> or C<$hashref{"KEY"}> there. The dereference of the |
270 | scalar variable happens I<BEFORE> it does any key lookups. Anything more |
271 | complicated than a simple scalar variable must use methods 2 or 3 below. |
272 | However, a "simple scalar" includes an identifier that itself uses method |
273 | 1 recursively. Therefore, the following prints "howdy". |
274 | |
275 | $refrefref = \\\"howdy"; |
276 | print $$$$refrefref; |
277 | |
278 | =item 2. |
279 | |
6309d9d9 |
280 | Anywhere you'd put an identifier (or chain of identifiers) as part of a |
281 | variable or subroutine name, you can replace the identifier with a |
282 | BLOCK returning a reference of the correct type. In other words, the |
283 | previous examples could be written like this: |
a0d0e21e |
284 | |
285 | $bar = ${$scalarref}; |
286 | push(@{$arrayref}, $filename); |
287 | ${$arrayref}[0] = "January"; |
288 | ${$hashref}{"KEY"} = "VALUE"; |
289 | &{$coderef}(1,2,3); |
36477c24 |
290 | $globref->print("output\n"); # iff IO::Handle is loaded |
a0d0e21e |
291 | |
292 | Admittedly, it's a little silly to use the curlies in this case, but |
293 | the BLOCK can contain any arbitrary expression, in particular, |
294 | subscripted expressions: |
295 | |
296 | &{ $dispatch{$index} }(1,2,3); # call correct routine |
297 | |
298 | Because of being able to omit the curlies for the simple case of C<$$x>, |
299 | people often make the mistake of viewing the dereferencing symbols as |
300 | proper operators, and wonder about their precedence. If they were, |
5f05dabc |
301 | though, you could use parentheses instead of braces. That's not the case. |
a0d0e21e |
302 | Consider the difference below; case 0 is a short-hand version of case 1, |
303 | I<NOT> case 2: |
304 | |
305 | $$hashref{"KEY"} = "VALUE"; # CASE 0 |
306 | ${$hashref}{"KEY"} = "VALUE"; # CASE 1 |
307 | ${$hashref{"KEY"}} = "VALUE"; # CASE 2 |
308 | ${$hashref->{"KEY"}} = "VALUE"; # CASE 3 |
309 | |
310 | Case 2 is also deceptive in that you're accessing a variable |
311 | called %hashref, not dereferencing through $hashref to the hash |
312 | it's presumably referencing. That would be case 3. |
313 | |
314 | =item 3. |
315 | |
316 | The case of individual array elements arises often enough that it gets |
317 | cumbersome to use method 2. As a form of syntactic sugar, the two |
318 | lines like that above can be written: |
319 | |
320 | $arrayref->[0] = "January"; |
748a9306 |
321 | $hashref->{"KEY"} = "VALUE"; |
a0d0e21e |
322 | |
323 | The left side of the array can be any expression returning a reference, |
324 | including a previous dereference. Note that C<$array[$x]> is I<NOT> the |
325 | same thing as C<$array-E<gt>[$x]> here: |
326 | |
327 | $array[$x]->{"foo"}->[0] = "January"; |
328 | |
329 | This is one of the cases we mentioned earlier in which references could |
330 | spring into existence when in an lvalue context. Before this |
331 | statement, C<$array[$x]> may have been undefined. If so, it's |
332 | automatically defined with a hash reference so that we can look up |
333 | C<{"foo"}> in it. Likewise C<$array[$x]-E<gt>{"foo"}> will automatically get |
334 | defined with an array reference so that we can look up C<[0]> in it. |
335 | |
336 | One more thing here. The arrow is optional I<BETWEEN> brackets |
337 | subscripts, so you can shrink the above down to |
338 | |
339 | $array[$x]{"foo"}[0] = "January"; |
340 | |
341 | Which, in the degenerate case of using only ordinary arrays, gives you |
342 | multidimensional arrays just like C's: |
343 | |
344 | $score[$x][$y][$z] += 42; |
345 | |
346 | Well, okay, not entirely like C's arrays, actually. C doesn't know how |
347 | to grow its arrays on demand. Perl does. |
348 | |
349 | =item 4. |
350 | |
351 | If a reference happens to be a reference to an object, then there are |
352 | probably methods to access the things referred to, and you should probably |
353 | stick to those methods unless you're in the class package that defines the |
354 | object's methods. In other words, be nice, and don't violate the object's |
355 | encapsulation without a very good reason. Perl does not enforce |
356 | encapsulation. We are not totalitarians here. We do expect some basic |
357 | civility though. |
358 | |
359 | =back |
360 | |
361 | The ref() operator may be used to determine what type of thing the |
362 | reference is pointing to. See L<perlfunc>. |
363 | |
364 | The bless() operator may be used to associate a reference with a package |
365 | functioning as an object class. See L<perlobj>. |
366 | |
5f05dabc |
367 | A typeglob may be dereferenced the same way a reference can, because |
a0d0e21e |
368 | the dereference syntax always indicates the kind of reference desired. |
369 | So C<${*foo}> and C<${\$foo}> both indicate the same scalar variable. |
370 | |
371 | Here's a trick for interpolating a subroutine call into a string: |
372 | |
cb1a09d0 |
373 | print "My sub returned @{[mysub(1,2,3)]} that time.\n"; |
374 | |
375 | The way it works is that when the C<@{...}> is seen in the double-quoted |
376 | string, it's evaluated as a block. The block creates a reference to an |
377 | anonymous array containing the results of the call to C<mysub(1,2,3)>. So |
378 | the whole block returns a reference to an array, which is then |
379 | dereferenced by C<@{...}> and stuck into the double-quoted string. This |
380 | chicanery is also useful for arbitrary expressions: |
a0d0e21e |
381 | |
184e9718 |
382 | print "That yields @{[$n + 5]} widgets\n"; |
a0d0e21e |
383 | |
384 | =head2 Symbolic references |
385 | |
386 | We said that references spring into existence as necessary if they are |
387 | undefined, but we didn't say what happens if a value used as a |
388 | reference is already defined, but I<ISN'T> a hard reference. If you |
389 | use it as a reference in this case, it'll be treated as a symbolic |
390 | reference. That is, the value of the scalar is taken to be the I<NAME> |
391 | of a variable, rather than a direct link to a (possibly) anonymous |
392 | value. |
393 | |
394 | People frequently expect it to work like this. So it does. |
395 | |
396 | $name = "foo"; |
397 | $$name = 1; # Sets $foo |
398 | ${$name} = 2; # Sets $foo |
399 | ${$name x 2} = 3; # Sets $foofoo |
400 | $name->[0] = 4; # Sets $foo[0] |
401 | @$name = (); # Clears @foo |
402 | &$name(); # Calls &foo() (as in Perl 4) |
403 | $pack = "THAT"; |
404 | ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval |
405 | |
406 | This is very powerful, and slightly dangerous, in that it's possible |
407 | to intend (with the utmost sincerity) to use a hard reference, and |
408 | accidentally use a symbolic reference instead. To protect against |
409 | that, you can say |
410 | |
411 | use strict 'refs'; |
412 | |
413 | and then only hard references will be allowed for the rest of the enclosing |
414 | block. An inner block may countermand that with |
415 | |
416 | no strict 'refs'; |
417 | |
418 | Only package variables are visible to symbolic references. Lexical |
419 | variables (declared with my()) aren't in a symbol table, and thus are |
420 | invisible to this mechanism. For example: |
421 | |
422 | local($value) = 10; |
423 | $ref = \$value; |
424 | { |
425 | my $value = 20; |
426 | print $$ref; |
427 | } |
428 | |
429 | This will still print 10, not 20. Remember that local() affects package |
430 | variables, which are all "global" to the package. |
431 | |
748a9306 |
432 | =head2 Not-so-symbolic references |
433 | |
434 | A new feature contributing to readability in 5.001 is that the brackets |
435 | around a symbolic reference behave more like quotes, just as they |
436 | always have within a string. That is, |
437 | |
438 | $push = "pop on "; |
439 | print "${push}over"; |
440 | |
441 | has always meant to print "pop on over", despite the fact that push is |
442 | a reserved word. This has been generalized to work the same outside |
443 | of quotes, so that |
444 | |
445 | print ${push} . "over"; |
446 | |
447 | and even |
448 | |
449 | print ${ push } . "over"; |
450 | |
451 | will have the same effect. (This would have been a syntax error in |
452 | 5.000, though Perl 4 allowed it in the spaceless form.) Note that this |
453 | construct is I<not> considered to be a symbolic reference when you're |
454 | using strict refs: |
455 | |
456 | use strict 'refs'; |
457 | ${ bareword }; # Okay, means $bareword. |
458 | ${ "bareword" }; # Error, symbolic reference. |
459 | |
460 | Similarly, because of all the subscripting that is done using single |
461 | words, we've applied the same rule to any bareword that is used for |
462 | subscripting a hash. So now, instead of writing |
463 | |
464 | $array{ "aaa" }{ "bbb" }{ "ccc" } |
465 | |
5f05dabc |
466 | you can write just |
748a9306 |
467 | |
468 | $array{ aaa }{ bbb }{ ccc } |
469 | |
470 | and not worry about whether the subscripts are reserved words. In the |
471 | rare event that you do wish to do something like |
472 | |
473 | $array{ shift } |
474 | |
475 | you can force interpretation as a reserved word by adding anything that |
476 | makes it more than a bareword: |
477 | |
478 | $array{ shift() } |
479 | $array{ +shift } |
480 | $array{ shift @_ } |
481 | |
482 | The B<-w> switch will warn you if it interprets a reserved word as a string. |
5f05dabc |
483 | But it will no longer warn you about using lowercase words, because the |
748a9306 |
484 | string is effectively quoted. |
485 | |
cb1a09d0 |
486 | =head1 WARNING |
748a9306 |
487 | |
488 | You may not (usefully) use a reference as the key to a hash. It will be |
489 | converted into a string: |
490 | |
491 | $x{ \$a } = $a; |
492 | |
493 | If you try to dereference the key, it won't do a hard dereference, and |
184e9718 |
494 | you won't accomplish what you're attempting. You might want to do something |
cb1a09d0 |
495 | more like |
748a9306 |
496 | |
cb1a09d0 |
497 | $r = \@a; |
498 | $x{ $r } = $r; |
499 | |
500 | And then at least you can use the values(), which will be |
501 | real refs, instead of the keys(), which won't. |
502 | |
503 | =head1 SEE ALSO |
a0d0e21e |
504 | |
505 | Besides the obvious documents, source code can be instructive. |
506 | Some rather pathological examples of the use of references can be found |
507 | in the F<t/op/ref.t> regression test in the Perl source directory. |
cb1a09d0 |
508 | |
509 | See also L<perldsc> and L<perllol> for how to use references to create |
510 | complex data structures, and L<perlobj> for how to use them to create |
511 | objects. |