Re: Re^6: No braces
by TimToady (Parson) on Feb 12, 2004 at 17:12 UTC
|
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.
| [reply] [d/l] [select] |
Re: No braces
by Abigail-II (Bishop) on Feb 11, 2004 at 15:57 UTC
|
Damian's Switch.pm relies on source filtering, which means that unless it can parse Perl perfectly (ha ha!), you'll get bugs in portions of code that having nothing to do with switches just because you included use Switch; in your code. So it shouldn't be used in any practical code.
I don't agree with that. It's an argument against editor
filters for colouring text - they either have to be perfect
for any text thrown at them, or you have to suffer the
consequences. A source filter is fundamentally different.
There you apply a filter to a program. And then you run your
tests. If the filter works, you're done. Otherwise, you tune
your filter and/or your program until it works. (But usually, the filter will work the first time).
Abigail
| [reply] |
|
|
Editors don't have to be perfect at coloring text, because the worst that can happen is that the colors are wrong when you view the source--it's annoying, but I can live with it. In the case of Switch.pm, anything other than parsing code exactly the way perl does is unaccaptable, because anything less will cause bugs, often in areas which are unrelated to the use of a switch. There are a few source filters which don't have to be perfect because they're doing very simple things (Acme::Bleach comes to mind), but Switch.pm isn't one of them.
----
: () { :|:& };:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] |
|
|
In the case of Switch.pm, anything other than parsing code exactly the way perl does is unaccaptable, because anything less will cause bugs, often in areas which are unrelated to the use of a switch.
That's like saying you can't make typos when editing, because
they cause bugs. And you are implying those bugs can't be found before they product is released. If Switch.pm parses
something incorrectly, it's very likely to produce uncompilable code. Perhaps you deliver code without even attempting to compile, I sure don't. In the few cases it
doesn't produce uncompilable code, it'll produce code that
is wrong - and your test suite is going to catch it. (If it
doesn't produce code that's wrong, the code is correct,
so it's not a problem). If you don't want to trust your test
suite, you can always visually inspect the results of the
source filter, to check whether it substituted only the
code you wanted to be substituted, and all of it.
The essence is that you write a particular program, and apply a filter to it. And that program isn't going to
change from run to run. It's fixed. It's not input.
Switch.pm might not parse Perl correctly, but that's ok
because you can catch the abnormalities in time.
Abigail
| [reply] |
|
|
|
|
|
Not Use Switch - Re: Re^6: No braces
by metadoktor (Hermit) on Feb 11, 2004 at 18:58 UTC
|
"Damian's Switch.pm relies on source filtering, which means that unless it can parse Perl perfectly (ha ha!), you'll get bugs in portions of code that having nothing to do with switches just because you included use Switch; in your code. So it shouldn't be used in any practical code."
This is true. I naively tried to use Switch once upon a time, because I like that construct, in some software that I was writing and it seriously broke my code. I had obscure error messages that were nearly impossible to debug until I realized that Switch was the culprit.
I agree. Don't use Switch unless you want to court devious Perl bugs.
metadoktor
"The doktor is in."
| [reply] |
Re: Re^6: No braces
by flyingmoose (Priest) on Feb 11, 2004 at 19:20 UTC
|
The requirements for the module did seem to imply a potential performance hit, true.
Source filtering is not the way I'd like to see a language extended.
Your jump-table description does make sense to me. This is why switch statements only accept constants ... they are not evaluating if-tests. That makes it very clear, appreciate the explanation, sort of a "a ha! that explains it!" kind of feeling after using switches for so many years.
| [reply] |