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

So I've written the following working example where I call my script via Javascript with a query parameter ( function= ) , the value of function is evaluated by the perl script, and then calls the appropriate sub based on the value. Then I take the output and just display it with some ajax. This all works great with straight perl, however falls apart when I use pperl. When using pperl the variables do not 're-declare' so if you send 'function=one' to the script 5 times, and then send 'function=two' 5 times, you'll still get the 'funcOne' sub to run. I think I understand why this is I just can't figure a way around. I've looked at mod_perl's, speedyCGI and other persistent environment documentation. Any help would be appreciated.
#!/usr/bin/pperl use strict; use CGI qw(:standard); use Switch; ###################################### # Setup our Variables my $myCGI = new CGI; my $parameters = $myCGI->Vars; # Holds the POST passed function name my $function = $parameters->{'function'}; ###################################### # This is the function request handler, it takes the 'function' argume +nt passed # in the POST and calls the correct function switch ( $function ) { case 'one' { print $myCGI->header(-type=>"text/html",-charset=>"UTF-8"); print $myCGI->start_html; funcOne(); } case 'two' { print $myCGI->header(-type=>"text/html",-charset=>"UTF-8"); print $myCGI->start_html; funcTwo(); } case 'three' { print $myCGI->header(-type=>"text/html",-charset=>"UTF-8"); print $myCGI->start_html; funcThree(); } else { print $myCGI->header(-type=>"text/html",-charset=>"UTF-8"); print $myCGI->start_html; print "Function not found"; } } sub funcOne(){ print "\nONE\n"; return( 1 ); } sub funcTwo(){ print "\nTWO\n"; return( 1 ); } sub funcThree(){ print "\nTHREE\n"; return( 1 ); }

Replies are listed 'Best First'.
Re: Having trouble with pperl (Persistent Perl) and CGI
by perrin (Chancellor) on Apr 24, 2008 at 21:26 UTC
    I believe the problem you're having here is that CGI.pm is a totally insane piece of code that uses global variables everywhere. To make it work in a persistent environment, you have to call CGI::initialize_globals() at the beginning of every request. It does this automatically when it detects mod_perl.

      I did not even know that. Nice. It explains, I think, the problem I've seen using CGI inline in TT2 templates from within Catalyst. [% USE CGI %] hangs the server (on submits IIRC) while [% USE CGI(foo) %], fake initializer, works fine. Now I know why. :)

        This sounds a lot like what would happen if you had read STDIN already before calling new CGI(). If you just want to create a CGI object without it trying to automatically read the script's input, you can initialize it with an empty string: new CGI("").
      that was indeed the problem . Can't believe google hadn't indexed somebody having this problem before-- it's 2008 for goodness sake. Anyway, thanks :)
      Worked perfectly ... thanks everyone for the help
Re: Having trouble with pperl (Persistent Perl) and CGI
by toolic (Bishop) on Apr 24, 2008 at 20:47 UTC
    Just a shot in the dark... but have you tried it without using Switch?
    if ($function eq 'one') { ... } elsif ($function eq 'two') { ... }