1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlref - Perl references and nested data structures 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 NOTE 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateThis is complete documentation about all aspects of references. 8*0Sstevel@tonic-gateFor a shorter, tutorial introduction to just the essential features, 9*0Sstevel@tonic-gatesee L<perlreftut>. 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gate=head1 DESCRIPTION 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gateBefore release 5 of Perl it was difficult to represent complex data 14*0Sstevel@tonic-gatestructures, because all references had to be symbolic--and even then 15*0Sstevel@tonic-gateit was difficult to refer to a variable instead of a symbol table entry. 16*0Sstevel@tonic-gatePerl now not only makes it easier to use symbolic references to variables, 17*0Sstevel@tonic-gatebut also lets you have "hard" references to any piece of data or code. 18*0Sstevel@tonic-gateAny scalar may hold a hard reference. Because arrays and hashes contain 19*0Sstevel@tonic-gatescalars, you can now easily build arrays of arrays, arrays of hashes, 20*0Sstevel@tonic-gatehashes of arrays, arrays of hashes of functions, and so on. 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gateHard references are smart--they keep track of reference counts for you, 23*0Sstevel@tonic-gateautomatically freeing the thing referred to when its reference count goes 24*0Sstevel@tonic-gateto zero. (Reference counts for values in self-referential or 25*0Sstevel@tonic-gatecyclic data structures may not go to zero without a little help; see 26*0Sstevel@tonic-gateL<perlobj/"Two-Phased Garbage Collection"> for a detailed explanation.) 27*0Sstevel@tonic-gateIf that thing happens to be an object, the object is destructed. See 28*0Sstevel@tonic-gateL<perlobj> for more about objects. (In a sense, everything in Perl is an 29*0Sstevel@tonic-gateobject, but we usually reserve the word for references to objects that 30*0Sstevel@tonic-gatehave been officially "blessed" into a class package.) 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gateSymbolic references are names of variables or other objects, just as a 33*0Sstevel@tonic-gatesymbolic link in a Unix filesystem contains merely the name of a file. 34*0Sstevel@tonic-gateThe C<*glob> notation is something of a symbolic reference. (Symbolic 35*0Sstevel@tonic-gatereferences are sometimes called "soft references", but please don't call 36*0Sstevel@tonic-gatethem that; references are confusing enough without useless synonyms.) 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gateIn contrast, hard references are more like hard links in a Unix file 39*0Sstevel@tonic-gatesystem: They are used to access an underlying object without concern for 40*0Sstevel@tonic-gatewhat its (other) name is. When the word "reference" is used without an 41*0Sstevel@tonic-gateadjective, as in the following paragraph, it is usually talking about a 42*0Sstevel@tonic-gatehard reference. 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gateReferences are easy to use in Perl. There is just one overriding 45*0Sstevel@tonic-gateprinciple: Perl does no implicit referencing or dereferencing. When a 46*0Sstevel@tonic-gatescalar is holding a reference, it always behaves as a simple scalar. It 47*0Sstevel@tonic-gatedoesn't magically start being an array or hash or subroutine; you have to 48*0Sstevel@tonic-gatetell it explicitly to do so, by dereferencing it. 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate=head2 Making References 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gateReferences can be created in several ways. 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate=over 4 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate=item 1. 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gateBy using the backslash operator on a variable, subroutine, or value. 59*0Sstevel@tonic-gate(This works much like the & (address-of) operator in C.) 60*0Sstevel@tonic-gateThis typically creates I<another> reference to a variable, because 61*0Sstevel@tonic-gatethere's already a reference to the variable in the symbol table. But 62*0Sstevel@tonic-gatethe symbol table reference might go away, and you'll still have the 63*0Sstevel@tonic-gatereference that the backslash returned. Here are some examples: 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate $scalarref = \$foo; 66*0Sstevel@tonic-gate $arrayref = \@ARGV; 67*0Sstevel@tonic-gate $hashref = \%ENV; 68*0Sstevel@tonic-gate $coderef = \&handler; 69*0Sstevel@tonic-gate $globref = \*foo; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gateIt isn't possible to create a true reference to an IO handle (filehandle 72*0Sstevel@tonic-gateor dirhandle) using the backslash operator. The most you can get is a 73*0Sstevel@tonic-gatereference to a typeglob, which is actually a complete symbol table entry. 74*0Sstevel@tonic-gateBut see the explanation of the C<*foo{THING}> syntax below. However, 75*0Sstevel@tonic-gateyou can still use type globs and globrefs as though they were IO handles. 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate=item 2. 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gateA reference to an anonymous array can be created using square 80*0Sstevel@tonic-gatebrackets: 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate $arrayref = [1, 2, ['a', 'b', 'c']]; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gateHere we've created a reference to an anonymous array of three elements 85*0Sstevel@tonic-gatewhose final element is itself a reference to another anonymous array of three 86*0Sstevel@tonic-gateelements. (The multidimensional syntax described later can be used to 87*0Sstevel@tonic-gateaccess this. For example, after the above, C<< $arrayref->[2][1] >> would have 88*0Sstevel@tonic-gatethe value "b".) 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gateTaking a reference to an enumerated list is not the same 91*0Sstevel@tonic-gateas using square brackets--instead it's the same as creating 92*0Sstevel@tonic-gatea list of references! 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate @list = (\$a, \@b, \%c); 95*0Sstevel@tonic-gate @list = \($a, @b, %c); # same thing! 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gateAs a special case, C<\(@foo)> returns a list of references to the contents 98*0Sstevel@tonic-gateof C<@foo>, not a reference to C<@foo> itself. Likewise for C<%foo>, 99*0Sstevel@tonic-gateexcept that the key references are to copies (since the keys are just 100*0Sstevel@tonic-gatestrings rather than full-fledged scalars). 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate=item 3. 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gateA reference to an anonymous hash can be created using curly 105*0Sstevel@tonic-gatebrackets: 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate $hashref = { 108*0Sstevel@tonic-gate 'Adam' => 'Eve', 109*0Sstevel@tonic-gate 'Clyde' => 'Bonnie', 110*0Sstevel@tonic-gate }; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gateAnonymous hash and array composers like these can be intermixed freely to 113*0Sstevel@tonic-gateproduce as complicated a structure as you want. The multidimensional 114*0Sstevel@tonic-gatesyntax described below works for these too. The values above are 115*0Sstevel@tonic-gateliterals, but variables and expressions would work just as well, because 116*0Sstevel@tonic-gateassignment operators in Perl (even within local() or my()) are executable 117*0Sstevel@tonic-gatestatements, not compile-time declarations. 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gateBecause curly brackets (braces) are used for several other things 120*0Sstevel@tonic-gateincluding BLOCKs, you may occasionally have to disambiguate braces at the 121*0Sstevel@tonic-gatebeginning of a statement by putting a C<+> or a C<return> in front so 122*0Sstevel@tonic-gatethat Perl realizes the opening brace isn't starting a BLOCK. The economy and 123*0Sstevel@tonic-gatemnemonic value of using curlies is deemed worth this occasional extra 124*0Sstevel@tonic-gatehassle. 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gateFor example, if you wanted a function to make a new hash and return a 127*0Sstevel@tonic-gatereference to it, you have these options: 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate sub hashem { { @_ } } # silently wrong 130*0Sstevel@tonic-gate sub hashem { +{ @_ } } # ok 131*0Sstevel@tonic-gate sub hashem { return { @_ } } # ok 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gateOn the other hand, if you want the other meaning, you can do this: 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate sub showem { { @_ } } # ambiguous (currently ok, but may change) 136*0Sstevel@tonic-gate sub showem { {; @_ } } # ok 137*0Sstevel@tonic-gate sub showem { { return @_ } } # ok 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gateThe leading C<+{> and C<{;> always serve to disambiguate 140*0Sstevel@tonic-gatethe expression to mean either the HASH reference, or the BLOCK. 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate=item 4. 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gateA reference to an anonymous subroutine can be created by using 145*0Sstevel@tonic-gateC<sub> without a subname: 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate $coderef = sub { print "Boink!\n" }; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gateNote the semicolon. Except for the code 150*0Sstevel@tonic-gateinside not being immediately executed, a C<sub {}> is not so much a 151*0Sstevel@tonic-gatedeclaration as it is an operator, like C<do{}> or C<eval{}>. (However, no 152*0Sstevel@tonic-gatematter how many times you execute that particular line (unless you're in an 153*0Sstevel@tonic-gateC<eval("...")>), $coderef will still have a reference to the I<same> 154*0Sstevel@tonic-gateanonymous subroutine.) 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gateAnonymous subroutines act as closures with respect to my() variables, 157*0Sstevel@tonic-gatethat is, variables lexically visible within the current scope. Closure 158*0Sstevel@tonic-gateis a notion out of the Lisp world that says if you define an anonymous 159*0Sstevel@tonic-gatefunction in a particular lexical context, it pretends to run in that 160*0Sstevel@tonic-gatecontext even when it's called outside the context. 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gateIn human terms, it's a funny way of passing arguments to a subroutine when 163*0Sstevel@tonic-gateyou define it as well as when you call it. It's useful for setting up 164*0Sstevel@tonic-gatelittle bits of code to run later, such as callbacks. You can even 165*0Sstevel@tonic-gatedo object-oriented stuff with it, though Perl already provides a different 166*0Sstevel@tonic-gatemechanism to do that--see L<perlobj>. 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gateYou might also think of closure as a way to write a subroutine 169*0Sstevel@tonic-gatetemplate without using eval(). Here's a small example of how 170*0Sstevel@tonic-gateclosures work: 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate sub newprint { 173*0Sstevel@tonic-gate my $x = shift; 174*0Sstevel@tonic-gate return sub { my $y = shift; print "$x, $y!\n"; }; 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate $h = newprint("Howdy"); 177*0Sstevel@tonic-gate $g = newprint("Greetings"); 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate # Time passes... 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate &$h("world"); 182*0Sstevel@tonic-gate &$g("earthlings"); 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gateThis prints 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate Howdy, world! 187*0Sstevel@tonic-gate Greetings, earthlings! 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gateNote particularly that $x continues to refer to the value passed 190*0Sstevel@tonic-gateinto newprint() I<despite> "my $x" having gone out of scope by the 191*0Sstevel@tonic-gatetime the anonymous subroutine runs. That's what a closure is all 192*0Sstevel@tonic-gateabout. 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gateThis applies only to lexical variables, by the way. Dynamic variables 195*0Sstevel@tonic-gatecontinue to work as they have always worked. Closure is not something 196*0Sstevel@tonic-gatethat most Perl programmers need trouble themselves about to begin with. 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate=item 5. 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gateReferences are often returned by special subroutines called constructors. 201*0Sstevel@tonic-gatePerl objects are just references to a special type of object that happens to know 202*0Sstevel@tonic-gatewhich package it's associated with. Constructors are just special 203*0Sstevel@tonic-gatesubroutines that know how to create that association. They do so by 204*0Sstevel@tonic-gatestarting with an ordinary reference, and it remains an ordinary reference 205*0Sstevel@tonic-gateeven while it's also being an object. Constructors are often 206*0Sstevel@tonic-gatenamed new() and called indirectly: 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate $objref = new Doggie (Tail => 'short', Ears => 'long'); 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gateBut don't have to be: 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate $objref = Doggie->new(Tail => 'short', Ears => 'long'); 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate use Term::Cap; 215*0Sstevel@tonic-gate $terminal = Term::Cap->Tgetent( { OSPEED => 9600 }); 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate use Tk; 218*0Sstevel@tonic-gate $main = MainWindow->new(); 219*0Sstevel@tonic-gate $menubar = $main->Frame(-relief => "raised", 220*0Sstevel@tonic-gate -borderwidth => 2) 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate=item 6. 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gateReferences of the appropriate type can spring into existence if you 225*0Sstevel@tonic-gatedereference them in a context that assumes they exist. Because we haven't 226*0Sstevel@tonic-gatetalked about dereferencing yet, we can't show you any examples yet. 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate=item 7. 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gateA reference can be created by using a special syntax, lovingly known as 231*0Sstevel@tonic-gatethe *foo{THING} syntax. *foo{THING} returns a reference to the THING 232*0Sstevel@tonic-gateslot in *foo (which is the symbol table entry which holds everything 233*0Sstevel@tonic-gateknown as foo). 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate $scalarref = *foo{SCALAR}; 236*0Sstevel@tonic-gate $arrayref = *ARGV{ARRAY}; 237*0Sstevel@tonic-gate $hashref = *ENV{HASH}; 238*0Sstevel@tonic-gate $coderef = *handler{CODE}; 239*0Sstevel@tonic-gate $ioref = *STDIN{IO}; 240*0Sstevel@tonic-gate $globref = *foo{GLOB}; 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gateAll of these are self-explanatory except for C<*foo{IO}>. It returns 243*0Sstevel@tonic-gatethe IO handle, used for file handles (L<perlfunc/open>), sockets 244*0Sstevel@tonic-gate(L<perlfunc/socket> and L<perlfunc/socketpair>), and directory 245*0Sstevel@tonic-gatehandles (L<perlfunc/opendir>). For compatibility with previous 246*0Sstevel@tonic-gateversions of Perl, C<*foo{FILEHANDLE}> is a synonym for C<*foo{IO}>, though it 247*0Sstevel@tonic-gateis deprecated as of 5.8.0. If deprecation warnings are in effect, it will warn 248*0Sstevel@tonic-gateof its use. 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gateC<*foo{THING}> returns undef if that particular THING hasn't been used yet, 251*0Sstevel@tonic-gateexcept in the case of scalars. C<*foo{SCALAR}> returns a reference to an 252*0Sstevel@tonic-gateanonymous scalar if $foo hasn't been used yet. This might change in a 253*0Sstevel@tonic-gatefuture release. 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gateC<*foo{IO}> is an alternative to the C<*HANDLE> mechanism given in 256*0Sstevel@tonic-gateL<perldata/"Typeglobs and Filehandles"> for passing filehandles 257*0Sstevel@tonic-gateinto or out of subroutines, or storing into larger data structures. 258*0Sstevel@tonic-gateIts disadvantage is that it won't create a new filehandle for you. 259*0Sstevel@tonic-gateIts advantage is that you have less risk of clobbering more than 260*0Sstevel@tonic-gateyou want to with a typeglob assignment. (It still conflates file 261*0Sstevel@tonic-gateand directory handles, though.) However, if you assign the incoming 262*0Sstevel@tonic-gatevalue to a scalar instead of a typeglob as we do in the examples 263*0Sstevel@tonic-gatebelow, there's no risk of that happening. 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate splutter(*STDOUT); # pass the whole glob 266*0Sstevel@tonic-gate splutter(*STDOUT{IO}); # pass both file and dir handles 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate sub splutter { 269*0Sstevel@tonic-gate my $fh = shift; 270*0Sstevel@tonic-gate print $fh "her um well a hmmm\n"; 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate $rec = get_rec(*STDIN); # pass the whole glob 274*0Sstevel@tonic-gate $rec = get_rec(*STDIN{IO}); # pass both file and dir handles 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate sub get_rec { 277*0Sstevel@tonic-gate my $fh = shift; 278*0Sstevel@tonic-gate return scalar <$fh>; 279*0Sstevel@tonic-gate } 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate=back 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate=head2 Using References 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gateThat's it for creating references. By now you're probably dying to 286*0Sstevel@tonic-gateknow how to use references to get back to your long-lost data. There 287*0Sstevel@tonic-gateare several basic methods. 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate=over 4 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate=item 1. 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gateAnywhere you'd put an identifier (or chain of identifiers) as part 294*0Sstevel@tonic-gateof a variable or subroutine name, you can replace the identifier with 295*0Sstevel@tonic-gatea simple scalar variable containing a reference of the correct type: 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate $bar = $$scalarref; 298*0Sstevel@tonic-gate push(@$arrayref, $filename); 299*0Sstevel@tonic-gate $$arrayref[0] = "January"; 300*0Sstevel@tonic-gate $$hashref{"KEY"} = "VALUE"; 301*0Sstevel@tonic-gate &$coderef(1,2,3); 302*0Sstevel@tonic-gate print $globref "output\n"; 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gateIt's important to understand that we are specifically I<not> dereferencing 305*0Sstevel@tonic-gateC<$arrayref[0]> or C<$hashref{"KEY"}> there. The dereference of the 306*0Sstevel@tonic-gatescalar variable happens I<before> it does any key lookups. Anything more 307*0Sstevel@tonic-gatecomplicated than a simple scalar variable must use methods 2 or 3 below. 308*0Sstevel@tonic-gateHowever, a "simple scalar" includes an identifier that itself uses method 309*0Sstevel@tonic-gate1 recursively. Therefore, the following prints "howdy". 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate $refrefref = \\\"howdy"; 312*0Sstevel@tonic-gate print $$$$refrefref; 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate=item 2. 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gateAnywhere you'd put an identifier (or chain of identifiers) as part of a 317*0Sstevel@tonic-gatevariable or subroutine name, you can replace the identifier with a 318*0Sstevel@tonic-gateBLOCK returning a reference of the correct type. In other words, the 319*0Sstevel@tonic-gateprevious examples could be written like this: 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate $bar = ${$scalarref}; 322*0Sstevel@tonic-gate push(@{$arrayref}, $filename); 323*0Sstevel@tonic-gate ${$arrayref}[0] = "January"; 324*0Sstevel@tonic-gate ${$hashref}{"KEY"} = "VALUE"; 325*0Sstevel@tonic-gate &{$coderef}(1,2,3); 326*0Sstevel@tonic-gate $globref->print("output\n"); # iff IO::Handle is loaded 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gateAdmittedly, it's a little silly to use the curlies in this case, but 329*0Sstevel@tonic-gatethe BLOCK can contain any arbitrary expression, in particular, 330*0Sstevel@tonic-gatesubscripted expressions: 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate &{ $dispatch{$index} }(1,2,3); # call correct routine 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gateBecause of being able to omit the curlies for the simple case of C<$$x>, 335*0Sstevel@tonic-gatepeople often make the mistake of viewing the dereferencing symbols as 336*0Sstevel@tonic-gateproper operators, and wonder about their precedence. If they were, 337*0Sstevel@tonic-gatethough, you could use parentheses instead of braces. That's not the case. 338*0Sstevel@tonic-gateConsider the difference below; case 0 is a short-hand version of case 1, 339*0Sstevel@tonic-gateI<not> case 2: 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate $$hashref{"KEY"} = "VALUE"; # CASE 0 342*0Sstevel@tonic-gate ${$hashref}{"KEY"} = "VALUE"; # CASE 1 343*0Sstevel@tonic-gate ${$hashref{"KEY"}} = "VALUE"; # CASE 2 344*0Sstevel@tonic-gate ${$hashref->{"KEY"}} = "VALUE"; # CASE 3 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gateCase 2 is also deceptive in that you're accessing a variable 347*0Sstevel@tonic-gatecalled %hashref, not dereferencing through $hashref to the hash 348*0Sstevel@tonic-gateit's presumably referencing. That would be case 3. 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate=item 3. 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gateSubroutine calls and lookups of individual array elements arise often 353*0Sstevel@tonic-gateenough that it gets cumbersome to use method 2. As a form of 354*0Sstevel@tonic-gatesyntactic sugar, the examples for method 2 may be written: 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate $arrayref->[0] = "January"; # Array element 357*0Sstevel@tonic-gate $hashref->{"KEY"} = "VALUE"; # Hash element 358*0Sstevel@tonic-gate $coderef->(1,2,3); # Subroutine call 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gateThe left side of the arrow can be any expression returning a reference, 361*0Sstevel@tonic-gateincluding a previous dereference. Note that C<$array[$x]> is I<not> the 362*0Sstevel@tonic-gatesame thing as C<< $array->[$x] >> here: 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate $array[$x]->{"foo"}->[0] = "January"; 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gateThis is one of the cases we mentioned earlier in which references could 367*0Sstevel@tonic-gatespring into existence when in an lvalue context. Before this 368*0Sstevel@tonic-gatestatement, C<$array[$x]> may have been undefined. If so, it's 369*0Sstevel@tonic-gateautomatically defined with a hash reference so that we can look up 370*0Sstevel@tonic-gateC<{"foo"}> in it. Likewise C<< $array[$x]->{"foo"} >> will automatically get 371*0Sstevel@tonic-gatedefined with an array reference so that we can look up C<[0]> in it. 372*0Sstevel@tonic-gateThis process is called I<autovivification>. 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gateOne more thing here. The arrow is optional I<between> brackets 375*0Sstevel@tonic-gatesubscripts, so you can shrink the above down to 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate $array[$x]{"foo"}[0] = "January"; 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gateWhich, in the degenerate case of using only ordinary arrays, gives you 380*0Sstevel@tonic-gatemultidimensional arrays just like C's: 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate $score[$x][$y][$z] += 42; 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gateWell, okay, not entirely like C's arrays, actually. C doesn't know how 385*0Sstevel@tonic-gateto grow its arrays on demand. Perl does. 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate=item 4. 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gateIf a reference happens to be a reference to an object, then there are 390*0Sstevel@tonic-gateprobably methods to access the things referred to, and you should probably 391*0Sstevel@tonic-gatestick to those methods unless you're in the class package that defines the 392*0Sstevel@tonic-gateobject's methods. In other words, be nice, and don't violate the object's 393*0Sstevel@tonic-gateencapsulation without a very good reason. Perl does not enforce 394*0Sstevel@tonic-gateencapsulation. We are not totalitarians here. We do expect some basic 395*0Sstevel@tonic-gatecivility though. 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate=back 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gateUsing a string or number as a reference produces a symbolic reference, 400*0Sstevel@tonic-gateas explained above. Using a reference as a number produces an 401*0Sstevel@tonic-gateinteger representing its storage location in memory. The only 402*0Sstevel@tonic-gateuseful thing to be done with this is to compare two references 403*0Sstevel@tonic-gatenumerically to see whether they refer to the same location. 404*0Sstevel@tonic-gate 405*0Sstevel@tonic-gate if ($ref1 == $ref2) { # cheap numeric compare of references 406*0Sstevel@tonic-gate print "refs 1 and 2 refer to the same thing\n"; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate 409*0Sstevel@tonic-gateUsing a reference as a string produces both its referent's type, 410*0Sstevel@tonic-gateincluding any package blessing as described in L<perlobj>, as well 411*0Sstevel@tonic-gateas the numeric address expressed in hex. The ref() operator returns 412*0Sstevel@tonic-gatejust the type of thing the reference is pointing to, without the 413*0Sstevel@tonic-gateaddress. See L<perlfunc/ref> for details and examples of its use. 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gateThe bless() operator may be used to associate the object a reference 416*0Sstevel@tonic-gatepoints to with a package functioning as an object class. See L<perlobj>. 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gateA typeglob may be dereferenced the same way a reference can, because 419*0Sstevel@tonic-gatethe dereference syntax always indicates the type of reference desired. 420*0Sstevel@tonic-gateSo C<${*foo}> and C<${\$foo}> both indicate the same scalar variable. 421*0Sstevel@tonic-gate 422*0Sstevel@tonic-gateHere's a trick for interpolating a subroutine call into a string: 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate print "My sub returned @{[mysub(1,2,3)]} that time.\n"; 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gateThe way it works is that when the C<@{...}> is seen in the double-quoted 427*0Sstevel@tonic-gatestring, it's evaluated as a block. The block creates a reference to an 428*0Sstevel@tonic-gateanonymous array containing the results of the call to C<mysub(1,2,3)>. So 429*0Sstevel@tonic-gatethe whole block returns a reference to an array, which is then 430*0Sstevel@tonic-gatedereferenced by C<@{...}> and stuck into the double-quoted string. This 431*0Sstevel@tonic-gatechicanery is also useful for arbitrary expressions: 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate print "That yields @{[$n + 5]} widgets\n"; 434*0Sstevel@tonic-gate 435*0Sstevel@tonic-gate=head2 Symbolic references 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gateWe said that references spring into existence as necessary if they are 438*0Sstevel@tonic-gateundefined, but we didn't say what happens if a value used as a 439*0Sstevel@tonic-gatereference is already defined, but I<isn't> a hard reference. If you 440*0Sstevel@tonic-gateuse it as a reference, it'll be treated as a symbolic 441*0Sstevel@tonic-gatereference. That is, the value of the scalar is taken to be the I<name> 442*0Sstevel@tonic-gateof a variable, rather than a direct link to a (possibly) anonymous 443*0Sstevel@tonic-gatevalue. 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gatePeople frequently expect it to work like this. So it does. 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gate $name = "foo"; 448*0Sstevel@tonic-gate $$name = 1; # Sets $foo 449*0Sstevel@tonic-gate ${$name} = 2; # Sets $foo 450*0Sstevel@tonic-gate ${$name x 2} = 3; # Sets $foofoo 451*0Sstevel@tonic-gate $name->[0] = 4; # Sets $foo[0] 452*0Sstevel@tonic-gate @$name = (); # Clears @foo 453*0Sstevel@tonic-gate &$name(); # Calls &foo() (as in Perl 4) 454*0Sstevel@tonic-gate $pack = "THAT"; 455*0Sstevel@tonic-gate ${"${pack}::$name"} = 5; # Sets $THAT::foo without eval 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gateThis is powerful, and slightly dangerous, in that it's possible 458*0Sstevel@tonic-gateto intend (with the utmost sincerity) to use a hard reference, and 459*0Sstevel@tonic-gateaccidentally use a symbolic reference instead. To protect against 460*0Sstevel@tonic-gatethat, you can say 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate use strict 'refs'; 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gateand then only hard references will be allowed for the rest of the enclosing 465*0Sstevel@tonic-gateblock. An inner block may countermand that with 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate no strict 'refs'; 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gateOnly package variables (globals, even if localized) are visible to 470*0Sstevel@tonic-gatesymbolic references. Lexical variables (declared with my()) aren't in 471*0Sstevel@tonic-gatea symbol table, and thus are invisible to this mechanism. For example: 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate local $value = 10; 474*0Sstevel@tonic-gate $ref = "value"; 475*0Sstevel@tonic-gate { 476*0Sstevel@tonic-gate my $value = 20; 477*0Sstevel@tonic-gate print $$ref; 478*0Sstevel@tonic-gate } 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gateThis will still print 10, not 20. Remember that local() affects package 481*0Sstevel@tonic-gatevariables, which are all "global" to the package. 482*0Sstevel@tonic-gate 483*0Sstevel@tonic-gate=head2 Not-so-symbolic references 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gateA new feature contributing to readability in perl version 5.001 is that the 486*0Sstevel@tonic-gatebrackets around a symbolic reference behave more like quotes, just as they 487*0Sstevel@tonic-gatealways have within a string. That is, 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate $push = "pop on "; 490*0Sstevel@tonic-gate print "${push}over"; 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gatehas always meant to print "pop on over", even though push is 493*0Sstevel@tonic-gatea reserved word. This has been generalized to work the same outside 494*0Sstevel@tonic-gateof quotes, so that 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate print ${push} . "over"; 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gateand even 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate print ${ push } . "over"; 501*0Sstevel@tonic-gate 502*0Sstevel@tonic-gatewill have the same effect. (This would have been a syntax error in 503*0Sstevel@tonic-gatePerl 5.000, though Perl 4 allowed it in the spaceless form.) This 504*0Sstevel@tonic-gateconstruct is I<not> considered to be a symbolic reference when you're 505*0Sstevel@tonic-gateusing strict refs: 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate use strict 'refs'; 508*0Sstevel@tonic-gate ${ bareword }; # Okay, means $bareword. 509*0Sstevel@tonic-gate ${ "bareword" }; # Error, symbolic reference. 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gateSimilarly, because of all the subscripting that is done using single 512*0Sstevel@tonic-gatewords, we've applied the same rule to any bareword that is used for 513*0Sstevel@tonic-gatesubscripting a hash. So now, instead of writing 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate $array{ "aaa" }{ "bbb" }{ "ccc" } 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gateyou can write just 518*0Sstevel@tonic-gate 519*0Sstevel@tonic-gate $array{ aaa }{ bbb }{ ccc } 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gateand not worry about whether the subscripts are reserved words. In the 522*0Sstevel@tonic-gaterare event that you do wish to do something like 523*0Sstevel@tonic-gate 524*0Sstevel@tonic-gate $array{ shift } 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gateyou can force interpretation as a reserved word by adding anything that 527*0Sstevel@tonic-gatemakes it more than a bareword: 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate $array{ shift() } 530*0Sstevel@tonic-gate $array{ +shift } 531*0Sstevel@tonic-gate $array{ shift @_ } 532*0Sstevel@tonic-gate 533*0Sstevel@tonic-gateThe C<use warnings> pragma or the B<-w> switch will warn you if it 534*0Sstevel@tonic-gateinterprets a reserved word as a string. 535*0Sstevel@tonic-gateBut it will no longer warn you about using lowercase words, because the 536*0Sstevel@tonic-gatestring is effectively quoted. 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gate=head2 Pseudo-hashes: Using an array as a hash 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gateB<WARNING>: This section describes an experimental feature. Details may 541*0Sstevel@tonic-gatechange without notice in future versions. 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gateB<NOTE>: The current user-visible implementation of pseudo-hashes 544*0Sstevel@tonic-gate(the weird use of the first array element) is deprecated starting from 545*0Sstevel@tonic-gatePerl 5.8.0 and will be removed in Perl 5.10.0, and the feature will be 546*0Sstevel@tonic-gateimplemented differently. Not only is the current interface rather ugly, 547*0Sstevel@tonic-gatebut the current implementation slows down normal array and hash use quite 548*0Sstevel@tonic-gatenoticeably. The 'fields' pragma interface will remain available. 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gateBeginning with release 5.005 of Perl, you may use an array reference 551*0Sstevel@tonic-gatein some contexts that would normally require a hash reference. This 552*0Sstevel@tonic-gateallows you to access array elements using symbolic names, as if they 553*0Sstevel@tonic-gatewere fields in a structure. 554*0Sstevel@tonic-gate 555*0Sstevel@tonic-gateFor this to work, the array must contain extra information. The first 556*0Sstevel@tonic-gateelement of the array has to be a hash reference that maps field names 557*0Sstevel@tonic-gateto array indices. Here is an example: 558*0Sstevel@tonic-gate 559*0Sstevel@tonic-gate $struct = [{foo => 1, bar => 2}, "FOO", "BAR"]; 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate $struct->{foo}; # same as $struct->[1], i.e. "FOO" 562*0Sstevel@tonic-gate $struct->{bar}; # same as $struct->[2], i.e. "BAR" 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate keys %$struct; # will return ("foo", "bar") in some order 565*0Sstevel@tonic-gate values %$struct; # will return ("FOO", "BAR") in same some order 566*0Sstevel@tonic-gate 567*0Sstevel@tonic-gate while (my($k,$v) = each %$struct) { 568*0Sstevel@tonic-gate print "$k => $v\n"; 569*0Sstevel@tonic-gate } 570*0Sstevel@tonic-gate 571*0Sstevel@tonic-gatePerl will raise an exception if you try to access nonexistent fields. 572*0Sstevel@tonic-gateTo avoid inconsistencies, always use the fields::phash() function 573*0Sstevel@tonic-gateprovided by the C<fields> pragma. 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate use fields; 576*0Sstevel@tonic-gate $pseudohash = fields::phash(foo => "FOO", bar => "BAR"); 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gateFor better performance, Perl can also do the translation from field 579*0Sstevel@tonic-gatenames to array indices at compile time for typed object references. 580*0Sstevel@tonic-gateSee L<fields>. 581*0Sstevel@tonic-gate 582*0Sstevel@tonic-gateThere are two ways to check for the existence of a key in a 583*0Sstevel@tonic-gatepseudo-hash. The first is to use exists(). This checks to see if the 584*0Sstevel@tonic-gategiven field has ever been set. It acts this way to match the behavior 585*0Sstevel@tonic-gateof a regular hash. For instance: 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate use fields; 588*0Sstevel@tonic-gate $phash = fields::phash([qw(foo bar pants)], ['FOO']); 589*0Sstevel@tonic-gate $phash->{pants} = undef; 590*0Sstevel@tonic-gate 591*0Sstevel@tonic-gate print exists $phash->{foo}; # true, 'foo' was set in the declaration 592*0Sstevel@tonic-gate print exists $phash->{bar}; # false, 'bar' has not been used. 593*0Sstevel@tonic-gate print exists $phash->{pants}; # true, your 'pants' have been touched 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gateThe second is to use exists() on the hash reference sitting in the 596*0Sstevel@tonic-gatefirst array element. This checks to see if the given key is a valid 597*0Sstevel@tonic-gatefield in the pseudo-hash. 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gate print exists $phash->[0]{bar}; # true, 'bar' is a valid field 600*0Sstevel@tonic-gate print exists $phash->[0]{shoes};# false, 'shoes' can't be used 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gatedelete() on a pseudo-hash element only deletes the value corresponding 603*0Sstevel@tonic-gateto the key, not the key itself. To delete the key, you'll have to 604*0Sstevel@tonic-gateexplicitly delete it from the first hash element. 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate print delete $phash->{foo}; # prints $phash->[1], "FOO" 607*0Sstevel@tonic-gate print exists $phash->{foo}; # false 608*0Sstevel@tonic-gate print exists $phash->[0]{foo}; # true, key still exists 609*0Sstevel@tonic-gate print delete $phash->[0]{foo}; # now key is gone 610*0Sstevel@tonic-gate print $phash->{foo}; # runtime exception 611*0Sstevel@tonic-gate 612*0Sstevel@tonic-gate=head2 Function Templates 613*0Sstevel@tonic-gate 614*0Sstevel@tonic-gateAs explained above, a closure is an anonymous function with access to the 615*0Sstevel@tonic-gatelexical variables visible when that function was compiled. It retains 616*0Sstevel@tonic-gateaccess to those variables even though it doesn't get run until later, 617*0Sstevel@tonic-gatesuch as in a signal handler or a Tk callback. 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gateUsing a closure as a function template allows us to generate many functions 620*0Sstevel@tonic-gatethat act similarly. Suppose you wanted functions named after the colors 621*0Sstevel@tonic-gatethat generated HTML font changes for the various colors: 622*0Sstevel@tonic-gate 623*0Sstevel@tonic-gate print "Be ", red("careful"), "with that ", green("light"); 624*0Sstevel@tonic-gate 625*0Sstevel@tonic-gateThe red() and green() functions would be similar. To create these, 626*0Sstevel@tonic-gatewe'll assign a closure to a typeglob of the name of the function we're 627*0Sstevel@tonic-gatetrying to build. 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate @colors = qw(red blue green yellow orange purple violet); 630*0Sstevel@tonic-gate for my $name (@colors) { 631*0Sstevel@tonic-gate no strict 'refs'; # allow symbol table manipulation 632*0Sstevel@tonic-gate *$name = *{uc $name} = sub { "<FONT COLOR='$name'>@_</FONT>" }; 633*0Sstevel@tonic-gate } 634*0Sstevel@tonic-gate 635*0Sstevel@tonic-gateNow all those different functions appear to exist independently. You can 636*0Sstevel@tonic-gatecall red(), RED(), blue(), BLUE(), green(), etc. This technique saves on 637*0Sstevel@tonic-gateboth compile time and memory use, and is less error-prone as well, since 638*0Sstevel@tonic-gatesyntax checks happen at compile time. It's critical that any variables in 639*0Sstevel@tonic-gatethe anonymous subroutine be lexicals in order to create a proper closure. 640*0Sstevel@tonic-gateThat's the reasons for the C<my> on the loop iteration variable. 641*0Sstevel@tonic-gate 642*0Sstevel@tonic-gateThis is one of the only places where giving a prototype to a closure makes 643*0Sstevel@tonic-gatemuch sense. If you wanted to impose scalar context on the arguments of 644*0Sstevel@tonic-gatethese functions (probably not a wise idea for this particular example), 645*0Sstevel@tonic-gateyou could have written it this way instead: 646*0Sstevel@tonic-gate 647*0Sstevel@tonic-gate *$name = sub ($) { "<FONT COLOR='$name'>$_[0]</FONT>" }; 648*0Sstevel@tonic-gate 649*0Sstevel@tonic-gateHowever, since prototype checking happens at compile time, the assignment 650*0Sstevel@tonic-gateabove happens too late to be of much use. You could address this by 651*0Sstevel@tonic-gateputting the whole loop of assignments within a BEGIN block, forcing it 652*0Sstevel@tonic-gateto occur during compilation. 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gateAccess to lexicals that change over type--like those in the C<for> loop 655*0Sstevel@tonic-gateabove--only works with closures, not general subroutines. In the general 656*0Sstevel@tonic-gatecase, then, named subroutines do not nest properly, although anonymous 657*0Sstevel@tonic-gateones do. If you are accustomed to using nested subroutines in other 658*0Sstevel@tonic-gateprogramming languages with their own private variables, you'll have to 659*0Sstevel@tonic-gatework at it a bit in Perl. The intuitive coding of this type of thing 660*0Sstevel@tonic-gateincurs mysterious warnings about ``will not stay shared''. For example, 661*0Sstevel@tonic-gatethis won't work: 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate sub outer { 664*0Sstevel@tonic-gate my $x = $_[0] + 35; 665*0Sstevel@tonic-gate sub inner { return $x * 19 } # WRONG 666*0Sstevel@tonic-gate return $x + inner(); 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate 669*0Sstevel@tonic-gateA work-around is the following: 670*0Sstevel@tonic-gate 671*0Sstevel@tonic-gate sub outer { 672*0Sstevel@tonic-gate my $x = $_[0] + 35; 673*0Sstevel@tonic-gate local *inner = sub { return $x * 19 }; 674*0Sstevel@tonic-gate return $x + inner(); 675*0Sstevel@tonic-gate } 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gateNow inner() can only be called from within outer(), because of the 678*0Sstevel@tonic-gatetemporary assignments of the closure (anonymous subroutine). But when 679*0Sstevel@tonic-gateit does, it has normal access to the lexical variable $x from the scope 680*0Sstevel@tonic-gateof outer(). 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gateThis has the interesting effect of creating a function local to another 683*0Sstevel@tonic-gatefunction, something not normally supported in Perl. 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate=head1 WARNING 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gateYou may not (usefully) use a reference as the key to a hash. It will be 688*0Sstevel@tonic-gateconverted into a string: 689*0Sstevel@tonic-gate 690*0Sstevel@tonic-gate $x{ \$a } = $a; 691*0Sstevel@tonic-gate 692*0Sstevel@tonic-gateIf you try to dereference the key, it won't do a hard dereference, and 693*0Sstevel@tonic-gateyou won't accomplish what you're attempting. You might want to do something 694*0Sstevel@tonic-gatemore like 695*0Sstevel@tonic-gate 696*0Sstevel@tonic-gate $r = \@a; 697*0Sstevel@tonic-gate $x{ $r } = $r; 698*0Sstevel@tonic-gate 699*0Sstevel@tonic-gateAnd then at least you can use the values(), which will be 700*0Sstevel@tonic-gatereal refs, instead of the keys(), which won't. 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gateThe standard Tie::RefHash module provides a convenient workaround to this. 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gate=head1 SEE ALSO 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gateBesides the obvious documents, source code can be instructive. 707*0Sstevel@tonic-gateSome pathological examples of the use of references can be found 708*0Sstevel@tonic-gatein the F<t/op/ref.t> regression test in the Perl source directory. 709*0Sstevel@tonic-gate 710*0Sstevel@tonic-gateSee also L<perldsc> and L<perllol> for how to use references to create 711*0Sstevel@tonic-gatecomplex data structures, and L<perltoot>, L<perlobj>, and L<perlbot> 712*0Sstevel@tonic-gatefor how to use them to create objects. 713