nashr has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to perl and trying to optimize my very simplistic code. I think that using if..elsif conditionals is probably not the best method to test for multiple conditions. If I understand correctly, each condition will be tested every time no matter what the results. I'm looking for a method to execute a block of code based on a set condition. In BASIC (please forgive this reference) there was the on..goto conditional. In dbase coding, there was the do..case statement. Can anyone recommend a similar code concept for PERL please? Thanks in advance from a perl newbie.

UPDATE:
Wow! What a great community, and so FAST! Thanks for fast and relevant response. I think I'll research the hash table method (Thanks Dave). Just for more info, I've set myself a learning project to develop a perl sniffer. The project will be tracked at iptraffic if anyone is interested.

Replies are listed 'Best First'.
Re: PERL Conditionals
by davido (Cardinal) on Nov 09, 2004 at 16:28 UTC

    You have a few options, and which one you'll use depends on the nature of the conditional test, the actions you want to branch to, and the number of options, among other things. Here are a couple of examples:

    First, a block method:

    SWITCH: { $condition1 and do{ stuff(); last SWITCH; }; $condition2 and do{ other(); last SWITCH; ); default(); }

    Second, a hash table method:

    my %options = ( key1 => \&sub1, key2 => \&sub2, key3 => \&sub3 ); $options{$condition}->();

    Those are two very common variants. But there are many others, due to Perl's flexibility. Check perlsyn for more info.


    Dave

      Dave, Thanks for the tip. I am looking into the hash table method. I just wanted to pass along that perldoc is STILL down.

        perldoc.com gets a little flakey from time to time, for various reasons. Fortunately, if you have Perl, you have perldoc right on your own computer too. Try typing "perldoc perlintro", just for example. Also, with ActiveState Perl you have both the command line utility "perldoc", as well as an HTML version of the POD that is browsable with your favorite web browser.


        Dave

Re: PERL Conditionals
by dragonchild (Archbishop) on Nov 09, 2004 at 16:18 UTC
    First off, if..elsif..else will not test every single condition. It will only test enough conditions to find the one that matches. So, if you have
    if ($x == 1) { } elsif ($x == 2) { } elsif ($x == 3) { } else { }
    and $x is set to 2, then only the first two conditions will be tested. The third will be ignored.

    Second, you probably are looking for Switch.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: PERL Conditionals
by Fletch (Bishop) on Nov 09, 2004 at 16:17 UTC

    Basically, there isn't one. Read perldoc -q switch:

    How do I create a switch or case statement? This is explained in more depth in the perlsyn. Briefly, there +'s no official case statement, because of the variety of tests possib +le in Perl (numeric comparison, string comparison, glob comparison, r +egex matching, overloaded comparisons, ...). Larry couldn't decide +how best to do this, so he left it out, even though it's been on the wis +h list since perl1. ...
Re: PERL Conditionals
by fglock (Vicar) on Nov 09, 2004 at 16:20 UTC
Re: PERL Conditionals
by Happy-the-monk (Canon) on Nov 09, 2004 at 16:18 UTC

    Although in Perl (note the spelling), there also is a goto,

    you will probably be better off with subroutines and scope.

    Cheerio, Sören