You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Jeremy Silva <js...@mitre.org> on 2004/04/07 23:03:46 UTC

Weird CGI Caching problem

I'm having a problem with Mod_perl 2.0 that I cannot seem to find any 
help on.

I found some help on Mod Perl's caching 
(http://bulknews.net/lib/mod_perl_guide/porting.html)
and tested some of his scripts.  I was having some problems in some of 
my conversions, so I decided to modify his scripts to see if the problem 
persisted.

mycounter.pl
_________________
   use CGI;
   my $counter;
   my $cgi;
   sub run{
     print "Content-type: text/plain\r\n\r\n";
	print "HERE";
	
	$counter = 0;
	$cgi=null;
	$cgi=CGI->new;
     for (1..5) {
       increment_counter();
     }
		$cgi=null;

   }
   sub increment_counter{
     $counter++;
	my $str=$cgi->param("name");
     print "Name=$str Counter is equal to $counter !\r\n";
   }
   1;


counter.pl
_____________
   use strict;
   require "./mycounter.pl";
   run();



 From a browser, I've passed in some name and then have tried changing 
it.  It seems as though Apache2 is keeping the CGI parameters cached in 
memory for every run.  My question is, how do I make sure I get the 
latest CGI object from Apache?  As you can see, I've tried changing the 
cgi parameter to null (and everything else).


JS


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Jeremy Silva <js...@mitre.org>.
Perrin,

Did both things you mentioned.  I upgraded to the latest CGI for 
ActiveState and made the modification.

I still get the same caching problem that I was getting before (try it 
on your own system).

Is this a bug in Mod_perl?  In my parent script, why don't I have 
control over the CGI object?

Jeremy

Perrin Harkins wrote:

> On Wed, 2004-04-07 at 17:03, Jeremy Silva wrote:
> 
>>   use CGI;
>>   my $counter;
>>   my $cgi;
>>   sub run{
>>     print "Content-type: text/plain\r\n\r\n";
>>	print "HERE";
>>	
>>	$counter = 0;
>>	$cgi=null;
> 
> 
> You don't need to do this.  Just pass the CGI object to your sub:
> 
> my $cgi = CGI->new();
> 
> run($cgi);
> 
> sub run {
>     my $cgi = shift;
>     # do something with $cgi
> }
> 
> Also, be sure that you are using the latest version of CGI.pm.
> 
> - Perrin
> 



-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Perrin Harkins <pe...@elem.com>.
On Wed, 2004-04-14 at 13:38, Perrin Harkins wrote:
> # Counter.pm
> 
> sub run {
>     my $cgi = shift;
>     print "Content-type: text/plain\r\n\r\n";
>     print "HERE";
>     my $counter = 0;
>     for ( 1 .. 5 ) { increment_counter(\$counter, $cgi); }
> }
> 
> sub increment_counter {
>     my ($counter_ref, $cgi) = @_;
>     ${$counter}++;
>     my $str = $cgi->param("name");
>     print "Name=$str Counter is equal to $counter !\r\n";
> }
> 
> 1;

That should have been more like this:

package Counter;
use strict;
use warnings;

sub run {
    my $cgi = shift;
    print "Content-type: text/plain\r\n\r\n";
    print "HERE";
    my $counter = 0;
    for ( 1 .. 5 ) { increment_counter(\$counter, $cgi); }
}

sub increment_counter {
    my ($counter_ref, $cgi) = @_;
    ${$counter_ref}++;
    my $str = $cgi->param("name");
    print "Name=$str Counter is equal to $counter !\r\n";
}

1;



-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Perrin Harkins <pe...@elem.com>.
On Wed, 2004-04-14 at 13:07, Jeremy Silva wrote:
> The following code will cache the CGI 
> parameters with Mod_perl.

It will cache them without mod_perl too, although you won't see it
because your CGI will exit after each request.  Your sub
increment_counter() is a closure because it uses the $cgi (and $counter)
variable that is declared outside of it's scope.  You have to pass $cgi
to every sub that you want to use it in.

Here's a cleaned up version of your code:

# counter.pl

use strict;
use warnings;

use CGI;
use Counter;

my $cgi = CGI->new();
Counter::run($cgi);

# Counter.pm

sub run {
    my $cgi = shift;
    print "Content-type: text/plain\r\n\r\n";
    print "HERE";
    my $counter = 0;
    for ( 1 .. 5 ) { increment_counter(\$counter, $cgi); }
}

sub increment_counter {
    my ($counter_ref, $cgi) = @_;
    ${$counter}++;
    my $str = $cgi->param("name");
    print "Name=$str Counter is equal to $counter !\r\n";
}

1;

- Perrin


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Jeremy Silva <js...@mitre.org>.
Perrin,

I'd be interested to see how this would work though, given my original 
code post and your modifications.  The following code will cache the CGI 
parameters with Mod_perl.  Switching to CGI::Simple was the only 
solution that I could find.

JS



mycounter.pl
_________________
   use CGI;
   my $counter;

   sub run{
     my $cgi = shift;
     print "Content-type: text/plain\r\n\r\n";
     print "HERE";
     $counter = 0;
     for (1..5) {
       increment_counter();
     }
         $cgi=null;

   }
   sub increment_counter{
     $counter++;
     my $str=$cgi->param("name");
     print "Name=$str Counter is equal to $counter !\r\n";
   }
   1;


counter.pl
_____________
   use strict;
   require "./mycounter.pl";
   my $cgi = CGI->new();
   run($cgi);


Perrin Harkins wrote:

> On Fri, 2004-04-09 at 12:00, Jeremy Silva wrote:
> 
>>The simple answer, is to not use the default CGI.pm module, but instead 
>>use the CGI::Simple module instead.  From what I've read, it is faster 
>>than the default CGI module.
> 
> 
> Glad to hear CGI::Simple is working for you.  CGI.pm's mod_perl support
> is somewhat tricky, because it uses a lot of global variables.  However,
> it does work.  If it didn't work for you, it was probably due to a
> scoping problem somewhere in your code, like an unintentional closure. 
> If you have further problems with apparent caching, look for something
> like that.
> 
> - Perrin
> 
> 
> 


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2004-04-09 at 12:00, Jeremy Silva wrote:
> The simple answer, is to not use the default CGI.pm module, but instead 
> use the CGI::Simple module instead.  From what I've read, it is faster 
> than the default CGI module.

Glad to hear CGI::Simple is working for you.  CGI.pm's mod_perl support
is somewhat tricky, because it uses a lot of global variables.  However,
it does work.  If it didn't work for you, it was probably due to a
scoping problem somewhere in your code, like an unintentional closure. 
If you have further problems with apparent caching, look for something
like that.

- Perrin




-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Jeremy Silva <js...@mitre.org>.
All,

Sorry for the barrage, but I found a solution to my cgi caching problem, 
using Apache2 and Mod_perl (under Win/ActiveState).

The simple answer, is to not use the default CGI.pm module, but instead 
use the CGI::Simple module instead.  From what I've read, it is faster 
than the default CGI module.

As soon as I installed CGI-Simple in the PPM, and edited the Simple.pm 
module found in the  perl\site\lib\cgi\  directory according to the 
comment "# comment this and the __DATA__ token out under mod_perl", the 
cgi caching problem dissapeared under Mod_perl.


JS


js wrote:

> Perrin,
> 
> Did both things you mentioned.  I upgraded to the latest CGI for 
> ActiveState
> 
> Perrin Harkins wrote:
> 
>> On Wed, 2004-04-07 at 17:03, Jeremy Silva wrote:
>>
>>>   use CGI;
>>>   my $counter;
>>>   my $cgi;
>>>   sub run{
>>>     print "Content-type: text/plain\r\n\r\n";
>>>     print "HERE";
>>>     
>>>     $counter = 0;
>>>     $cgi=null;
>>
>>
>>
>> You don't need to do this.  Just pass the CGI object to your sub:
>>
>> my $cgi = CGI->new();
>>
>> run($cgi);
>>
>> sub run {
>>     my $cgi = shift;
>>     # do something with $cgi
>> }
>>
>> Also, be sure that you are using the latest version of CGI.pm.
>>
>> - Perrin
>>
> 


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Weird CGI Caching problem

Posted by Perrin Harkins <pe...@elem.com>.
On Wed, 2004-04-07 at 17:03, Jeremy Silva wrote:
>    use CGI;
>    my $counter;
>    my $cgi;
>    sub run{
>      print "Content-type: text/plain\r\n\r\n";
> 	print "HERE";
> 	
> 	$counter = 0;
> 	$cgi=null;

You don't need to do this.  Just pass the CGI object to your sub:

my $cgi = CGI->new();

run($cgi);

sub run {
    my $cgi = shift;
    # do something with $cgi
}

Also, be sure that you are using the latest version of CGI.pm.

- Perrin


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html