Re: localized lexical varibles

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

From: Michael Peters
Subject: Re: localized lexical varibles
Date: 17:30 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;
> 
>    sub Call {
>        print "From Call: $s<br/>";
>        print "But request: ".$r->uri;
>    }
>   }

You're creating a closure with $s and $r. Check out 
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#Understanding_Closures____the_Easy_Way
to understand why this causes strange behavior.

You wouldn't normally notice this under normal perl since the 
interpreter gets reloaded and your scripts get re-compiled on each run. 
mod_perl doesn't recompile your scripts for each run thus letting you 
see this behaviour that actually exists in perl itself.

The easiest/cleanest workaround is to always pass those kind of shared 
resources to your subroutines as arguments.

        -- 
        Michael Peters
Developer
Plus Three, LP

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