in reply to Use of SWITCH vs IF ELSE in speed and readability

Another approach is to use a dispatch table - a hash of code references,

my $dtbl = { 1 => sub { $abc = 1 }, 2 => sub { $def = 1 }, 3 => sub { $xyz = 1 }, }; exists $dtbl->{$x} ? $dtbl->{$x}() : $nothing = 1;

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Use of SWITCH vs IF ELSE in speed and readability
by halley (Prior) on Sep 01, 2003 at 03:37 UTC
    Just a style difference, but I really dislike using the ternary ? : for statements (code which is of irrelevant or void context, performed for their side-effects). It's all just code and doesn't matter inside the machine, but to me, the ternary is intended to express a selection of either one expression or the other.

    --
    [ e d @ h a l l e y . c c ]