<?xml version="1.0" encoding="windows-1252"?>
<node id="623269" title="Re: why use a coderef?" created="2007-06-25 18:11:40" updated="2007-06-25 14:11:40">
<type id="11">
note</type>
<author id="61104">
cLive ;-)</author>
<data>
<field name="doctext">
&lt;p&gt;All good responses so far. I'll agree, in this example, I can't see a reason though.&lt;/p&gt;

&lt;p&gt;Just to add, another useful instance is if you want to avoid a long nested "if/else" structure, eg, instead of:&lt;/p&gt;

&lt;code&gt;
if ($var eq 'one') 
{ 
    &amp;subOne; 
}
elsif ($var eq 'two') 
{ 
    &amp;subTwo; 
}

...

elsif ($var eq 'ten') { 
    &amp;subTen; 
}
else 
{
    die "Invalid \$var"; 
}
&lt;/code&gt;
use this instead
&lt;code&gt;
my %subs = ( one =&gt; \&amp;subOne,
             two =&gt; \&amp;subTwo,
             ...
             ten =&gt; \&amp;subTen,
           );

if ( exists $subs{$var} )
{
    &amp;{$subs{$var}};
}
else
{
    die "Invalid \$var";
}
&lt;/code&gt;</field>
<field name="root_node">
623162</field>
<field name="parent_node">
623162</field>
</data>
</node>
