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

Esteemed monks,

In my never ending search for more elegant looking and self-documenting code I decided to try out the Switch module. In the following fragment of code:

#!/usr/local/bin/perl # AppSys: Manage Profile use strict; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use Switch; my $cgi = CGI->new; my $control = 'Search'; switch ( $control ) { case ("Search") { print $cgi->header; print $cgi->start_html; print $cgi->p("Search"); print $cgi->end_html; } }
produces the following in the Apache error log:
[error] syntax error at /home/appsys/www/modperl/ap03.pl line 12, near + ") {" syntax error at /home/appsys/www/modperl/ap03.pl line 14, near "} }"
When run as an Apache::Registry script.

When run under mod_cgi (i.e. run from the cgi-bin directory on the same server, it works perfectly and prints 'Search' on the browser.

I will ask this on the mod_perl list also, but I have had some good assistance here for mod_perl type questions, so maybe someone can help.

jdtoronto

Replies are listed 'Best First'.
Re: Switch (module) and Apache::Registry problem.
by diotalevi (Canon) on Oct 30, 2003 at 16:27 UTC

    While I don't know the answer I suspect it is because Switch is actually a source filter and it probably isn't generated correct perl for the mod_perl environment. In fact, I quote from the module's documentation.

    BUGSThere are undoubtedly serious bugs lurking somewhere in code this funky :-) Bug reports and other feedback are most welcome.
    I'd almost rather just point you towards something else besides Switch if only because you're introducing such oddball code that I couldn't help but laugh when I read your introductory sentence.

Re: Switch (module) and Apache::Registry problem.
by nevyn (Monk) on Oct 30, 2003 at 17:36 UTC

    Well if you really like the look of the switch type construct, TIMTOWTDI...

    SubSwitch.pm

    package SubSwitch; use strict; use base 'Exporter'; our @EXPORT_OK = qw( switch case ); our $SubSwitch = undef; sub switch { my ($val, $func) = @_; local $SubSwitch = $val; $func->(); } sub case { my ($val, $func) = @_; if ($val ne $SubSwitch) { return; } $func->(); } 1;

    test.pl

    #! /usr/bin/perl -w use strict; use SubSwitch qw(switch case); switch ("foo", sub { case ("bar", sub { print "not here\n"; }); case ("foo", sub { print "here\n"; switch ("bar", sub { case ("bar", sub { print "there\n"; }); }); }); case ("bar", sub { print "still not here\n"; }); })
    --
    James Antill
Re: Switch (module) and Apache::Registry problem.
by hardburn (Abbot) on Oct 30, 2003 at 16:30 UTC

    Yeah, Switch is really cool. Don't use it.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      Okay, so switch is too cool for its own good eh :)

      Thanks guys, now I can go back to my ugly old nested if's and not feel guilty! I always liked the switch/case construct in EBasic back in the CP/M - MP/M days, and then in C of course, always wanted one in Perl - I suppose I have to wait for Perl 6 for given/when.

      jdtoronto

        Not all is lost for Perl5. A hash containing subroutine refs can be equally readable, though fall-through can be difficult to achieve. It also has the advantage of having O(1) efficiency, just like switches in C (an interesting discussion of which is here).

        In C switches, you trade off some flexibility for efficiency. Perl6 switches will allow any expression to be used as a case, so they're basically as efficient as a bunch of if/elsif statements (though I'm hoping simple cases will be optimized into something like a C switch).

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated