You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Tomas Zeman <to...@yahoo.com> on 2000/06/29 23:50:59 UTC

global variables in mod_perl

Hi all, 

I am new to mod_perl and i have one problem<br>

I want to rewrite this simple script from fastcgi to
mdo_perl
---------
use FCGI;
$cnt = 0;
while (FCGI::Accept)
{
   print "Content-type:text/html\n\n";
   print $cnt++;
}
---------

This script writes (when i relaod it) 1,2,3,4,5,...
and $cnt is "global variable"

in mod_perl
I created My.pm package
-----------
package My;

use strict;
use Apache::Constants qw(:common);

# I try both global variable possibilites
use vars qw($cnt1);
$My::cnt2 = 1;

my $cnt1 = 1;

sub increment{
 $My::cnt2++;
 $cnt++;
 print "$cnt1<br>";
 print $My::cnt2;
}
1;
-----------

I have this module in starup.pl and start it when
apache start

I also have a script when i use My.pm module
-----------
#!/usr/bin/perl -w

use strict;
use Apache::Registry;
use My;

print "Content-type:text/html\n\n";


$rr = Myy->increment();
print "<br>$rr<br>"; 
-----------

This script writes 1,1,1,2,2,2,3,3,3,2,2,3,4,2,1 .....
randomly !!

Can anybody help me, where is the problem ?


Thanks a lot
Tomas Zeman



__________________________________________________
Do You Yahoo!?
Get Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/

Re: global variables in mod_perl

Posted by Richard Dice <rd...@pobox.com>.
> I am new to mod_perl and i have one problem<br>
.
.
.
> This script writes 1,1,1,2,2,2,3,3,3,2,2,3,4,2,1 .....
> randomly !!

Global variables are your problem.  The thing is, each global has a 
value that is persistent... IN ANY GIVEN APACHE CHILD PROCESS!

So, your increments are working, but you are randomly hitting different
child processes each time you access your web server.

The solution to this problem depends on what your goal is with this
program.

Cheers,
Richard

-- 
----------------------------------------------------------------------------
 Richard Dice * Personal 514 816 9568 * Fax 514 816 9569
 ShadNet Creator * http://shadnet.shad.ca/ * rdice@shadnet.shad.ca
 Occasional Writer, HotWired * http://www.hotwired.com/webmonkey/
     "squeeze the world 'til it's small enough to join us heel to toe"
         - jesus jones

Re: global variables in mod_perl

Posted by Casey Bristow <Ca...@xor.com>.
 seems to me, that the script is working as it should.. you are just 
 hitting several different instances of the script.. one for each 
 apache child process.. try starting apache with the -X flag.
 './httpd -X' .. this will cause apache to run in single process mode.

 hope this helps.

On Thu, 29 Jun 2000, Tomas Zeman wrote:

> Hi all, 
> 
> I am new to mod_perl and i have one problem<br>
> 
> I want to rewrite this simple script from fastcgi to
> mdo_perl
> ---------
> use FCGI;
> $cnt = 0;
> while (FCGI::Accept)
> {
>    print "Content-type:text/html\n\n";
>    print $cnt++;
> }
> ---------
> 
> This script writes (when i relaod it) 1,2,3,4,5,...
> and $cnt is "global variable"
> 
> in mod_perl
> I created My.pm package
> -----------
> package My;
> 
> use strict;
> use Apache::Constants qw(:common);
> 
> # I try both global variable possibilites
> use vars qw($cnt1);
> $My::cnt2 = 1;
> 
> my $cnt1 = 1;
> 
> sub increment{
>  $My::cnt2++;
>  $cnt++;
>  print "$cnt1<br>";
>  print $My::cnt2;
> }
> 1;
> -----------
> 
> I have this module in starup.pl and start it when
> apache start
> 
> I also have a script when i use My.pm module
> -----------
> #!/usr/bin/perl -w
> 
> use strict;
> use Apache::Registry;
> use My;
> 
> print "Content-type:text/html\n\n";
> 
> 
> $rr = Myy->increment();
> print "<br>$rr<br>"; 
> -----------
> 
> This script writes 1,1,1,2,2,2,3,3,3,2,2,3,4,2,1 .....
> randomly !!
> 
> Can anybody help me, where is the problem ?
> 
> 
> Thanks a lot
> Tomas Zeman
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Get Yahoo! Mail - Free email you can access from anywhere!
> http://mail.yahoo.com/
> 

-- 

     -Casey