http://www.perlmonks.org?node_id=436877


in reply to Re^2: How to use select
in thread How to use select

Ah, then what you want is a switch like statement .
There's a module : switch .

Or you can do it in a great number of various ways, the most common one is using a named block with last:

SWITCH: { if (expression) { statement(s); last SWITCH; } if (expression) { statement(s); last SWITCH; } default statement(s); }
update : don't try to be funky by using that switch module :) just use the if / last style ! See the answers by corion , holli and virtualsue !!

Replies are listed 'Best First'.
Re^4: How to use select (Switch.pm considered harmfull)
by Corion (Patriarch) on Mar 05, 2005 at 13:12 UTC

    I cannot recommend the use of Switch.pm to anybody. It is a fragile, unmaintained source filter that introduces bugs into your code that are hard to track down unless you already know what to look for. Hand-coding the switch-statement is the only choice that is hassle-free and doesn't introduce bugs in unrelated pieces of code.

Re^4: How to use select
by holli (Abbot) on Mar 05, 2005 at 13:21 UTC
    Note that Switch.pm is a source filter an as such notorious unreliable. I remember a discussion in the CB a while ago where quantum had some code on his scratchpad that had a mysterios error. The script included a subroutine, that wasn't called. The strange thing: When you commented it out the error went away.

    Three or four monks (including me) who worked on the code could only scratch their heads. Then virtualsue pointed out the script used Switch.pm. Removing the use Switch; statement fixed the problem.

    From the docs of Switch:
    There are undoubtedly serious bugs lurking somewhere in code this funky :-)


    Update: Corion told me quantum's name. I had forgotten it.


    holli, /regexed monk/
Re^4: How to use select
by virtualsue (Vicar) on Mar 05, 2005 at 13:35 UTC
    Recently somebody was asking for help in the chatterbox, tearing his hair out over some strange bug. It turned out that the sole cause of the problem was the fact that he had a "use Switch;" statement in his code. He wasn't actually using anything from Switch, he had just neglected to remove the use statement after a failed attempt to get it to DWHW.

    I wholeheartedly second your other suggestion for implementing a C-style switch. :-)

    FWIW I don't understand why Switch.pm is included in core. It's such an attractive nuisance for those who come from a C/C++ background and miss having a switch construct. The little disclaimer in the "Bugs" section of Switch's pod isn't really enough of a warning.