This specific example isn't very good in my opinion since there is only possible matching case per result. However, imagine a situation where you have, say, the numbers 1 till 100, and out of those numbers ie 6, 12, 13, 37, 44, 68 and 99 all need to result in performing some sort of action. Now in a situation like that a switch statement would look something like:
switch($number) { case 6, 12, 13, 37, 44, 68 { \&subroutine(@args); } case 1, 29, 77, 98 { \&othersub(@args); } default { sleep(1); } }
whereas if you needed to use if-elsif-else it'd be something like:
if(($number == 6) || ($number == 12) || ($number == 13) || ($number == + 37) || ($number == 44) || ($number == 68)) { \&subroutine(@args) } elsif(($number == 1) || ($number == 29) || ($number == 77) || ($number + == 98)) { \&othersub(@args); } else { sleep(1); }
In this example I actually resorted to copy-pasting $number, having to copy-paste a piece of code over and over again is in itself a sign that what you're doing is inefficient.

Now, the method i personally use atm is to create a hash(or in this example above I'd even have used an array because all the case values are numerical) that would look something like this(copied from actual code I use atm):

my %actions = ( '001' => \&_rpl_welcome, '005' => \&_rpl_isupport, '332' => \&_rpl_topic, '333' => \&_rpl_topicdetails, '352' => \&_rpl_whoreply, '353' => \&_rpl_namreply, 'JOIN' => \&_msg_join, 'PART' => \&_msg_part, 'KICK' => \&_msg_part, 'MODE' => \&_msg_mode, 'QUIT' => \&_msg_quit, ); sub process { my($self, $line) = @_; my @text = split(/\s/, $line); if($actions{$text[1]}) { return(&{$actions{$text[1]}}($self, $line, @text)); } }
Long story short, the main argument in favor of a switch-like statement is not having to type $wday for each and every comparison. Repetition = bad :-)

Remember rule one...


In reply to Re: using Switch; by Forsaken
in thread using Switch; by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.