<?xml version="1.0" encoding="windows-1252"?>
<node id="69927" title="References quick reference" created="2001-04-04 22:39:52" updated="2005-08-15 10:36:09">
<type id="956">
perltutorial</type>
<author id="22609">
tye</author>
<data>
<field name="doctext">
&lt;p&gt;
I find that the following 4 simple rules cover all of the ways to dereference data structure references.  Having the rules spelled out so simply has made using references much less confusing to me.
&lt;ol&gt;&lt;li&gt;
To use a reference, say $ref, you put &lt;code&gt;{$ref}&lt;/code&gt;
in place of a variable &lt;em&gt;name&lt;/em&gt; ( not counting the &lt;code&gt;$&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;, or &lt;code&gt;@&lt;/code&gt; ).  That is, replace the variable name with the reference enclosed in braces:
&lt;code&gt;
    $scalar        ${$sRef}
    @array         @{$aRef}
    $array[0]      ${$aRef}[0]
    $#array        $#{$aRef}
    %hash          %{$hRef}
    $hash{KEY}     ${$hRef}{KEY}
    @hash{@list}   @{$hRef}{@list}
&lt;/code&gt;
Note that this works for any expression that returns a reference, not just the simple examples above:
&lt;code&gt;
    ${$sRefs[0]}   ${$sRefs{key}}   ${getScalarRef()}
    @{$aRefs[0]}   @{$aRefs{key}}   @{getArrayRef()}
    %{$hRefs[0]}   %{$hRefs{key}}   %{getHashRef()}
    @{ $bool ? \%hash1 : \%hash2 }{qw(some keys)}
&lt;/code&gt;
&lt;/li&gt;&lt;li&gt;
If the reference is held in a simple scalar variable, then
the braces, { and }, can be dropped:
&lt;code&gt;
    $scalar        $$sRef
    @array         @$aRef
    $array[0]      $$aRef[0]
    $#array        $#$aRef
    %hash          %$hRef
    $hash{KEY}     $$hRef{KEY}
    @hash{@list}   @$hRef{@list}
&lt;/code&gt;&lt;/li&gt;&lt;li&gt;
If you are getting a &lt;em&gt;scalar&lt;/em&gt; from a hash or an array, then you can replace &lt;code&gt;${$ref}&lt;/code&gt; with &lt;code&gt;$ref-&gt;&lt;/code&gt;:
&lt;code&gt;
    $array[0]     ${$aRef}[0]     $aRef-&gt;[0]
    $hash{KEY}    ${$hRef}{KEY}   $hRef-&gt;{KEY}
&lt;/code&gt;&lt;/li&gt;&lt;li&gt;
If the reference is in a hash or an array (and you are getting back a scalar), then you can drop the &lt;code&gt;-&gt;&lt;/code&gt; between the adjacent &lt;code&gt;[0]&lt;/code&gt; and/or &lt;code&gt;{KEY}&lt;/code&gt; parts:
&lt;code&gt;
    ${$aRef-&gt;[0]}[1]    $aRef-&gt;[0]-&gt;[1]    $aRef-&gt;[0][1]
    ${$aRef-&gt;[0]}{KEY}  $aRef-&gt;[0]-&gt;{KEY}  $aRef-&gt;[0]{KEY}
    ${$hRef-&gt;{KEY}}[1]  $hRef-&gt;{KEY}-&gt;[1]  $hRef-&gt;{KEY}[1]
    ${$hRef-&gt;{A}}{B}    $hRef-&gt;{A}-&gt;{B}    $hRef-&gt;{A}{B}
&lt;/code&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;
I hope others find this as helpful as I have.
&lt;/p&gt;&lt;p&gt;&lt;small&gt;
Thanks to [arturo] for suggesting that I turn [id://55329] into a tutorial.
&lt;/small&gt;&lt;/p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;- 
&lt;a href="/index.pl?node=tye&amp;lastnode_id=1072"&gt;tye&lt;/a&gt; 
(but my friends call me "Tye")
&lt;p&gt;
&lt;small&gt;Thanks to [kutsu] for pointing out that I've had &lt;code&gt;${hRef}{KEY}&lt;/code&gt; instead of &lt;code&gt;${$hRef}{KEY}&lt;/code&gt; for all these years.&lt;/small&gt;
&lt;/p&gt;</field>
</data>
</node>
