Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^2: Switch/case as a dispatch table with C-style fall-through

by Roy Johnson (Monsignor)
on May 13, 2005 at 12:42 UTC ( #456697=note: print w/replies, xml ) Need Help??

in reply to Re: Switch/case as a dispatch table with C-style fall-through
in thread Switch/case as a dispatch table with C-style fall-through

Good things to think about.

While I was proud of how I got a break() into the mix, it smells more like a trick than a well-designed component of the tool. As you point out, it should really stop execution of the current sub as well as preventing execution of the fallthrough sub, and I don't think I can make it do that.

If the user had defined a break() sub of his own, it would be unavailable in the switch subs, masked by the one I defined. But outside of that, it would be perfectly functional. If the user wanted to use it in the switch, he'd have to take a reference to it and call that. I thought it was a reasonable compromise, but I think I have a better, break()-less design in mind, now.

In the K&R book on C, the double-edged sword of fallthrough is addressed. Programmers are encouraged to use it only to apply one code block to multiple conditions, rather than to have one code block fall through to another. That is how I plan to make my case statement operate. Suddenly, there's no more need for break().

For the exceptional cases where chaining is desired, I will provide a local $Case::this (or $Case::self, whatever...maybe $Case::chain) variable that the user can use for recursing or chaining cases. So instead of introducing new pseudo-keywords (and the compromises they bring), we get less-cluttered, DWIM normal operation, and a flexible facility for doing tricky things if needed. The features you requested would translate thus:
last return
next $Case::chain->('specified term'); return
redo goto &$Case::chain
I particularly like having an explicit target for fall-through, which specifically addresses the dangers of C-style fall-through. Oh, and I use $_ instead of $_[0] because that's where I think the current term should go.

I don't think I've designed anything impossible. I won't be able to implement it until next week, though.

Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://456697]
[stevieb]: 16.96 hours of runtime of a Raspberry Pi on a 12v, 7.2Ah battery :) Now to watch now long it takes to recharge the battery completely with my 18v solar cells in the sun :)
[stevieb]: my high-range estimate was 18 hours
[stevieb]: battery was at about 95% capacity when the test began
[stevieb]: in the meantime while it was on battery, I got my shift register issues fixed, as well as certain interrupt issues

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-03-26 15:23 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.