Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: why use a coderef?

by cLive ;-) (Parson)
on Jun 25, 2007 at 22:11 UTC ( #623269=note: print w/ replies, xml ) Need Help??


in reply to why use a coderef?

All good responses so far. I'll agree, in this example, I can't see a reason though.

Just to add, another useful instance is if you want to avoid a long nested "if/else" structure, eg, instead of:

if ($var eq 'one') { &subOne; } elsif ($var eq 'two') { &subTwo; } ... elsif ($var eq 'ten') { &subTen; } else { die "Invalid \$var"; }
use this instead
my %subs = ( one => \&subOne, two => \&subTwo, ... ten => \&subTen, ); if ( exists $subs{$var} ) { &{$subs{$var}}; } else { die "Invalid \$var"; }


Comment on Re: why use a coderef?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://623269]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-09-21 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls