Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^6: fall through switch/case in perl

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


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

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?
No, I'm saying that goto $label dies if the label has not been defined. That is not how it works in C. In C, if you have not provided a case that matches your switch value, it jumps to the default case. That's why there's an eval, to prevent the die (not warning).
[sstone@granite sstone]$ perl -le 'print 1; goto "FOO"; print 2' 1 Can't find label FOO at -e line 1. [sstone@granite sstone]$
As far as preventing a warning on undef, well... I'd imagine that that should fit into whatever scheme you are using around undefs: warn if you have undef warnings on, else not. And there's nothing about stringifying $var that changes that semantic, so all seems well and good with my method.
------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^7: fall through switch/case in perl
by BrowserUk (Pope) on Sep 07, 2004 at 01:57 UTC

    Fair enough, but in TimToady's original, he had 'CASE'.($var+0) which avoids dieing.

    P:\test>perl -le" print 1; $var = 'foo'; goto 'CASE'.($var+0); CASE0: +print 2;" 1 2

    And that allows CASE0: to become the default.

    The only situation this doesn't cover is when $var is undef'd; but then none of the other solutions handle that (except the do{ local $^W; $var + 0 }; I suggested).


    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
      Yes, but what if $var were 11? (Insert Spinal-Tap joke here).

      Also, as I said: when it comes to the behavior of undef (as the value of $var), shouldn't the surrounding scope's notion of whether or not to warn on undef apply here? Why should this be different (in that specific regard: i.e., whether or not to warn on using an undefined value) than, say, $var == 10 (etetera). I would think that if warnings were on, it SHOULD warn, and if warnings were not on, then it should NOT warn... and that's what you get. The presence of an eval doesn't change that fact.

      I'm confused... are we arguing or agreeing? :-D

      ------------ :Wq Not an editor command: Wq
        I'm confused... are we arguing or agreeing? :-D

        Agreeing--with gusto :).

        Yes, but what if $var were 11?

        Ah! I (stupidly) didn't test that one. Point made.

        (Insert Spinal-Tap joke here).

        Actually, if you're so tough that you can make light of a spinal tap, I think I'm gonna agree with you--whatever you say :).


        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
Re^7: ** fall through switch/case in perl
by Anonymous Monk on Sep 07, 2004 at 04:43 UTC
    goto qw [CASE1 CASE2 CASE3 CASE4 CASE5 CASE6 CASE7 CASE8 CASE9 CASE10] [$var - 1] || "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"; print "\n"; default:
      You missed the $var=undef and $var<=0 cases.
      goto (undef, 'CASE01' .. 'CASE10')[abs($var) == $var ? $var+0 : 0]||"d +efault"; CASE10: print "a"; CASE09: print "b"; CASE08: print "c"; CASE07: print "d"; CASE06: print "e"; CASE05: print "f"; CASE04: print "g"; CASE03: print "h"; CASE02: print "i"; CASE01: print "j"; print "\n"; default:

      Search, Ask, Know

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2019-09-15 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (180 votes). Check out past polls.

    Notices?