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


if (defined(foo_2)){ do bar; }; elsif (defined(foo_1)){ do foo_2; }; else { do foo_1; };

Replies are listed 'Best First'.
Re: Is this an efficient way check for certain conditions?
by blakem (Monsignor) on Aug 23, 2001 at 12:42 UTC
    The idea looks fine, but the (pseudo?)code needs a bit of improvement.

    First, 'foo_1' and 'foo_2' are probably supposed to be scalars, they need to be prefaced with a dollar sign ($foo_1,$foo_2). Second, your if..elsif..else construct has a few extra semicolons in it. Third, I think your "do" lines are really placeholders, which need to be commented out. Fourth, the "do" statements don't seem to be in the right place. After fixing that we now have:

    if (defined($foo_2)){ # do something with $foo_2 } elsif (defined(foo_1)){ # do something with $foo_1 } else { # take defalut action }
    (note, indentation of if/else is one of those holy wars... some might prefer a different indentation)

    -Blake

Re: Is this an efficient way check for certain conditions?
by Sifmole (Chaplain) on Aug 23, 2001 at 16:19 UTC
    In the case where you only have two or three cases, this works fine. If you have a case where you have many conditions you can use a dispatch table as such.
    my %dispatch = ('foo_1' => \&bar, 'foo_2' => \&foo_2, 'bar_3' => \&foo_2, 'bar_4' => \&foo_2, 'blah_5' => \&foo_2, 'default' => \&default, ); (defined $dispatch{$value}) ? &{$dispatch{$value}}() : &{$dispatch{'default'}}();
Re: Is this an efficient way check for certain conditions?
by htoug (Deacon) on Aug 23, 2001 at 13:24 UTC
    You mean "is if..elseif..else" efficient?

    Yes it is!

    You can sometimes reorder your conditions to ensure that the most common (or least expensive to evaluate) comes first, but apart from that there are no glaring efficeiency problems here.

    You do have a syntax error though. The semicolons after the if-clause and the endif-clause are not only superfluous but downright wrong.
    The entire 'if(..){...}elseif(..){..}else{..}' is one statement. Note the braces and parentheses. They are all required.

    if (defined(foo_2)){ do bar; } elsif (defined(foo_1)){ do foo_2; } else { do foo_1; };
    Would be more correct. But still not quite there. Variables have a '$' in front of them (mostly), and do is not the preferred way of calling subroutines.....

Re: Is this an efficient way check for certain conditions?
by MZSanford (Curate) on Aug 23, 2001 at 12:47 UTC
    I think what you are asking about (but not sure) is getting away from the if/else block. You may be looking for ...
    $number = 1; $code = "foo_".$number; &{$code}();
    But, i usually suggest shying away from that unless absolutly needed, because it can get terribly difficult to maintain. But, when it is needed, it is a good tool
    can't sleep clowns will eat me
    -- MZSanford
Re: Is this an efficient way check for certain conditions?
by George_Sherston (Vicar) on Aug 23, 2001 at 12:47 UTC
    Err... I don't think it works, does it? Because in the case where the elsif returns false, the block in the else won't execute. But perhaps I've not understood what it's meant to do... if you have the chance I'd be interested to know what conditions it's meant to check for, and also that I'm right in thinking foo_1, foo_2 and bar are subroutines.

    § George Sherston
      I posted the actual code snippet in my node