hi,

I am new to mod_perl.This is my first doubt,and I need some help.

I test this script under mod_perl:



#!/usr/bin/perl -w
 use strict;
 use warnings;
print "Content-type: text/plain\r\n\r\n"; my $counter = 0; # Explicit initialization technically redundant for (1..5) {
   increment_counter();
 }
sub increment_counter{
   $counter++;
   print "Counter is equal to $counter !\r\n";
 }


When I reload from user agent,this script can't work correctly.It seems that the script cached the result in memory.The output is:

first run:
 Counter is equal to 1 !
 Counter is equal to 2 !
 Counter is equal to 3 !
 Counter is equal to 4 !
 Counter is equal to 5 !

reload:
 Counter is equal to 6 !
 Counter is equal to 7 !
 Counter is equal to 8 !
 Counter is equal to 9 !
 Counter is equal to 10 !

Just as Stas Bekman said,the resolving way is to change the var declare from:
my $counter = 0;

to:
our $counter = 0;

I have read Stas Bekman's article,but I CAN'T understand for it.In order to avoid persistent vars between requests,we should select using 'my' to declare a var.While here we use a 'our' var to resolve the problem.Can anyone tell me clearly why this happen?Thanks a lot.

Angel Flower

_________________________________________________________________
享用世界上最大的电子邮件系统― MSN Hotmail。 http://www.hotmail.com

Reply via email to