Okay, so switch is too cool for its own good eh :)
Thanks guys, now I can go back to my ugly old nested if's and not feel guilty! I always liked the switch/case construct in EBasic back in the CP/M - MP/M days, and then in C of course, always wanted one in Perl - I suppose I have to wait for Perl 6 for given/when.
jdtoronto | [reply] |
Not all is lost for Perl5. A hash containing subroutine refs can be equally readable, though fall-through can be difficult to achieve. It also has the advantage of having O(1) efficiency, just like switches in C (an interesting discussion of which is here).
In C switches, you trade off some flexibility for efficiency. Perl6 switches will allow any expression to be used as a case, so they're basically as efficient as a bunch of if/elsif statements (though I'm hoping simple cases will be optimized into something like a C switch).
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
: () { :|:& };:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] |