Re: Nested Switch case
by roboticus (Chancellor) on Nov 29, 2010 at 14:48 UTC
|
teg:
I'd advise you to avoid the Switch module. If you want something similar and you're on perl 5.10 or later, you're in luck:
use strict;
use warnings;
use fature ':5.10';
my $foo = 7;
given ($foo) {
when (1) { say "foo is one!" }
when ([2 .. 5]) { say "foo is one of 2, 3, 4 or 5" }
default { say "Eh ... wrong foo!" }
}
...roboticus
| [reply] [Watch: Dir/Any] [d/l] |
|
Unfortunately I am having perl version 5.8.6..
:(
| [reply] [Watch: Dir/Any] |
|
tej:
Then I'd stay away from Switch and just use if/elsif/else. First of all, the Switch module is just rewriting your code to use if/elsif/else anyway, so there wouldn't be any speed boosts.
The problem with Switch is that it mostly works as you want it. But since it's a source filter, it's basically changing your code for you. Generally, Switch will work, and it may work well for you. But if you get a program to the point where it confuses Switch, it can be difficult to determine the problem and fix it. Hence, most people will just skip that module.
...roboticus
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
Why do you suggest keeping away from Switch? Just curious because I use it in a couple scripts...
| [reply] [Watch: Dir/Any] |
|
Switch uses source filters, which are generally considered a poor and buggy solution. To quote from the perl 5.10.1 perldelta:
Switch is buggy and should be avoided. From perl 5.11.0 onwards, it is intended that any use of the core version of this module will emit a warning, and that the module will eventually be removed from the core (probably in perl 5.14.0). See Switch statements in the perlsyn manpage for its replacement.
| [reply] [Watch: Dir/Any] |
|
CheeseConQueso:
Primarily because it's a source filter: In other words, it rewrites your code before the parser can parse it. So it'll probably work perfectly nearly all of the time. But if it gets confused, the parser is going to report problems on the rewritten code, rather than the code you're working on. So the line numbers and/or descriptions of the errors may have little bearing on the source code you're working on. Additionally, if the module gets confused, it may alter code not related to any switches. This can be confusing, since you probably won't suspect Switch to be the cause of a problem where there are no switch statements near the reported problems.
...roboticus
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
I used the Switch module around 2006 in a program of a few thousands of lines that was supposed to be used in a production environment. It caused some surprising bugs. I don't even remember what they were exactly, but it really wasn't something that i would expect to come from using such a module. It didn't save me any typing and didn't add any elegance to the code, and i wasted some time (an hour maybe) trying to understand what causes the bugs until i just replaced it with if/else and didn't look back.
given/when is great if you can use it, though.
| [reply] [Watch: Dir/Any] |
Re: Nested Switch case
by sundialsvc4 (Abbot) on Nov 29, 2010 at 15:28 UTC
|
Best rule-of-thumb is... never worry about what is “faster.” Make it, instead, “abundantly clear and easily maintainable.” In these days of blisteringly-fast CPUs, nothing else really matters.
Switch seems to be regarded as badness because it does its work by playing games under-the-hood with the source code. When it breaks it is hard to figure out, and it really doesn’t bring anything new to the table.
| [reply] [Watch: Dir/Any] |
Re: Nested Switch case
by choroba (Cardinal) on Nov 29, 2010 at 14:22 UTC
|
Why don't you try yourself? Write a short script with nested switch. If it works, use Benchmark to compare its speed with the if-construct. | [reply] [Watch: Dir/Any] |
Re: Nested Switch case
by TomDLux (Vicar) on Nov 29, 2010 at 19:42 UTC
|
Choosing the appropriate case will take a microsecond or a fraction thereof, in either case. The code you run once you find the right branch will take far longer. So it doesn't matter.
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
| [reply] [Watch: Dir/Any] |