For scoping reasons, I would change:
sub switch{ eval{ goto "case_$_[0]" } or goto default; } for my $expr ( 1 .. 10, 'fred' ) { switch( $expr ); {
to:
my $switch = sub { eval{ goto "case_$_[0]" } or goto default; }; for my $expr ( 1 .. 10, 'fred' ) { $switch->( $expr ); {
but other than that, I must say I like this idiom as well. If you're not interested in performance, that is. Because the performance of the switch like this, is abysmal compared to an identical if/elsif/else structure, as this little benchmark shows:
use strict; use Benchmark; timethese( 50000,{ switch => sub { sub switch{ eval{ goto "case_$_[0]" } or goto default; } for my $expr ( 1 .. 10, 'fred' ) { switch( $expr ); { case_1: print STDERR '1'; last; case_2: print STDERR '2'; last; case_3: print STDERR '3'; last; case_4: print STDERR '4'; last; case_5: ; case_6: print STDERR '5 or 6'; last; case_fred: print STDERR 'fred'; last; default: print STDERR "default"; } } }, if => sub { for my $expr ( 1 .. 10, 'fred' ) { if ($expr eq '1') {print STDERR '1'} elsif ($expr eq '2') {print STDERR '2'} elsif ($expr eq '3') {print STDERR '3'} elsif ($expr eq '4') {print STDERR '4'} elsif ($expr eq '5' or $expr eq '6') {print STDERR '5 or 6'} elsif ($expr eq 'fred') {print STDERR 'fred'} else {print STDERR "default"} } }, } );
Please note that I changed the print statements to "print STDERR" so that I could send them to the bitbucket. The result of the benchmark:
Benchmark: timing 50000 iterations of if, switch if: 7 wallclock secs ( 5.41 usr + 0.00 sys = 5.41 CPU) @ 92 +42.14/s (n=50000) switch: 43 wallclock secs (39.85 usr + 0.00 sys = 39.85 CPU) @ 12 +54.71/s (n=50000)

So the switch structure is at least 7 times as a comparable id/elsif/else structure. So don't put this in very deep and tight loops!

Liz

Edit by tye, change PRE to CODE around long lines


In reply to Re: Re: Simple Switch statement by liz
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.