All:

*** BE CAREFUL ***

Examine the following code from a single ASP script:

<%
my $mainvariable = $Session->{mysessionvar};  #as an example.

$Response->write( Mylocalsub() );

sub Myloccalsub{
return $mainvariable;
}

%>

The subroutine "Mylocalsub" simply returns a variable originally set in the
main script.  One (at least naive me!) naturally assumed that Mylocalsub
will ALWAYS use the value set at the top of the script.  Heck - the variable
and the subroutine are in the same file!

NOT!  When multiple sessions are working the same $Application across
several processes, I have found that "Mylocalsub" often uses "$mainvariable"
from a different session!  I believe that because of how "Mylocalsub" gets
compiled into the application namespace, $mainvariable in "Mylocalsub"
points to the "$mainvariable" of the first-to-be-started session.
Subsequent sessions will display the session data of the first person to get
in!

Because of how the scripts gets compiled (and how DynamicIncludes is set),
$Response->Include behaves the same as above.  I do not think that it is
wise to write code dependent on the setting of DynamicIncludes.

I believe that this is one of several "mod_perl things" and that most
problems are caught with "use strict;"  However, the example above is not,
and it is a tricky bug to track down.

One possible fix:
<%
$Response->write( &Mylocalsub($mainvariable) );
#with appropriate fixes to Mylocalsub still required.

or

$Response->Include( "mylocalsub.inc", $mainvariable );  #if using Includes!

%>

The lesson:

DO NOT DEPEND ON GLOBAL VARIABLES.  ALWAYS PASS DATA TO SUBROUTINES VIA AN
ARGUMENT LIST.  (Or set appropriate variables in the "Script_OnStart" or
store important stuff in the $Session.)

JL




> -----Original Message-----
> From: Philip Mak [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, September 12, 2001 4:46 PM
> To: Tim Pushor
> Cc: [EMAIL PROTECTED]
> Subject: Re: Includes revisited
>
>
> On Wed, 12 Sep 2001, Tim Pushor wrote:
>
> > When I use $Response->Include('filename'), does this script execute as
> > a perl block or something? I notice that local variables are not
> > visible inside the included file:
>
> If you want Apache::ASP to be more liberal about Globals, you can do
> "PerlSetVar DynamicIncludes 0" in your httpd.conf, and always use the
> <!--#include file="..."--> syntax instead of $Response->Include. This is
> not very efficient if the included files are used in many different
> scripts, because then it must be compiled separately each time. But it
> will put the include files inside the same namespace, allowing you to use
> global variables freely.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to