hi,
Sorry,when I read Stas Bekman's article again,I understand for that problem which happened with me.

In fact,the script which I tested under mod_perl is wrapped into another subroutine which is named as handler().The code that was actually executed:

 package Apache::ROOT::perl::conference::counter_2epl;
 use Apache qw(exit);
 sub handler {
   BEGIN {
     $^W = 1;
   };
   $^W = 1;
use strict; 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";
   }
 }

With mod_perl, each subroutine in every Apache::Registry script is nested inside the handler subroutine.So increment_counter() is a nested subroutine of handler().When I declare vars with 'my' outside of nested subroutine,the problem happened.That's the reasons.

Thanks for everyone answerer.


Angel Flower




From: "angel flower" <[EMAIL PROTECTED]>
To: modperl@perl.apache.org
Subject: a new mod_perl problem
Date: Tue, 18 Oct 2005 17:38:17 +0800

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


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

Reply via email to