I like this enough that I will be using it for simple case statements.

You can simplify the syntax of it somewhat and make it read almost like the C version by declaring a sub called switch. It's actually more flexible being able to use (some) strings as well as integers.

Updated: Added check for none label characters in the switch expression in view of Courages concerns, allthough in this version, they just caused the default action without the check, the error reporting is handy.

#! perl -slw use strict; sub switch{ die "Bad switch expression at @{[ caller() ]}\n" unless $_[0] =~ /^\w+$/; eval{ goto "case_$_[0]" } or goto default; } for my $expr ( 1 .. 10, 'fred', "1;system('rm -rf /')" ) { switch( $expr ); { case_1: print '1'; last; case_2: print '2'; last; case_3: print '3'; last; case_4: print '4'; last; case_5: ; case_6: print '5 or 6'; last; case_fred: print 'fred'; last; default: print "default"; } } __END__ P:\test>test 1 2 3 4 5 or 6 5 or 6 default default default default fred Bad switch expression at main P:\test\test.pl8 10

Two things to watch for. The semi-colon after the switch(expr); and the need for a semi-colon after a label without a body (as in case_5: ; above).

I'm actually not quite sure why this latter one is required, but it seems to be. Unless someone can tell me where it is written that you can't have two consequetive labels in perl code, I'd consider this a bug?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Re: Simple Switch statement by BrowserUk
in thread Simple Switch statement by knexus

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.