http://www.perlmonks.org?node_id=623269


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"; }