Re: localized lexical varibles
[prev]
[thread]
[next]
[Date index for 2005/03/15]
Vladimir D Belousov wrote:
> Hallo all!
>
> I'm new to mod_perl, and I see the strange behaviour of local variables
> in functions.
> This is my simple test:
>
> ==
> .htaccess:
>
> SetHandler perl-script
> PerlHandler My::Test;
>
> ==
> package My::Test;
> #/usr/local/apache/My/Test.pm
>
> use strict;
> use Apache::Constants ':common';
>
> sub handler {
> my $r=Apache->request;
> $r->content_type('text/html');
> $r->send_http_header;
> print "Request: ".$r->uri."<br/>";
> my $s = $r->uri;
> print "From main: $s<br/>";
> Call();
> return OK;
>
You are creating a closure here for $s here. There enough material out
there go and read about closures(See Apache::Registry).
Why do you embed a sub into a sub?
So this is not bug but a feature :-).
You code is buggy move the sub Call {...} outside the sub handler {} and
you are fine.
Tom
> sub Call {
> print "From Call: $s<br/>";
> print "But request: ".$r->uri;
> }
> }
>
> 1;
> ==
>
> First time I got correct answer:
> (request: GET /1111)
> Request: /111
> From main: /111
> From Call: /111
> But request: /111
>
> But in next times I got a old $s value in the Call() function:
>
> GET /1111/222
> Request: /111/222
> From main: /111/222
> From Call: /111
> But request: /111/222
>
> GET /1234
> Request: /1234
> From main: /1234
> From Call: /111
> But request: /1234
>
> Where I am wrong? Or where I can found corresponding documentation?
>
> I beg your pardon for my english.
>
 |
 |
Re: localized lexical varibles
Tom Schindl 17:40 on 15 Mar 2005
|