in reply to Re^6: No braces
in thread No braces
Note that neither Switch.pm nor Perl6 switches offer O(1) efficiency, since they can't be implemented using jump tables like C switches are. For that, you need to use a dispatch table using hashes holding references to subroutines (super search for "dispatch table" should give interesting results).This is true of Switch.pm but untrue of Perl 6 switches, which are expected to pay sufficient attention to the syntax that if you use constants it will recognize that it can optimize it to a jump table. In general, the compiler is perfectly free to jump over any cases that it knows will never be true. Even when the values aren't constants, you can tell the compiler to treat them a particular way by using an appropriate context operator such as + or ~.
Update: I forgot to mention that Perl 4 already does this optimization. It never got into Perl 5 due to a lack of tuits, however.
|
|---|