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