Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Global variables in mod_perl

Reply
Thread Tools

Global variables in mod_perl

 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-17-2004
If I run this script:

$count++;
print "Content-type: text/plain\n\n";
print $count;

20 times under mod_perl, it outputs:

1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4

while I would have expected it to output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

System:
Linux Fedora Core 1
Apache 2.0.47
mod_perl 1.99_12
perl v5.8.1

Anybody who has an idea of what I'm missing?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      05-17-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
> If I run this script:

[...]
> 20 times under mod_perl, it outputs:
> 1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
>
> while I would have expected it to output:
> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

[...]
> Anybody who has an idea of what I'm missing?


http://perl.apache.org/docs/2.0/user...ared_Variables

--
Glenn Jackman
NCF Sysadmin
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      05-17-2004
Gunnar Hjalmarsson wrote:
> If I run this script:
>
> $count++;
> print "Content-type: text/plain\n\n";
> print $count;
>
> 20 times under mod_perl, it outputs:
>
> 1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
>
> while I would have expected it to output:
>
> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

apache is traditionally multiprocessed - there is no guarantee that a
second or subsequent request will be served by the same child. Try

print "$$ $count\n";

Mark
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-17-2004
Mark Clements wrote:
> Gunnar Hjalmarsson wrote:
>> If I run this script:
>>
>> $count++;
>> print "Content-type: text/plain\n\n";
>> print $count;
>>
>> 20 times under mod_perl, it outputs:
>>
>> 1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
>>
>> while I would have expected it to output:
>>
>> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

>
> apache is traditionally multiprocessed - there is no guarantee that
> a second or subsequent request will be served by the same child.
> Try
>
> print "$$ $count\n";


Aha, thanks!

As an exercise, just to convince myself, I put together the following
code:

use SDBM_File;
use Fcntl;
print "Content-type: text/plain\n\n";
my $file = '/path/to/file';
my $total;

$count ++;

tie my %counts, 'SDBM_File', $file, O_RDWR|O_CREAT, 0644;
$counts{$$} = $count;
for (sort keys %counts) {
print "$_: $counts{$_}\n";
$total += $counts{$_};
}
untie %counts;

print "\nTotal: $total\n";

Now the result is as expected:

10569: 2
10570: 3
10571: 2
10572: 2
10573: 3
10574: 2
10575: 4
10576: 2

Total: 20

Previously I have only played with mod_perl using my IndigoPerl
installation on Windows 98, which (I suppose) runs as one single process.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-17-2004
Glenn Jackman wrote:
> http://perl.apache.org/docs/2.0/user...ared_Variables


Hmm.. Mark convinced me that it was caused by multiple processes
rather than threads. Can you please explain?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      05-18-2004
Gunnar Hjalmarsson wrote:

> Glenn Jackman wrote:
>
>> http://perl.apache.org/docs/2.0/user...ared_Variables

>
> Hmm.. Mark convinced me that it was caused by multiple processes rather
> than threads. Can you please explain?


Some operating systems implement threads in a way that they look like
processes. The value $$ is different in each thread.
-Joe
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-18-2004
Joe Smith wrote:
> Gunnar Hjalmarsson wrote:
>> Glenn Jackman wrote:
>>> http://perl.apache.org/docs/2.0/user...ared_Variables

>>
>> Hmm.. Mark convinced me that it was caused by multiple processes
>> rather than threads. Can you please explain?

>
> Some operating systems implement threads in a way that they look
> like processes. The value $$ is different in each thread.


Okay, thanks. Guess I need to read up on both processes and threads...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Preventing changes to a module's variables in mod_perl max@maxgraphic.com Perl Misc 9 09-19-2006 01:40 PM
mod_perl installed, but mod_perl.so file not created bennett@peacefire.org Perl Misc 2 01-25-2006 10:53 PM
mod_perl and global variables during startup nicc777 Perl Misc 2 07-13-2005 02:18 PM
mod_perl errors: prototype mismatch ... during global destruction ian douglas Perl 0 08-18-2003 11:17 PM



Advertisments