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

Hi monks
I have been using the switch module in several Perl programs without any trouble...until now.
One of my programs that wasn't working properly was fixed by changing the switch to if and elesif. Since the program is very long and consists of several files that inherit from each other, this is just the switch code:
switch($proxy_type){ case 'SQUID' { $web_proxy = $proxy_address; } case 'EZPROXY' { if ($proxy_credentials){ ($user_name, $password) = split /\//, $proxy_credentials; $prefix = $proxy_address."user=$user_name&pass=$password&url=" ; } else { $prefix = $proxy_address."url="; } } }
What the code does is not very important, but the fact is that when I change the switch to "if" and comment out the "use switch" line, the program works. I have used switches in other programs, and would like to know what is the cause of this problem, and where and when it might apear again? Any ideas?
Thanks, Guy Naamati (mrguy123)


A truth that's told in bad intent beats all the lies you can invent

Replies are listed 'Best First'.
Re: problems with the "switch" module
by Fletch (Bishop) on Jul 10, 2006 at 15:22 UTC
      Thanks for the advice. I guess it would have been better if I had known about Categorized Damian Modules before I released programs with Switch to customers :( , but better late than never.
Re: problems with the "switch" module
by Zaxo (Archbishop) on Jul 10, 2006 at 17:07 UTC

    Your use for Switch.pm could be done as well with a dispatch table. That is a hash of code references, like so,

    my %dispatch = ( SQUID => sub { $web_proxy = $proxy_address; }, EZPROXY => sub { if ($proxy_credentials){ ($user_name, $password) = split /\//, $proxy_credentials; $prefix = $proxy_address."user=$user_name&pass=$password& +url="; } else { $prefix = $proxy_address."url="; } } ); $dispatch{$proxy_type}->();
    Your functions should take arguments and return values, rather than modifying global variables, but I didn't know enough about your app to change that.

    CountZero++ is right, repaired. Paste confusion.

    After Compline,
    Zaxo

      I think the $prefix = $proxy_address."url="; is part of the EZPROXY case, so it should go into the EZPROXY sub.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: problems with the "switch" module
by planetscape (Chancellor) on Jul 10, 2006 at 17:34 UTC
Re: problems with the "switch" module
by kwaping (Priest) on Jul 10, 2006 at 15:23 UTC
    I recommend doing a SuperSearch on switch.pm. There are quite a few discussions about problems with this module.

    ---
    It's all fine and dandy until someone has to look at the code.
Re: problems with the "switch" module
by pjf (Curate) on Jul 11, 2006 at 00:29 UTC

    G'day Guy,

    Switch uses source-filters in order to make your code do its job. Most of the time it gets it right, but sometimes it will twist the code that reaches the Perl in unexpected ways, and that can make debugging very challenging. If you're getting very strange errors due to using Switch, there's a good chance that the source-filter may have adjusted your code incorrectly.

    The good news is that Perl 5.10 (not yet released) has a Switch-style construct that's built into the language. It's called given, and looks very similar to its Perl 6 counterpart. You can find more information in one of the recent development deltas.