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;
>
> 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
 |
 |
Re: localized lexical varibles
Michael Peters 17:30 on 15 Mar 2005
|