in reply to Simple Switch statement

When I need a switch I normally do something like this:

my %switch = ( default => \&list_ads, submit_payment => \&save_payment, set_duration => \&set_duration, go_here => sub {print 'hi'}, ); my $value = "go_here"; if (defined($value) && exists($switch{$value})) { $switch{$value}->(); } else { $pages{'default'}->(); }

Replies are listed 'Best First'.
Re: Re: Simple Switch statement
by liz (Monsignor) on Sep 12, 2003 at 20:14 UTC
    FWIW, I usually write this:
    if (defined($value) && exists($switch{$value})) { $switch{$value}->(); } else { $switch{'default'}->(); # I assume $pages was a mistake }
    as:
    ($switch{$value} || $switch{'default'})->();

    Liz