Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Apocalypse 4 on perl.com

by scott (Chaplain)
on Jan 19, 2002 at 03:19 UTC ( [id://139994]=note: print w/replies, xml ) Need Help??


in reply to Apocalypse 4 on perl.com

I thought the switch statement was completely cool until I saw default. Ick. I'd like this much better:

given EXPR { when EXPR { ... } when EXPR { ... } ... otherwise { ... } }

My 0.02$US.

Replies are listed 'Best First'.
Re: Re: Apocalypse 4 on perl.com
by adamsj (Hermit) on Jan 19, 2002 at 07:10 UTC
    I know what you mean, but given (sigh) how often this'll be used, I think it's better to Huffman it. If anyone has a brilliant idea for a shorter keyword than default, I hope they'll send it in.

    adamsj

    They laughed at Joan of Arc, but she went right ahead and built it. --Gracie Allen

      How about nothing?
      given ($foo) { when (1) {print "One";}; when (2) {print "Two";}; print "Didn't fit any case we know about"; }
      Indent to taste.

      UPDATE
      I don't think that "normal code" will often see a difference. While I can dream up situations where I think it matters, in all of them you are doing evil, awful things that you probably shouldn't. (Like using a default to shortcircuit out of a function.)

        That is already defined to work but doesn't solve the problem of the presense of a real "default" block changing the handling of exceptions.

                - tye (but my friends call me "Tye")
      If anyone has a brilliant idea for a shorter keyword than default, I hope they'll send it in.
      Not shorter, but I think it's easier to read and a lot better:
      given $foo { when 1 { ... } when 2 { ... } when we have tried all other options and really have nothing left but this last resort { ... } }


      :)

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      I pondered that for a bit but the best I could to was orelse. But that's even worse. :(.

Re^2: Apocalypse 4 on perl.com
by Aristotle (Chancellor) on Jan 20, 2002 at 13:43 UTC
    By Larry's explanation, the idea of default was to prevent given's which don't have any when's from looking funny.
    Makeshifts last the longest.

      Hmmm ...

      given EXPR { otherwise { ... } } given EXPR { default { ... } }

      default definitely looks better here. I wonder how often people write empty switch statements though? Then again, given isn't your father's switch so past coding patterns may very well not apply.

Perl 6 - A Better Switch-Case Default?
by metadoktor (Hermit) on Jan 26, 2002 at 16:54 UTC
    Hmmm...how about...
    given EXPR { when EXPR { } when EXPR { } ... japh { } }
    ha, ha...

    metadoktor

    "The doktor is in."

      You are missing a colon.
      given EXPR { when EXPR { } when EXPR { } ... japh: { } }
      Happy to help,
      Ben

      :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 16:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found