RE: Is it me or is mod_perl extremely dangerous?

"Desilets, Alain" <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
Hum... I'm puzzled.

According to this page:

http://docstore.mik.ua/orelly/weblinux2/apache/ch17_05.htm

One of the ways to avoid these kinds of problems is to:

"Put your code into modules as subroutines and call it from the main script - for some reason global variables in the module will be initialized"

But this is not what I am experiencing. It seems to me that module variables are NOT being reinitialized.

Towards the end of this email is a script (try_mode_perl.cgi) and a module (TryModPerl.pm) that I wrote to test how different types of variables get reinitialized or not.

When I run it say, 6 times, I get the output which is at the end of this email.

The section of the output that says:

---
$TryModPerl::package_var_noprefix=6
$TryModPerl::package_var_my=5
$TryModPerl::package_var_local=5
$TryModPerl::package_var_our=5
---

Seems to indicate that the package level variables are not getting reinitialized, whether they are declared in any of the following forms.

----
package TryModPerl;

$package_var_noprefix = 1;
my $package_var_my = 1;
local $package_var_local = 1;
our $package_var_local = 1;
----

Is this normal behavior? Note that I am running on Windows 7. 

Thx

Alain

=== try_mod_perl.cgi:
#!perl -w

use CGI;

$script_var_noprefix = 1;
my $script_var_my = 1;
local $script_var_local = 1;
our $script_var_our = 1;

sub run {

	my $cgi = CGI->new(); 
	print $cgi->header();
	print $cgi->start_html();

	print_various_vars($cgi);
	
	print $cgi->end_html();

}

sub print_various_vars {
	my ($cgi) = @_;
	my $txt =
			"With a normal CGI environment, all the variables below should always have a value of 1.";
	print $cgi->p($txt);

	$txt = "But in a mod_perl environment, some of them may keep increase in value, because they are not reinitialized from one call to the next.";
	print $cgi->p("$txt");
	
	print "\$script_var_noprefix=$script_var_noprefix<br>\n";	
	print "\$script_var_my=$script_var_my<br>\n";	
	print "\$script_var_local=$script_var_local<br>\n";	
	print "\$script_var_our=$script_var_our<br>\n";	
	print "<br>\n";

	$script_var_noprefix++;
	$script_var_my++;
	$script_var_local ++;
	$script_var_local ++;
	
	$script_sub_var_noprefix = 1;
	my $script_sub_var_my = 1;
	local $script_sub_var_local = 1;
	our $script_sub_var_our = 1;

	print "\$script_sub_var_noprefix=$script_sub_var_noprefix<br>\n";	
	print "\$script_sub_var_my=$script_sub_var_my<br>\n";	
	print "\$script_sub_var_local=$script_sub_var_local<br>\n";	
	print "\$script_sub_var_our=$script_sub_var_our<br>\n";	
	print "<br>\n";

	$script_sub_var_noprefix ++;
	$script_sub_var_my ++;
	$script_sub_var_local ++;
	$script_sub_var_our ++;

	print "\$TryModPerl::package_var_noprefix=$TryModPerl::package_var_noprefix<br>\n";	
	print "\$TryModPerl::package_var_my=$TryModPerl::package_var_my<br>\n";	
	print "\$TryModPerl::package_var_local=$TryModPerl::package_var_local<br>\n";	
	print "\$TryModPerl::package_var_our=$TryModPerl::package_var_our<br>\n";	
	print "<br>\n";

	$TryModPerl::package_var_noprefix++;	
	$TryModPerl::package_var_my++;	
	$TryModPerl::package_var_local++;	
	$TryModPerl::package_var_our++;	

	TryModPerl::print_various_vars($cgi);
}

run();

=== TryModPerl.pm:
package TryModPerl;

$package_var_noprefix = 1;
my $package_var_my = 1;
local $package_var_local = 1;
our $package_var_local = 1;

sub print_various_vars {
	my ($cgi) = @_;
	
	$package_sub_var_noprefix = 1;
	my $package_sub_var_my = 1;
	local $package_sub_var_local = 1;
	our $package_sub_var_our = 1;

	print "\$package_sub_var_noprefix=$package_sub_var_noprefix<br>\n";	
	print "\$package_sub_var_my=$package_sub_var_my<br>\n";	
	print "\$package_sub_var_local=$package_sub_var_local<br>\n";	
	print "\$package_sub_var_our=$package_sub_var_our<br>\n";	
	print "<br>\n";

	$package_sub_var_noprefix++;
	$package_sub_var_my++;
	$package_sub_var_local++;
	$package_sub_var_our++;
}

1;

=== After 6 invocations, generates output:

With a normal CGI environment, all the variables below should always have a value of 1.

But in a mod_perl environment, some of them may keep increase in value, because they are not reinitialized from one call to the next.
$script_var_noprefix=1
$script_var_my=6
$script_var_local=1
$script_var_our=1

$script_sub_var_noprefix=1
$script_sub_var_my=1
$script_sub_var_local=1
$script_sub_var_our=1

$TryModPerl::package_var_noprefix=6
$TryModPerl::package_var_my=5
$TryModPerl::package_var_local=5
$TryModPerl::package_var_our=5

$package_sub_var_noprefix=1
$package_sub_var_my=1
$package_sub_var_local=1
$package_sub_var_our=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.