Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Nested Switch case

by tej (Scribe)
on Nov 29, 2010 at 14:17 UTC ( [id://874250]=perlquestion: print w/replies, xml ) Need Help??

tej has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Is it possible to have nested Switch case?

If yes then which is faster...If()..elsif() or nested Switch?

Thank you

Replies are listed 'Best First'.
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

      Unfortunately I am having perl version 5.8.6.. :(

        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

        That vesion is from 2004, perhaps you could install a shiney new build elsewhere on your system.

      Why do you suggest keeping away from Switch? Just curious because I use it in a couple scripts...
        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.

        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

        Not to mention the author consider(ed|s) it "experimental" and not for production use.

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

        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.

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.

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.
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://874250]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found