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

I'm considering introducing the use of Switch.pm into a large existing body of code, but because of that "There are undoubtedly serious bugs lurking somewhere in code this funky" source filter warning, I'm considering it with a certain amount of trepidation. :-)

I know it doesn't work in eval'd strings. I know about the m?...? issue mentioned in the LIMITATION section. Have you run into any other issues, or surprises, while using recent versions of Switch.pm? Is the risk of introducing subtle breakage by putting "use Switch" at the top of a library file something that should worry me? I'd like to hear some real-life experiences.

Thanks.

Replies are listed 'Best First'.
Re: Switch.pm gotchas?
by ignatz (Vicar) on Sep 24, 2002 at 03:47 UTC
    I love the idea switch. However, the two times I've used it my code started creating errors that I could not explain. At the time I was in a rush, so I just gave up and moved on figuring that it was the sort of Black Magics beyond my comprehension. I wish that I could give more information or an example. All I can say is that I'll wait until Perl 6 to use it in production. I do remember that the last time had something to do with using it with Regex::Common and how they didn't seem to like each other. I'll try to hack up an example tomorrow morning.

    /me-- for not being a better Perl citizen and saving the code in question or pointing it out to anybody.

    ()-()
     \"/
      `                                                     
    
Re: Switch.pm gotchas?
by grinder (Bishop) on Sep 24, 2002 at 17:02 UTC
    You want real life experience?

    Don't

    Disclaimer: my use of Switch is about 18 months old. I'm not sure how actively it has been developed since then. My impression was that it works as advertised for little hello world scripts, but in bigger pieces of work, it seems to introduce garbage behind the scenes (due to source filtering), and the parser barfs on perfectly valid code.

    Symptoms usually involved the fact that the parser thought it was coming out a string (single or double quoted) when in fact it was going into one, or vice versa. (update: reading broquaint's reponse... "Yes, exactly!" The syntax errors in question are miles away from the switch statement).

    Change a line around, and an error crops up somewhere else. It was most infuriating, and eventually any attempt to use it in a non-trivial script resulted in failure.

    Some time after that, Petruchio posted Help Debug Switch.pm! but I must admit that I had neither the time nor inclination to go back and try again. Maybe it works better these days, but the alternate P5 ways of implementing a switch are ok by me. I'll wait for P6 for the real thing.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Switch.pm gotchas?
by jordanh (Chaplain) on Sep 24, 2002 at 14:09 UTC
    Has there been any discussion of providing support for syntax extension in Perl 6? I know some things are possible in Perl 5, like Switch, but it seems hard to implement and dangerous.

    I know Perl 6 has a Switch statement, but it'd be cool if there was support for a powerful integrated macro processor that would allow things like Switch to be implemented conveniently.

    I know, I know, feature creep, untried, Second System effect, hard to integrate macro expansion with debuggers in a way that's intuitive to use, focus on getting something out the door... I'm just thinking out loud.

      The reason source filters are problematic in Perl5 is that "only perl can parse Perl" (ie only the perl binary knows exactly how to parse source written in the language Perl). Perl6 however will come with its own grammar built into itself for use by a script; I'd imagine it'll pretty simple to write robust source filters.

      Makeshifts last the longest.

Re: Switch.pm gotchas?
by BrowserUk (Patriarch) on Sep 25, 2002 at 01:00 UTC

    Not sure if this is worthy of production code use yet as I'm still playing with it. It might never be. I offer it for consideration anyway.

    package myswitch; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(switch); use carp; sub switch ($%) { my ( $expr, $cases ) = @_; my $default = $cases->{default} and delete $cases->{default} if exists $cases->{default}; my $selector = eval $expr; carp $@ if $@; for (keys %{$cases} ) { $cases->{$_}() and return if eval $_ eq $selector; } $default->(); } 1; =pod Name: (Currently) MySwitch.pm Purpose: To provide a simulated switch statement for Perl. Features: Arbitrary selector and case expression. It uses nothing but standard Perl so conflicts with other modules +should be minimal. Caveats: Testing so far is minimal. It captures the symantics of the switch statement, but the syntax +is slightly odd --but maybe as its Perl that doesn't matter too much. If only prototypes were more consistant, specifically if (&) did t +he same thing when it was the second element of the prototype as it does when the fir +st a much nicer syntax could be achieved. There is no fall-thru of cases as with the C equivalent (not a bad + thing in my book!) I make no claims for efficiency as the cases are evaluated in hash + order. If multiple case expressions evaluate to the same value and match +the selector expression the case executed is the first discovered in hash key order (Ie. b +asically random) but it should be consistant. Author: BrowserUk c/o PerlMonks.com Copyright 2002, BrowserUk at PerlMonks.com Its free and without warrenty of any kind. Use it as you will at your own risk. =cut

    Short, far-from-comprehensive test program.

    #! perl -sw use strict; use MySwitch; switch 4/2 => { # Abitrary expression for s +elector. 1 => sub { print 'Expression equals 1'.$/; }, # NOTE: comma not semicolon +. length 'xx' => sub { # Arbitrary expression for eac +h case. print 'Expression equals 2'.$/; }, 3 => sub { print 'Expression equals 3'.$/; }, default => sub { # default case (if supplied) u +sed if no match. print 'Expression failed to match any given case'.$/; }, }; __END__ # Output C:\test>switchtest Expression equals 2 C:\test>

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Switch.pm gotchas?
by broquaint (Abbot) on Sep 24, 2002 at 20:36 UTC
    Having just recently (i.e today) tried to replace a bunch of simple if statements with a switch and failing miserably I wouldn't recommend it's use in production code. The code was straightforward and the module relatively innocent but the resulting filtered code produced parse errors in code that wasn't even near the switch statement :-/
    HTH

    _________
    broquaint