in reply to elsif stupid question...

As a side note, if you have a long chain of elsif()'s then chances are that you may choose a "better logic" in the first place. Of course this is just a random observation and may well not be your actual case.

Replies are listed 'Best First'.
Re^2: elsif stupid question...
by Fletch (Bishop) on Apr 28, 2005 at 15:09 UTC

    Yeah, presuming an OOP context lots of branches may indicate you should use the "Replace Conditional with Polymorphism" refactoring. Push the different conditional behaviors down into a method in child classes and just call that method.

    But again, that presumes a set of circumstances which may not apply in this case.

      Yeah, presuming an OOP context lots of branches may indicate you should use the "Replace Conditional with Polymorphism" refactoring.
      Indeed, but not only limited to this case. Of course newbies coming from other languages happen to abuse them where e.g. a hash would have been enough, not to say the best thing! But then again it's not something one can know in advance, from a generic point of view. I only did that remark in the hope that it may have been useful to the OP or to anyone else who will subsequently read the thread.
Re^2: elsif stupid question...
by dmorelli (Scribe) on Apr 28, 2005 at 15:09 UTC
    Optionally, the nested ternary is good for long chains of if/elsif/else

    #! /usr/bin/perl -w use strict; (my $foo = shift) ||= 0; ($foo == 1) ? print "It's one\n" : ($foo == 2) ? print "It's two\n" : ($foo == 3) ? print "It's three\n" : print "It's nothing meaningful!\n";
      Personally I try to avoid using?: for its side effects, when not golfing - that is! Re the example you gave, a hash or an array would have provided a much neater solution.