You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Jonathan Vanasco <jv...@2xlp.com> on 2007/06/05 15:02:51 UTC

Re: Weird problem - local variable appears to be available in 2 instances of mod-perl

Steven-

Variables in mod_perl are persistant - you need to make use of 'my'  
to reinitialize the variable on each suroutine call

look for 'scoping' in the docs.  there are several sections.

consider the following,  which is very likely what you have

package handler;
my $world;
function hello {
	$world += $_[0];
	return "hello, $world";
}
1;

$var is initialized when the module compiles (startup), but  each  
invocation of test() just concatenates a new value to $var.
hello('world') -> "hello, world"
hello('world'); hello('universe'); -> "hello, world" , "hello,  
worlduniverse"

you want to be doing

package handler;
function hello {
	my $world;
	$world += $_[0];
	return "hello, $world";
}
1;

the problem seems intermittant, because the variable is kept  
'initialized' in the parent and then cloned to the children via copy- 
on-write.  every time a new apache process is spawned, it has a  
fresh  intialized variable (so you see no issue).  you only notice  
the issue when a processess serves its second or later request.






It is appearing/disappearing most likely because eac

On Jun 5, 2007, at 7:28 AM, Steven Heimann wrote:

> I am new to mod_perl but have been using perl over the years for  
> various
> text processing needs.
>
> I needed to extract and modify data on the fly for our web site and
> thought mod_perl would be perfect.  The Ubuntu Breezy package
> installation worked fine and mod perl seemed to work correctly.
>
> The links on the web page call a perl script to open an xml file,
> process and extract data in the file and produce HTML.  It appears to
> work correctly except that in testing on odd occasions some data  
> from a
> previous call to the perl script gets displayed.  It is not the whole
> page.  A paragraph from the previously accessed page will get  
> displayed
> along with the current page.  This paragraph of text represents the
> contents of one variable in the perl script.
>
> Continual refreshes from the browser results in apparently random
> appearance of the same incorrect data.  I am testing with direct  
> access
> to the web server (apache2) to ensure it is not caching causing the
> problem.
>
> I don't understand how intermittently a variable seems to be  
> persisting
> from one instance of the script to another, especially when during the
> course of several refreshes it will disappear and then reappear.
>
> I have been trying to find an answer for much of the day without
> success.  Any suggestions of how to track down this problem would be
> appreciated.
>
> Thank you and regards
> Steven