perlreftut.html 51.8 KB
Newer Older
1 2 3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
4 5
<head>
  <title>perlreftut - perldoc.perl.org</title>
6 7
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
8 9 10
  <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">
11 12
</head>

13 14 15 16 17 18 19
<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>
20 21 22 23 24 25 26 27 28
	<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>
29
      </div>
30 31 32 33
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
34 35
            
	    <div class="side_panel doc_panel">
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
              <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>
54
                <li><a href="index-internals.html">Internals</a>
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
                <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>
277 278 279 280 281 282 283 284
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
285 286 287 288 289 290 291 292 293
          </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">
294
                Perl 5 version 26.0 documentation
295
              </div>
296
              <div class="page_links" id="page_links_top">
297 298
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
299 300
              </div>
	      <div class="page_links" id="page_links_bottom">
301
		
302
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
303
		
304 305 306 307 308 309 310
                <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>
311 312 313 314 315 316 317 318 319 320 321
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-tutorials.html">Tutorials</a> &gt;
      
    
    perlreftut
  
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
            </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>
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

	    <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>
  
379 380 381 382 383 384
	    <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>
385
<p>perlreftut - Mark's very short tutorial about references</p>
386 387 388 389
<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
390
<i>references</i>, and using references is the key to managing complicated,
391
structured data in Perl.  Unfortunately, there's a lot of funny syntax
392 393
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
394 395
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
396 397
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>
398 399 400
<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>
401
<p>Why would you want a hash of lists?  Let's take a simple example: You
402
have a file of city and country names, like this:</p>
403
<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
404
once, and then an alphabetical list of the cities in that country:</p>
405
<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
406 407 408
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
409
country, and append the new city to the list.  When you're done reading
410 411
the input, iterate over the hash as usual, sorting each list of cities
before you print it out.</p>
412
<p>If hash values couldn't be lists, you lose.  You'd probably have to
413
combine all the cities into a single string somehow, and then when
414
time came to write the output, you'd have to break the string into a
415
list, sort the list, and turn it back into a string.  This is messy
416
and error-prone.  And it's frustrating, because Perl already has
417 418 419 420 421 422 423 424
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
425
reference that you're already familiar with.  Think of the President
426 427
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
428 429 430 431
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
432 433 434 435
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>
436 437
<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
438
an entire array, and references are scalars, so you can have a hash of
439 440 441
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
442 443 444 445 446
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>
447
<a name="*Make-Rule-1*"></a><h3><b>Make Rule 1</b></h3>
448 449 450
<p>If you put a <code class="inline">\</code>
 in front of a variable, you get a
reference to that variable.</p>
451
<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
452
can copy it or store it just the same as any other scalar value:</p>
453
<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.
454
Sometimes you want to make an array or a hash that doesn't have a
455 456 457 458
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>
459
<a name="*Make-Rule-2*"></a><h3><b>Make Rule 2</b></h3>
460
<p><code class="inline"><span class="s">[</span> <span class="w">ITEMS</span> <span class="s">]</span></code>
461
 makes a new, anonymous array, and returns a reference to
462
that array.  <code class="inline"><span class="s">{</span> <span class="w">ITEMS</span> <span class="s">}</span></code>
463 464
 makes a new, anonymous hash, and returns a
reference to that hash.</p>
465
<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
466
references that you get from rule 1:</p>
467
<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
468
that it doesn't create the superfluous array variable <code class="inline"><span class="i">@array</span></code>
469 470 471 472 473 474
.</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>
475 476
<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
477
again just like any scalar.  There are just two more ways to use it:</p>
478
<a name="*Use-Rule-1*"></a><h3><b>Use Rule 1</b></h3>
479 480 481 482 483 484
<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>
485
<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
486 487 488 489
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
490
they find the array they're operating on, both versions do the same
491 492
things to the arrays.</p>
<p>Using a hash reference is <i>exactly</i> the same:</p>
493
<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
494 495 496
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>
497 498
.  "How do I loop over an
array when all I have is a reference?"  Well, to loop over an array, you
499
would write</p>
500
<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>
501
, with the reference:</p>
502
<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
503
reference?"  First write the code for printing out a hash:</p>
504 505
<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>
506 507 508 509 510
<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>
511 512 513 514
<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>
515
<p><code class="inline"><span class="i">$</span>{<span class="i">$href</span>}{<span class="w">red</span>}</code>
516
 is too hard to read, so you can write
517
<code class="inline"><span class="i">$href</span>-&gt;{<span class="w">red</span>}</code>
518 519 520 521
 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
522
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>
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
,
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>
538
 hash.  It's
539
easy to forget to leave out the <code class="inline">-&gt;</code>
540
, and if you do, you'll get
541
bizarre results when your program gets array and hash elements out of
542
totally unexpected hashes and arrays that weren't the ones you wanted
543 544
to use.</p>
<a name="An-Example"></a><h2>An Example</h2>
545
<p>Let's see a quick example of how all this is useful.</p>
546 547 548 549 550
<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>
551 552
<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
553 554 555 556 557
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,
558 559 560
<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>
561 562 563
 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
564
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>
565 566
 to get or
set the element in any row and any column of the array.</p>
567
<p>The notation still looks a little cumbersome, so there's one more
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
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>
588
<p>Here's the answer to the problem I posed earlier, of reformatting a
589
file of city and country names.</p>
590
<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
591
structure, and lines 8-13 analyze the data and print out the report.
592
We're going to have a hash, <code class="inline"><span class="i">%table</span></code>
593 594 595
, whose keys are country names,
and whose values are references to arrays of city names.  The data
structure will look like this:</p>
596
<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,
597
how do we print it out?</p>
598 599 600 601
<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>
602
 looks up the key <code class="inline"><span class="i">$country</span></code>
603 604 605 606
 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>
607
.  Line 10 is just like</p>
608
<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>
609 610 611 612 613 614 615 616
 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>
617
<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
618
country is already present as a key in the hash.  If it's not, the
619
program uses the <code class="inline"><span class="s">[</span><span class="s">]</span></code>
620 621 622
 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>
623 624 625 626
<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>
627
<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>
628 629
 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>
630 631
.  The <a href="functions/push.html">push ARRAY,LIST</a> adds a
city name to the end of the referred-to array.</p>
632
<p>There's one fine point I skipped.  Line 5 is unnecessary, and we can
633
get rid of it.</p>
634
<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>
635 636 637 638
 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>
639 640 641 642 643
, 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>
644 645 646
, 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
647
to push <code class="inline"><span class="w">Athens</span></code>
648
 onto an array that doesn't exist, so it helpfully
649 650
makes a new, empty, anonymous array for you, installs it into
<code class="inline"><span class="i">%table</span></code>
651
, and then pushes <code class="inline"><span class="w">Athens</span></code>
652
 onto it.  This is called
653
<i>autovivification</i>--bringing things to life automatically.  Perl saw
654
that the key wasn't in the hash, so it created a new hash entry
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
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>
671 672 673 674
<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>
675
 is the same as <code class="inline"><span class="i">@</span>{<span class="i">$aref</span>}</code>
676 677 678 679 680 681
, 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>
682 683
</li>
<li>
684
<p>This doesn't copy the underlying array:</p>
685
<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
686 687 688
<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>
689
 you'll see the change.</p>
690
<p>To copy the array, use</p>
691
<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>
692 693 694 695 696 697
 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>
698
<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>
699
<li>
700 701 702 703
<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>
704
 for hash references and <code class="inline"><span class="w">ARRAY</span></code>
705
 for array references.</p>
706 707 708
</li>
<li>
<p>If you try to use a reference like a string, you get strings like</p>
709
<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
710
printed out a reference by mistake.</p>
711 712 713 714
<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>
715 716 717 718
</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>
719
 as an array reference, it's taken to be a reference to the
720
array <code class="inline"><span class="i">@foo</span></code>
721 722 723
.  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>
724 725 726 727
</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
728
that, you should move on to <a href="perldsc.html">perldsc</a>; it's a Data Structure Cookbook
729 730 731 732 733 734 735 736 737
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>
738
<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>
739 740 741 742 743 744 745 746 747 748 749 750 751
)</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>
752 753 754 755




756 757 758 759 760
  <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>
761
    </div>
762
    <div id="page_index_content" class="hud_content">
763 764
      <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>
765 766 767 768
    <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>
769 770 771
    </div>
  </div>

772 773 774 775 776 777 778

	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
779
      </div>
780
      
781 782 783 784 785 786 787 788 789
    <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">
790
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
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
	    </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">
827
      </div>
828
    </div>
829
      
830
    </div>
831 832
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
833 834 835 836 837
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlreftut';
  perldoc.pageAddress = 'perlreftut.html';
  perldoc.contentPage = 1;
838 839
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
840 841 842
</script>
    
  </body>
843
</html>