Re: localized lexical varibles

[prev] [thread] [next] [Date index for 2005/03/15]

From: Tom Schindl
Subject: Re: localized lexical varibles
Date: 17:40 on 15 Mar 2005
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.
> 

localized lexical varibles
Vladimir D Belousov 17:29 on 15 Mar 2005

Re: localized lexical varibles
Michael Peters 17:30 on 15 Mar 2005

Re: localized lexical varibles
Vladimir D Belousov 19:27 on 15 Mar 2005

Re: localized lexical varibles
Perrin Harkins 23:59 on 15 Mar 2005

Re: localized lexical varibles
Tom Schindl 17:40 on 15 Mar 2005

Re: localized lexical varibles
Vladimir D Belousov 18:54 on 15 Mar 2005

Re: localized lexical varibles
Michael Peters 18:59 on 15 Mar 2005

Re: localized lexical varibles
Vladimir D Belousov 19:41 on 15 Mar 2005

Generated at 16:59 on 18 Mar 2005 by mariachi v0.52