in reply to switch statement for subroutines?

Use a hash; much easier to read than long if statements, and more extendable:
my %subhash = { do_run => \&do_run, do_walk => \&do_walk, do_skip => \&do_skip }; foreach ( @do_something ) { if ( defined $subhash{ $_ } ) { &{ $subhash{ $_ } }(); } }

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: switch statement for subroutines?
by abstracts (Hermit) on Jul 28, 2001 at 02:58 UTC
    Hello,

    I noticed you used the curley brackets instead of the regular brackets in the hash assignment, but that's okay ;)

    I guess doing the following should be more readable, no?

    foreach ( @do_something ) { defined $subhash{$_} and $subhash{$_}->() } # OR foreach ( @do_something ) { $subhash{$_}->() if defined $subhash{$_} }
    This way we avoid most of the ugly punctuation marks.

    Aziz