Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: fall through switch/case in perl

by etcshadow (Priest)
on Sep 07, 2004 at 00:27 UTC ( #388890=note: print w/replies, xml ) Need Help??


in reply to fall through switch/case in perl

Well, what the first couple folks said doesn't actually answer your question... they were answering what *would* have been your question if you *had* had break statements... I don't know if the original post was updated after the fact or if they simply did not actually read it... whatever.

The short answer is that there isn't anything that does this very well, and in the same way as C does. The thing to understand is that switch statements in C are actually just well-written GOTO statements! Likewise, the best way to do this might be GOTOs (as bad as that might sound)... or just coding it explicitly, something like:

for ( $var ) { my $go; ($go || $_ == 10) and $go++, print "a"; ($go || $_ == 9 ) and $go++, print "b"; ($go || $_ == 8 ) and $go++, print "c"; ($go || $_ == 7 ) and $go++, print "d"; ($go || $_ == 6 ) and $go++, print "e"; ($go || $_ == 5 ) and $go++, print "f"; ($go || $_ == 4 ) and $go++, print "g"; ($go || $_ == 3 ) and $go++, print "h"; ($go || $_ == 2 ) and $go++, print "i"; ($go || $_ == 1 ) and $go++, print "j"; }
------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: fall through switch/case in perl
by TimToady (Parson) on Sep 07, 2004 at 01:20 UTC
    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";

      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.

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2019-10-18 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?