in reply to "" in hash as a default?
Great for building switch-type handlers with a default case.my %way_of = ( the_sword => sub{do('sword',@_)}, the_gun => sub{do('gun',@_)}, "" => sub{do('default',@_)}, ); my $what = $something; my $stuff = $something_else; &{$way_of{$what} || $way_of{""}}($stuff);
A hash lookup is very fast compared to evaluating a long if structure, especially as the number of elements increases.if ($what eq 'the_sword') { do('sword', $stuff); } elsif ($what eq 'the_gun') { do('gun', $stuff); } else { do('default', $stuff); }
|
|---|