File: perlreftut.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (843 lines) | stat: -rw-r--r-- 53,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlreftut - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlreftut</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-tutorials.html">Tutorials</a> &gt;
      
    
    perlreftut
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlreftut</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Who-Needs-Complicated-Data-Structures%3f">Who Needs Complicated Data Structures?</a><li><a href="#The-Solution">The Solution</a><li><a href="#Syntax">Syntax</a><ul><li><a href="#Making-References">Making References</a><li><a href="#Using-References">Using References</a><li><a href="#An-Example">An Example</a><li><a href="#Arrow-Rule">Arrow Rule</a></ul><li><a href="#Solution">Solution</a><li><a href="#The-Rest">The Rest</a><li><a href="#Summary">Summary</a><li><a href="#Credits">Credits</a><ul><li><a href="#Distribution-Conditions">Distribution Conditions</a></ul></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlreftut - Mark's very short tutorial about references</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>One of the most important new features in Perl 5 was the capability to
manage complicated data structures like multidimensional arrays and
nested hashes.  To enable these, Perl 5 introduced a feature called
<i>references</i>, and using references is the key to managing complicated,
structured data in Perl.  Unfortunately, there's a lot of funny syntax
to learn, and the main manual page can be hard to follow.  The manual
is quite complete, and sometimes people find that a problem, because
it can be hard to tell what is important and what isn't.</p>
<p>Fortunately, you only need to know 10% of what's in the main page to get
90% of the benefit.  This page will show you that 10%.</p>
<a name="Who-Needs-Complicated-Data-Structures%3f"></a><h1>Who Needs Complicated Data Structures?</h1>
<p>One problem that comes up all the time is needing a hash whose values are
lists.  Perl has hashes, of course, but the values have to be scalars;
they can't be lists.</p>
<p>Why would you want a hash of lists?  Let's take a simple example: You
have a file of city and country names, like this:</p>
<pre class="verbatim"><ol><li>	<span class="w">Chicago</span><span class="cm">,</span> <span class="w">USA</span></li><li>	<span class="w">Frankfurt</span><span class="cm">,</span> <span class="w">Germany</span></li><li>	<span class="w">Berlin</span><span class="cm">,</span> <span class="w">Germany</span></li><li>	<span class="w">Washington</span><span class="cm">,</span> <span class="w">USA</span></li><li>	<span class="w">Helsinki</span><span class="cm">,</span> <span class="w">Finland</span></li><li>	<span class="w">New</span> <span class="w">York</span><span class="cm">,</span> <span class="w">USA</span></li></ol></pre><p>and you want to produce an output like this, with each country mentioned
once, and then an alphabetical list of the cities in that country:</p>
<pre class="verbatim"><ol><li>	Finland: Helsinki.</li><li>	Germany: Berlin, Frankfurt.</li><li>	USA:  Chicago, New York, Washington.</li></ol></pre><p>The natural way to do this is to have a hash whose keys are country
names.  Associated with each country name key is a list of the cities in
that country.  Each time you read a line of input, split it into a country
and a city, look up the list of cities already known to be in that
country, and append the new city to the list.  When you're done reading
the input, iterate over the hash as usual, sorting each list of cities
before you print it out.</p>
<p>If hash values couldn't be lists, you lose.  You'd probably have to
combine all the cities into a single string somehow, and then when
time came to write the output, you'd have to break the string into a
list, sort the list, and turn it back into a string.  This is messy
and error-prone.  And it's frustrating, because Perl already has
perfectly good lists that would solve the problem if only you could
use them.</p>
<a name="The-Solution"></a><h1>The Solution</h1>
<p>By the time Perl 5 rolled around, we were already stuck with this
design: Hash values must be scalars.  The solution to this is
references.</p>
<p>A reference is a scalar value that <i>refers to</i> an entire array or an
entire hash (or to just about anything else).  Names are one kind of
reference that you're already familiar with.  Think of the President
of the United States: a messy, inconvenient bag of blood and bones.
But to talk about him, or to represent him in a computer program, all
you need is the easy, convenient scalar string "Barack Obama".</p>
<p>References in Perl are like names for arrays and hashes.  They're
Perl's private, internal names, so you can be sure they're
unambiguous.  Unlike "Barack Obama", a reference only refers to one
thing, and you always know what it refers to.  If you have a reference
to an array, you can recover the entire array from it.  If you have a
reference to a hash, you can recover the entire hash.  But the
reference is still an easy, compact scalar value.</p>
<p>You can't have a hash whose values are arrays; hash values can only be
scalars.  We're stuck with that.  But a single reference can refer to
an entire array, and references are scalars, so you can have a hash of
references to arrays, and it'll act a lot like a hash of arrays, and
it'll be just as useful as a hash of arrays.</p>
<p>We'll come back to this city-country problem later, after we've seen
some syntax for managing references.</p>
<a name="Syntax"></a><h1>Syntax</h1>
<p>There are just two ways to make a reference, and just two ways to use
it once you have it.</p>
<a name="Making-References"></a><h2>Making References</h2>
<a name="*Make-Rule-1*"></a><h3><b>Make Rule 1</b></h3>
<p>If you put a <code class="inline">\</code>
 in front of a variable, you get a
reference to that variable.</p>
<pre class="verbatim"><ol><li>    <span class="i">$aref</span> = \<span class="i">@array</span><span class="sc">;</span>         <span class="c"># $aref now holds a reference to @array</span></li><li>    <span class="i">$href</span> = \<span class="i">%hash</span><span class="sc">;</span>          <span class="c"># $href now holds a reference to %hash</span></li><li>    <span class="i">$sref</span> = \<span class="i">$scalar</span><span class="sc">;</span>        <span class="c"># $sref now holds a reference to $scalar</span></li></ol></pre><p>Once the reference is stored in a variable like $aref or $href, you
can copy it or store it just the same as any other scalar value:</p>
<pre class="verbatim"><ol><li>    <span class="i">$xy</span> = <span class="i">$aref</span><span class="sc">;</span>             <span class="c"># $xy now holds a reference to @array</span></li><li>    <span class="i">$p</span>[<span class="n">3</span>] = <span class="i">$href</span><span class="sc">;</span>           <span class="c"># $p[3] now holds a reference to %hash</span></li><li>    <span class="i">$z</span> = <span class="i">$p</span>[<span class="n">3</span>]<span class="sc">;</span>              <span class="c"># $z now holds a reference to %hash</span></li></ol></pre><p>These examples show how to make references to variables with names.
Sometimes you want to make an array or a hash that doesn't have a
name.  This is analogous to the way you like to be able to use the
string <code class="inline"><span class="q">&quot;\n&quot;</span></code>
 or the number 80 without having to store it in a named
variable first.</p>
<a name="*Make-Rule-2*"></a><h3><b>Make Rule 2</b></h3>
<p><code class="inline"><span class="s">[</span> <span class="w">ITEMS</span> <span class="s">]</span></code>
 makes a new, anonymous array, and returns a reference to
that array.  <code class="inline"><span class="s">{</span> <span class="w">ITEMS</span> <span class="s">}</span></code>
 makes a new, anonymous hash, and returns a
reference to that hash.</p>
<pre class="verbatim"><ol><li>    <span class="i">$aref</span> = <span class="s">[</span> <span class="n">1</span><span class="cm">,</span> <span class="q">&quot;foo&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <span class="n">13</span> <span class="s">]</span><span class="sc">;</span></li><li>    <span class="c"># $aref now holds a reference to an array</span></li><li></li><li>    <span class="i">$href</span> = <span class="s">{</span> <span class="w">APR</span> <span class="cm">=&gt;</span> <span class="n">4</span><span class="cm">,</span> <span class="w">AUG</span> <span class="cm">=&gt;</span> <span class="n">8</span> <span class="s">}</span><span class="sc">;</span></li><li>    <span class="c"># $href now holds a reference to a hash</span></li></ol></pre><p>The references you get from rule 2 are the same kind of
references that you get from rule 1:</p>
<pre class="verbatim"><ol><li>	<span class="c"># This:</span></li><li>	<span class="i">$aref</span> = <span class="s">[</span> <span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span> <span class="s">]</span><span class="sc">;</span></li><li></li><li>	<span class="c"># Does the same as this:</span></li><li>	<span class="i">@array</span> = <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">$aref</span> = \<span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>The first line is an abbreviation for the following two lines, except
that it doesn't create the superfluous array variable <code class="inline"><span class="i">@array</span></code>
.</p>
<p>If you write just <code class="inline"><span class="s">[</span><span class="s">]</span></code>
, you get a new, empty anonymous array.
If you write just <code class="inline"><span class="s">{</span><span class="s">}</span></code>
, you get a new, empty anonymous hash.</p>
<a name="Using-References"></a><h2>Using References</h2>
<p>What can you do with a reference once you have it?  It's a scalar
value, and we've seen that you can store it as a scalar and get it back
again just like any scalar.  There are just two more ways to use it:</p>
<a name="*Use-Rule-1*"></a><h3><b>Use Rule 1</b></h3>
<p>You can always use an array reference, in curly braces, in place of
the name of an array.  For example, <code class="inline"><span class="i">@</span>{<span class="i">$aref</span>}</code>
 instead of <code class="inline"><span class="i">@array</span></code>
.</p>
<p>Here are some examples of that:</p>
<p>Arrays:</p>
<pre class="verbatim"><ol><li>	@a		@{$aref}		An array</li><li>	reverse @a	reverse @{$aref}	Reverse the array</li><li>	$a[3]		${$aref}[3]		An element of the array</li><li>	$a[3] = 17;	${$aref}[3] = 17	Assigning an element</li></ol></pre><p>On each line are two expressions that do the same thing.  The
left-hand versions operate on the array <code class="inline"><span class="i">@a</span></code>
.  The right-hand
versions operate on the array that is referred to by <code class="inline"><span class="i">$aref</span></code>
.  Once
they find the array they're operating on, both versions do the same
things to the arrays.</p>
<p>Using a hash reference is <i>exactly</i> the same:</p>
<pre class="verbatim"><ol><li>	%h		%{$href}	      A hash</li><li>	keys %h		keys %{$href}	      Get the keys from the hash</li><li>	$h{'red'}	${$href}{'red'}	      An element of the hash</li><li>	$h{'red'} = 17	${$href}{'red'} = 17  Assigning an element</li></ol></pre><p>Whatever you want to do with a reference, <b>Use Rule 1</b> tells you how
to do it.  You just write the Perl code that you would have written
for doing the same thing to a regular array or hash, and then replace
the array or hash name with <code class="inline"><span class="s">{</span><span class="i">$reference</span><span class="s">}</span></code>
.  "How do I loop over an
array when all I have is a reference?"  Well, to loop over an array, you
would write</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li>          ...</li><li>        <span class="s">}</span></li></ol></pre><p>so replace the array name, <code class="inline"><span class="i">@array</span></code>
, with the reference:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$element</span> <span class="s">(</span><span class="i">@</span>{<span class="i">$aref</span>}<span class="s">)</span> <span class="s">{</span></li><li>          ...</li><li>        <span class="s">}</span></li></ol></pre><p>"How do I print out the contents of a hash when all I have is a
reference?"  First write the code for printing out a hash:</p>
<pre class="verbatim"><ol><li>        for <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span> <span class="s">{</span></li><li>          <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$key =&gt; $hash{$key}\n&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li></ol></pre><p>And then replace the hash name with the reference:</p>
<pre class="verbatim"><ol><li>        for <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%</span>{<span class="i">$href</span>}<span class="s">)</span> <span class="s">{</span></li><li>          <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$key =&gt; ${$href}{$key}\n&quot;</span><span class="sc">;</span></li><li>        <span class="s">}</span></li></ol></pre><a name="*Use-Rule-2*"></a><h3><b>Use Rule 2</b></h3>
<p><a href="#Use-Rule-1">Use Rule 1</a> is all you really need, because it tells
you how to do absolutely everything you ever need to do with references.
But the most common thing to do with an array or a hash is to extract a
single element, and the <a href="#Use-Rule-1">Use Rule 1</a> notation is
cumbersome.  So there is an abbreviation.</p>
<p><code class="inline"><span class="i">$</span>{<span class="i">$aref</span>}[<span class="n">3</span>]</code>
 is too hard to read, so you can write <code class="inline"><span class="i">$aref</span>-&gt;[<span class="n">3</span>]</code>

instead.</p>
<p><code class="inline"><span class="i">$</span>{<span class="i">$href</span>}{<span class="w">red</span>}</code>
 is too hard to read, so you can write
<code class="inline"><span class="i">$href</span>-&gt;{<span class="w">red</span>}</code>
 instead.</p>
<p>If <code class="inline"><span class="i">$aref</span></code>
 holds a reference to an array, then <code class="inline"><span class="i">$aref</span>-&gt;[<span class="n">3</span>]</code>
 is
the fourth element of the array.  Don't confuse this with <code class="inline"><span class="i">$aref</span>[<span class="n">3</span>]</code>
,
which is the fourth element of a totally different array, one
deceptively named <code class="inline"><span class="i">@aref</span></code>
.  <code class="inline"><span class="i">$aref</span></code>
 and <code class="inline"><span class="i">@aref</span></code>
 are unrelated the
same way that <code class="inline"><span class="i">$item</span></code>
 and <code class="inline"><span class="i">@item</span></code>
 are.</p>
<p>Similarly, <code class="inline"><span class="i">$href</span>-&gt;{<span class="q">&#39;red&#39;</span>}</code>
 is part of the hash referred to by
the scalar variable <code class="inline"><span class="i">$href</span></code>
, perhaps even one with no name.
<code class="inline"><span class="i">$href</span>{<span class="q">&#39;red&#39;</span>}</code>
 is part of the deceptively named <code class="inline"><span class="i">%href</span></code>
 hash.  It's
easy to forget to leave out the <code class="inline">-&gt;</code>
, and if you do, you'll get
bizarre results when your program gets array and hash elements out of
totally unexpected hashes and arrays that weren't the ones you wanted
to use.</p>
<a name="An-Example"></a><h2>An Example</h2>
<p>Let's see a quick example of how all this is useful.</p>
<p>First, remember that <code class="inline"><span class="s">[</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="s">]</span></code>
 makes an anonymous array containing
<code class="inline"><span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span></code>
, and gives you a reference to that array.</p>
<p>Now think about</p>
<pre class="verbatim"><ol><li>	<span class="i">@a</span> = <span class="s">(</span> <span class="s">[</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="s">]</span><span class="cm">,</span></li><li>               <span class="s">[</span><span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">6</span><span class="s">]</span><span class="cm">,</span></li><li>	       <span class="s">[</span><span class="n">7</span><span class="cm">,</span> <span class="n">8</span><span class="cm">,</span> <span class="n">9</span><span class="s">]</span></li><li>             <span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="i">@a</span></code>
 is an array with three elements, and each one is a reference to
another array.</p>
<p><code class="inline"><span class="i">$a</span>[<span class="n">1</span>]</code>
 is one of these references.  It refers to an array, the array
containing <code class="inline"><span class="s">(</span><span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="cm">,</span> <span class="n">6</span><span class="s">)</span></code>
, and because it is a reference to an array,
<a href="#Use-Rule-2">Use Rule 2</a> says that we can write <code class="inline"><span class="i">$a</span>[<span class="n">1</span>]-&gt;[<span class="n">2</span>]</code>

to get the third element from that array.  <code class="inline"><span class="i">$a</span>[<span class="n">1</span>]-&gt;[<span class="n">2</span>]</code>
 is the 6.
Similarly, <code class="inline"><span class="i">$a</span>[<span class="n">0</span>]-&gt;[<span class="n">1</span>]</code>
 is the 2.  What we have here is like a
two-dimensional array; you can write <code class="inline"><span class="i">$a</span>[<span class="w">ROW</span>]-&gt;[<span class="w">COLUMN</span>]</code>
 to get or
set the element in any row and any column of the array.</p>
<p>The notation still looks a little cumbersome, so there's one more
abbreviation:</p>
<a name="Arrow-Rule"></a><h2>Arrow Rule</h2>
<p>In between two <b>subscripts</b>, the arrow is optional.</p>
<p>Instead of <code class="inline"><span class="i">$a</span>[<span class="n">1</span>]-&gt;[<span class="n">2</span>]</code>
, we can write <code class="inline"><span class="i">$a</span>[<span class="n">1</span>][<span class="n">2</span>]</code>
; it means the
same thing.  Instead of <code class="inline"><span class="i">$a</span>[<span class="n">0</span>]-&gt;[<span class="n">1</span>] = <span class="n">23</span></code>
, we can write
<code class="inline"><span class="i">$a</span>[<span class="n">0</span>][<span class="n">1</span>] = <span class="n">23</span></code>
; it means the same thing.</p>
<p>Now it really looks like two-dimensional arrays!</p>
<p>You can see why the arrows are important.  Without them, we would have
had to write <code class="inline"><span class="i">$</span>{<span class="i">$a</span>[<span class="n">1</span>]}[<span class="n">2</span>]</code>
 instead of <code class="inline"><span class="i">$a</span>[<span class="n">1</span>][<span class="n">2</span>]</code>
.  For
three-dimensional arrays, they let us write <code class="inline"><span class="i">$x</span>[<span class="n">2</span>][<span class="n">3</span>][<span class="n">5</span>]</code>
 instead of
the unreadable <code class="inline"><span class="i">$</span>{<span class="i">$</span>{<span class="i">$x</span>[<span class="n">2</span>]}[<span class="n">3</span>]}[<span class="n">5</span>]</code>
.</p>
<a name="Solution"></a><h1>Solution</h1>
<p>Here's the answer to the problem I posed earlier, of reformatting a
file of city and country names.</p>
<pre class="verbatim"><ol><li>    <span class="n">1</span>   <a class="l_k" href="functions/my.html">my</a> <span class="i">%table</span><span class="sc">;</span></li><li></li><li>    <span class="n">2</span>   <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>    <span class="n">3</span>     <a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span></li><li>    <span class="n">4</span>     <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$city</span><span class="cm">,</span> <span class="i">$country</span><span class="s">)</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/, /</span><span class="sc">;</span></li><li>    <span class="n">5</span>     <span class="i">$table</span>{<span class="i">$country</span>} = <span class="s">[</span><span class="s">]</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$table</span>{<span class="i">$country</span>}<span class="sc">;</span></li><li>    <span class="n">6</span>     <a class="l_k" href="functions/push.html">push</a> <span class="i">@</span>{<span class="i">$table</span>{<span class="i">$country</span>}}<span class="cm">,</span> <span class="i">$city</span><span class="sc">;</span></li><li>    <span class="n">7</span>   <span class="s">}</span></li><li></li><li>    <span class="n">8</span>   <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$country</span> <span class="s">(</span><a class="l_k" href="functions/sort.html">sort</a> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%table</span><span class="s">)</span> {</li><li>    <span class="n">9</span>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$country: &quot;</span><span class="sc">;</span></li><li>   <span class="n">10</span>     <a class="l_k" href="functions/my.html">my</a> <span class="i">@cities</span> = <span class="i">@</span>{<span class="i">$table</span>{<span class="i">$country</span>}}<span class="sc">;</span></li><li>   <span class="n">11</span>     <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/join.html">join</a> <span class="q">&#39;, &#39;</span><span class="cm">,</span> <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@cities</span><span class="sc">;</span></li><li>   <span class="n">12</span>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;.\n&quot;</span><span class="sc">;</span></li><li>   <span class="n">13</span>	}</li></ol></pre><p>The program has two pieces: Lines 2-7 read the input and build a data
structure, and lines 8-13 analyze the data and print out the report.
We're going to have a hash, <code class="inline"><span class="i">%table</span></code>
, whose keys are country names,
and whose values are references to arrays of city names.  The data
structure will look like this:</p>
<pre class="verbatim"><ol><li>           %table</li><li>        +-------+---+</li><li>        |       |   |   +-----------+--------+</li><li>        |Germany| *----&gt;| Frankfurt | Berlin |</li><li>        |       |   |   +-----------+--------+</li><li>        +-------+---+</li><li>        |       |   |   +----------+</li><li>        |Finland| *----&gt;| Helsinki |</li><li>        |       |   |   +----------+</li><li>        +-------+---+</li><li>        |       |   |   +---------+------------+----------+</li><li>        |  USA  | *----&gt;| Chicago | Washington | New York |</li><li>        |       |   |   +---------+------------+----------+</li><li>        +-------+---+</li></ol></pre><p>We'll look at output first.  Supposing we already have this structure,
how do we print it out?</p>
<pre class="verbatim"><ol><li>    <span class="n">8</span>   <a class="l_k" href="functions/for.html">for</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$country</span> <span class="s">(</span><a class="l_k" href="functions/sort.html">sort</a> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%table</span><span class="s">)</span> {</li><li>    <span class="n">9</span>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$country: &quot;</span><span class="sc">;</span></li><li>   <span class="n">10</span>     <a class="l_k" href="functions/my.html">my</a> <span class="i">@cities</span> = <span class="i">@</span>{<span class="i">$table</span>{<span class="i">$country</span>}}<span class="sc">;</span></li><li>   <span class="n">11</span>     <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/join.html">join</a> <span class="q">&#39;, &#39;</span><span class="cm">,</span> <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@cities</span><span class="sc">;</span></li><li>   <span class="n">12</span>     <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;.\n&quot;</span><span class="sc">;</span></li><li>   <span class="n">13</span>	}</li></ol></pre><p><code class="inline"><span class="i">%table</span></code>
 is an ordinary hash, and we get a list of keys from it, sort
the keys, and loop over the keys as usual.  The only use of references
is in line 10.  <code class="inline"><span class="i">$table</span>{<span class="i">$country</span>}</code>
 looks up the key <code class="inline"><span class="i">$country</span></code>
 in the
hash and gets the value, which is a reference to an array of cities in
that country.  <a href="#Use-Rule-1">Use Rule 1</a> says that we can recover
the array by saying <code class="inline"><span class="i">@</span>{<span class="i">$table</span>{<span class="i">$country</span>}}</code>
.  Line 10 is just like</p>
<pre class="verbatim"><ol><li>	<span class="i">@cities</span> = <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>except that the name <code class="inline"><span class="w">array</span></code>
 has been replaced by the reference
<code class="inline"><span class="s">{</span><span class="i">$table</span>{<span class="i">$country</span>}<span class="s">}</span></code>
.  The <code class="inline"><span class="i">@</span></code>
 tells Perl to get the entire array.
Having gotten the list of cities, we sort it, join it, and print it
out as usual.</p>
<p>Lines 2-7 are responsible for building the structure in the first
place.  Here they are again:</p>
<pre class="verbatim"><ol><li>    <span class="n">2</span>   <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>    <span class="n">3</span>     <a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span></li><li>    <span class="n">4</span>     <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$city</span><span class="cm">,</span> <span class="i">$country</span><span class="s">)</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/, /</span><span class="sc">;</span></li><li>    <span class="n">5</span>     <span class="i">$table</span>{<span class="i">$country</span>} = <span class="s">[</span><span class="s">]</span> <a class="l_k" href="functions/unless.html">unless</a> <a class="l_k" href="functions/exists.html">exists</a> <span class="i">$table</span>{<span class="i">$country</span>}<span class="sc">;</span></li><li>    <span class="n">6</span>     <a class="l_k" href="functions/push.html">push</a> <span class="i">@</span>{<span class="i">$table</span>{<span class="i">$country</span>}}<span class="cm">,</span> <span class="i">$city</span><span class="sc">;</span></li><li>    <span class="n">7</span>   <span class="s">}</span></li></ol></pre><p>Lines 2-4 acquire a city and country name.  Line 5 looks to see if the
country is already present as a key in the hash.  If it's not, the
program uses the <code class="inline"><span class="s">[</span><span class="s">]</span></code>
 notation (<a href="#Make-Rule-2">Make Rule 2</a>) to
manufacture a new, empty anonymous array of cities, and installs a
reference to it into the hash under the appropriate key.</p>
<p>Line 6 installs the city name into the appropriate array.
<code class="inline"><span class="i">$table</span>{<span class="i">$country</span>}</code>
 now holds a reference to the array of cities seen
in that country so far.  Line 6 is exactly like</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/push.html">push</a> <span class="i">@array</span><span class="cm">,</span> <span class="i">$city</span><span class="sc">;</span></li></ol></pre><p>except that the name <code class="inline"><span class="w">array</span></code>
 has been replaced by the reference
<code class="inline"><span class="s">{</span><span class="i">$table</span>{<span class="i">$country</span>}<span class="s">}</span></code>
.  The <a href="functions/push.html">push ARRAY,LIST</a> adds a
city name to the end of the referred-to array.</p>
<p>There's one fine point I skipped.  Line 5 is unnecessary, and we can
get rid of it.</p>
<pre class="verbatim"><ol><li>    <span class="n">2</span>   <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>    <span class="n">3</span>     <a class="l_k" href="functions/chomp.html">chomp</a><span class="sc">;</span></li><li>    <span class="n">4</span>     <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$city</span><span class="cm">,</span> <span class="i">$country</span><span class="s">)</span> = <a class="l_k" href="functions/split.html">split</a> <span class="q">/, /</span><span class="sc">;</span></li><li>    <span class="n">5</span>   <span class="c">####  $table{$country} = [] unless exists $table{$country};</span></li><li>    <span class="n">6</span>     <a class="l_k" href="functions/push.html">push</a> <span class="i">@</span>{<span class="i">$table</span>{<span class="i">$country</span>}}<span class="cm">,</span> <span class="i">$city</span><span class="sc">;</span></li><li>    <span class="n">7</span>   <span class="s">}</span></li></ol></pre><p>If there's already an entry in <code class="inline"><span class="i">%table</span></code>
 for the current <code class="inline"><span class="i">$country</span></code>
,
then nothing is different.  Line 6 will locate the value in
<code class="inline"><span class="i">$table</span>{<span class="i">$country</span>}</code>
, which is a reference to an array, and push <code class="inline"><span class="i">$city</span></code>

into the array.  But what does it do when <code class="inline"><span class="i">$country</span></code>
 holds a key, say
<code class="inline"><span class="w">Greece</span></code>
, that is not yet in <code class="inline"><span class="i">%table</span></code>
?</p>
<p>This is Perl, so it does the exact right thing.  It sees that you want
to push <code class="inline"><span class="w">Athens</span></code>
 onto an array that doesn't exist, so it helpfully
makes a new, empty, anonymous array for you, installs it into
<code class="inline"><span class="i">%table</span></code>
, and then pushes <code class="inline"><span class="w">Athens</span></code>
 onto it.  This is called
<i>autovivification</i>--bringing things to life automatically.  Perl saw
that the key wasn't in the hash, so it created a new hash entry
automatically. Perl saw that you wanted to use the hash value as an
array, so it created a new empty array and installed a reference to it
in the hash automatically.  And as usual, Perl made the array one
element longer to hold the new city name.</p>
<a name="The-Rest"></a><h1>The Rest</h1>
<p>I promised to give you 90% of the benefit with 10% of the details, and
that means I left out 90% of the details.  Now that you have an
overview of the important parts, it should be easier to read the
<a href="perlref.html">perlref</a> manual page, which discusses 100% of the details.</p>
<p>Some of the highlights of <a href="perlref.html">perlref</a>:</p>
<ul>
<li>
<p>You can make references to anything, including scalars, functions, and
other references.</p>
</li>
<li>
<p>In <a href="#Use-Rule-1">Use Rule 1</a>, you can omit the curly brackets
whenever the thing inside them is an atomic scalar variable like
<code class="inline"><span class="i">$aref</span></code>
.  For example, <code class="inline"><span class="i">@$aref</span></code>
 is the same as <code class="inline"><span class="i">@</span>{<span class="i">$aref</span>}</code>
, and
<code class="inline"><span class="i">$$aref</span>[<span class="n">1</span>]</code>
 is the same as <code class="inline"><span class="i">$</span>{<span class="i">$aref</span>}[<span class="n">1</span>]</code>
.  If you're just starting
out, you may want to adopt the habit of always including the curly
brackets.</p>
</li>
<li>
<p>This doesn't copy the underlying array:</p>
<pre class="verbatim"><ol><li>        <span class="i">$aref2</span> = <span class="i">$aref1</span><span class="sc">;</span></li></ol></pre><p>You get two references to the same array.  If you modify
<code class="inline"><span class="i">$aref1</span>-&gt;[<span class="n">23</span>]</code>
 and then look at
<code class="inline"><span class="i">$aref2</span>-&gt;[<span class="n">23</span>]</code>
 you'll see the change.</p>
<p>To copy the array, use</p>
<pre class="verbatim"><ol><li>        <span class="i">$aref2</span> = <span class="s">[</span><span class="i">@</span>{<span class="i">$aref1</span>}<span class="s">]</span><span class="sc">;</span></li></ol></pre><p>This uses <code class="inline"><span class="s">[</span>...<span class="s">]</span></code>
 notation to create a new anonymous array, and
<code class="inline"><span class="i">$aref2</span></code>
 is assigned a reference to the new array.  The new array is
initialized with the contents of the array referred to by <code class="inline"><span class="i">$aref1</span></code>
.</p>
<p>Similarly, to copy an anonymous hash, you can use</p>
<pre class="verbatim"><ol><li>        <span class="i">$href2</span> = <span class="s">{</span><span class="i">%</span>{<span class="i">$href1</span>}<span class="s">}</span><span class="sc">;</span></li></ol></pre></li>
<li>
<p>To see if a variable contains a reference, use the
<a href="functions/ref.html">ref EXPR</a> function.  It returns true if its argument
is a reference.  Actually it's a little better than that: It returns
<code class="inline"><span class="w">HASH</span></code>
 for hash references and <code class="inline"><span class="w">ARRAY</span></code>
 for array references.</p>
</li>
<li>
<p>If you try to use a reference like a string, you get strings like</p>
<pre class="verbatim"><ol><li>	<span class="i">ARRAY</span><span class="s">(</span><span class="n">0x80f5dec</span><span class="s">)</span>   or    <span class="i">HASH</span><span class="s">(</span><span class="n">0x826afc0</span><span class="s">)</span></li></ol></pre><p>If you ever see a string that looks like this, you'll know you
printed out a reference by mistake.</p>
<p>A side effect of this representation is that you can use
<a href="perlop.html#Equality-Operators">eq </a> to see if two references refer to the
same thing.  (But you should usually use
<a href="perlop.html#Equality-Operators">== </a> instead because it's much faster.)</p>
</li>
<li>
<p>You can use a string as if it were a reference.  If you use the string
<code class="inline"><span class="q">&quot;foo&quot;</span></code>
 as an array reference, it's taken to be a reference to the
array <code class="inline"><span class="i">@foo</span></code>
.  This is called a <i>symbolic reference</i>.  The declaration
<a href="strict.html">use strict &#39;refs&#39; </a> disables this feature, which can cause
all sorts of trouble if you use it by accident.</p>
</li>
</ul>
<p>You might prefer to go on to <a href="perllol.html">perllol</a> instead of <a href="perlref.html">perlref</a>; it
discusses lists of lists and multidimensional arrays in detail.  After
that, you should move on to <a href="perldsc.html">perldsc</a>; it's a Data Structure Cookbook
that shows recipes for using and printing out arrays of hashes, hashes
of arrays, and other kinds of data.</p>
<a name="Summary"></a><h1>Summary</h1>
<p>Everyone needs compound data structures, and in Perl the way you get
them is with references.  There are four important rules for managing
references: Two for making references and two for using them.  Once
you know these rules you can do most of the important things you need
to do with references.</p>
<a name="Credits"></a><h1>Credits</h1>
<p>Author: Mark Jason Dominus, Plover Systems (<code class="inline"><span class="w">mjd</span>-<span class="w">perl</span>-<a class="l_k" href="functions/ref.html">ref</a>+<span class="i">@plover</span>.<span class="w">com</span></code>
)</p>
<p>This article originally appeared in <i>The Perl Journal</i>
( <a href="http://www.tpj.com/">http://www.tpj.com/</a> ) volume 3, #2.  Reprinted with permission.</p>
<p>The original title was <i>Understand References Today</i>.</p>
<a name="Distribution-Conditions"></a><h2>Distribution Conditions</h2>
<p>Copyright 1998 The Perl Journal.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples in these files are
hereby placed into the public domain.  You are permitted and
encouraged to use this code in your own programs for fun or for profit
as you see fit.  A simple comment in the code giving credit would be
courteous but is not required.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#Who-Needs-Complicated-Data-Structures%3f">Who Needs Complicated Data Structures?</a><li><a href="#The-Solution">The Solution</a><li><a href="#Syntax">Syntax</a><ul><li><a href="#Making-References">Making References</a><li><a href="#Using-References">Using References</a><li><a href="#An-Example">An Example</a><li><a href="#Arrow-Rule">Arrow Rule</a></ul><li><a href="#Solution">Solution</a><li><a href="#The-Rest">The Rest</a><li><a href="#Summary">Summary</a><li><a href="#Credits">Credits</a><ul><li><a href="#Distribution-Conditions">Distribution Conditions</a></ul></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlreftut';
  perldoc.pageAddress = 'perlreftut.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>