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

Monks,
I try to run a Perl/CGI script on a server, where mod_perl is installed and running. It seems that my script is forking itself, so I experience unexpected behaviour. For example a value is remaining from the previous process, and messing up everything.

The question is how can I avoid this? I initialize every variable with my(), but it is still doing these annoying mistakes.
Is it possible that the mod_perl setup is wrong? Since I am not the sysadmin I can just suggest him that its working is unlikely.

I appreciate your help.

-- tune

Replies are listed 'Best First'.
Re: mod_perl configuration error?
by chipmunk (Parson) on Jan 26, 2001 at 23:23 UTC
    The idea behind mod_perl is that your script is run several times for each time it is loaded. While this improves efficiency, it does raise some new issues.

    The issue you've encountered is that when a script is executed the second time in a given fork, it keeps the old values from the first execution.

    Another issue is that declaring a my variable in your script outside of any subroutines and then using it inside a subroutine will cause the warning "Variable ... will not stay shared...". This is an effect of how mod_perl caches your script, by putting it inside a subroutine. See perldiag for more details.

    To solve these issues, you should:

    • Declare your variables with use vars instead of my. (Inside subroutines, variables can still be declared with my.)
    • Always explicitly initialize your variables. use vars qw/$x/; should be followed by $x = ';' (or some other initial value).
    In 5.6.0, you should be able to use our instead of use vars.
Re: mod_perl configuration error?
by Yohimbe (Pilgrim) on Jan 26, 2001 at 22:33 UTC
    We'd have to see your code to help you.
    a couple of notes to ponder:
  • does your code take into account that NOTHING is to occur outside the "sub handler {" section that affect program flow?
  • you can have potentially many separate processes running, so exclusive use of resources can be a problem?
    --
    Jay "Yohimbe" Thorne, alpha geek for UserFriendly
Re: mod_perl configuration error?
by tune (Curate) on Jan 26, 2001 at 23:05 UTC
    Look, I can't include code because it tells too much about our system config. I dont want PM to be a hacker knowledge database :-)

    I just need some guidelines, how to avoid mod_perl in my script, or accidental forking.

    -- tune

      hmm. Tall order. try this: standard form of a mod_perl routine:
      #!/usr/bin/perl -w
      use strict;
      packate Foo;
      use CGI;
      use MyLibrary;
      sub handler {
      	my $q = new CGI;
      	my $someparam=$q->param('someparam');
      	$someparam=MyLibrary::FooProcess($someparam);
      	my $output ="Hello and welcome to FooCo!"
      	if ($someparam eq 'foo') {
      		# handle foo
      		$output .= "You said Foo! heh heh!";
      	}
      	else {
      		# default
      		$output .= "Nobody gives me any input";
      	}
      	print $q->header,$output;
      	return;
      }
      ## and the mod_perl config 
      
      <Location /cgi-bin/foo.pl>
              SetHandler perl-script
              PerlHandler Foo
      </location>
      
      
      Does your code differ dramatically from this?
      --
      Jay "Yohimbe" Thorne, alpha geek for UserFriendly