Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: fall through switch/case in perl

by TimToady (Parson)
on Sep 07, 2004 at 01:20 UTC ( [id://388901]=note: print w/replies, xml ) Need Help??


in reply to Re: fall through switch/case in perl
in thread fall through switch/case in perl

Here's the computed goto version:
eval { goto 'L'.($var+0) }; die "$var out of range"; L10: print "a"; L9: print "b"; L8: print "c"; L7: print "d"; L6: print "e"; L5: print "f"; L4: print "g"; L3: print "h"; L2: print "i"; L1: print "j"; print "\n";

Replies are listed 'Best First'.
Re^3: fall through switch/case in perl
by Aristotle (Chancellor) on Sep 07, 2004 at 01:25 UTC

    Shouldn't that be

    eval { goto 'L'.($var+0) }; goto L_default; L10: print "a"; L9: print "b"; L8: print "c"; L7: print "d"; L6: print "e"; L5: print "f"; L4: print "g"; L3: print "h"; L2: print "i"; L1: print "j"; L_default: print "\n";

    to preserve the semantics of the original code?

    Makeshifts last the longest.

Re^3: fall through switch/case in perl
by BrowserUk (Patriarch) on Sep 07, 2004 at 01:46 UTC

    The eval isn't required (unless I missed a subtlety?)

    #! perl -sw use strict; my $var = 7; goto 'CASE'.($var+0); die "$var out of range"; CASE10: print "a"; CASE9: print "b"; CASE8: print "c"; CASE7: print "d"; CASE6: print "e"; CASE5: print "f"; CASE4: print "g"; CASE3: print "h"; CASE2: print "i"; CASE1: print "j"; print "\n"; __END__ P:\test>junk defghij

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      The eval is necessary if you want to fully emulate the C semantic of not dieing if there is no matching case. Really, the full-on computed goto method (I should have just written this into my top-level response) is this (I think):
      { eval { goto "CASE$var" } or goto DEFAULT; CASE10: print "a"; CASE9: print "b"; CASE8: print "c"; CASE7: print "d"; CASE6: print "e"; CASE5: print "f"; CASE4: print "g"; CASE3: print "h"; CASE2: print "i"; CASE1: print "j"; DEFAULT: }
      Which, admittedly, is not very different from Aristotle's. The only real difference being the outter braces. The purpose of them is to avoid polluting the label name-space. (Also, I prefer the "goto X or goto DEFAULT" over the "goto X; goto DEFAULT" purely for aesthetic reasons. :-D)

      I sort of wonder why this isn't given as one of the ways of achieving C-like switch statement behavior in the perl docs? Oh, well... it's probably horribly inefficient or something (apart from just being too C-ish or something).

      ------------ :Wq Not an editor command: Wq

        Hmm. I think what your saying is that the eval is necessary to avoid a warning if $var doesn't contain a value for which there is a label defined?

        If so, then it still doesn't handle the case where $var is undefined.

        That can be handled by using a do BLOCK construct to disable warnings and adding a CASE0:

        for my $var ( undef, 1 .. 10, 'fred' ) { goto 'CASE' . do{ local $^W; $var + 0 }; CASE10: print "a"; CASE9: print "b"; CASE8: print "c"; CASE7: print "d"; CASE6: print "e"; CASE5: print "f"; CASE4: print "g"; CASE3: print "h"; CASE2: print "i"; CASE1: print "j"; print "\n"; CASE0: }
        I sort of wonder why this isn't given as one of the ways of achieving C-like switch statement behavior in the perl docs? Oh, well... it's probably horribly inefficient or something (apart from just being to C-ish or something).

        C-ish it may be (though that's not a good reason in my book if it is clearer), but inefficient it is not:

        P:\test>388915 ?> 1..10 Testing for vals [1..10] Rate if_cascade and_cascade goto_do goto_eval + goto if_cascade 11395/s -- -2% -11% -26% + -26% and_cascade 11627/s 2% -- -9% -24% + -25% goto_do 12845/s 13% 10% -- -17% + -17% goto_eval 15389/s 35% 32% 20% -- + -0% goto 15423/s 35% 33% 20% 0% + -- ?>

        Benchmark/results


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://388901]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (None)
    As of 2024-04-25 00:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found