You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Christopher H. Laco" <cl...@chrislaco.com> on 2005/08/20 02:24:15 UTC

Scope Question

Sometimes I think too hard about this I swear. :-)

Just a mini backgrounder...I have two AxKit Taglibs that run under 
AxKit/mod_perl. Up till now, they've been seperate in terms of usage. I 
now need to have one modify a value of the other. This has brought me to 
wonder if what I've done is going to cause me grief.

Here's one of the modules:
http://search.cpan.org/src/CLACO/Handel-0.17_08/lib/AxKit/XSP/Handel/Cart.pm

In a nutshell, it's basically:

package MyMod;
{
   my @context;
   sub dosomething {
     push @context;
     #...do other stuff..
   };
};
1;


I'm assuming that each tome dosomething() changes @contect, it's only 
changing @contect for that MP child process. SInce I don't do a lot of 
MP development, I haven't live through all of the traps involved yet. :-)


Now, I need to have another module used in that same request, modify the 
context of the first...

package MyMod;
{
   my @context;
   sub dosomething {
     push @context;
     #...do other stuff..
   };
   sub pushcontext {
     push @context, shift;
   };
};
1;

package MyMod2;
{
   my @context;
   sub dosomething {
     push @context;
     #...do other stuff..
     MyMod::poshcontext('foo');
   };
};
1;

Is that safe across requests? I'm assuming that MyMod is change MyMod2 
in the same request/child process? FOr that matter, what is the lifetime 
of my @context in that situation?

Thanks for suffering though this...
-=Chris

Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2005-08-19 at 21:23 -0400, Christopher H. Laco wrote:
> Ah.... does that closure become a global?
> 
> package MyApp;
> {
> 
> };
> 
> 
> &MyApp::__ANON__ (ish)?

Not exactly, but it does persist the value of @context.  That's what
closures do.

> Makes sense now that I think about it.
> So, in the non MP world, that helps privitize lexicals to certain subs, 
> but in MP it causes a scoping issue correct?

It behaves exactly the same in both cases.  In a CGI script you will
exit and kill the interpreter after every request, so you'll usually not
notice that the value is the same if you call it again.

- Perrin


Re: Scope Question

Posted by "Christopher H. Laco" <cl...@chrislaco.com>.
Perrin Harkins wrote:
> On Fri, 2005-08-19 at 20:24 -0400, Christopher H. Laco wrote:
> 
>>I'm assuming that each tome dosomething() changes @contect, it's only 
>>changing @contect for that MP child process.
> 
> 
> Correct.  However, dosomething() is a closure in your code, so @context
> will persist.

Ah.... does that closure become a global?

package MyApp;
{

};


&MyApp::__ANON__ (ish)?


Makes sense now that I think about it.
So, in the non MP world, that helps privitize lexicals to certain subs, 
but in MP it causes a scoping issue correct?

Thanks,
-=Chris

Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2005-08-20 at 12:55 -0400, Todd Finney wrote:
> This is probably just an issue of style, but avoiding stuff like this...
> 
> our $x=10;
> 
> ....600 lines of code...
> 
> sub foo {
>          return $x++;
> }
> &foo();
> 
> ...strikes me as common sense.

Sometimes you actually need a persistent variable though, and "our" is
the preferred way to do it over the older "use vars".  I usually
capitalize globals to indicate they have a larger scope.

- Perrin


Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2005-08-20 at 15:09 -0400, Christopher H. Laco wrote:
> I want all subs in the module to share the stagte of one variable...in 
> my case @context.
> 
> I also want any changes to @context to only effect the current 
> user/request under mod_perl.
> 
> What's the best approach?

Either pass the data around (preferably in an object of some kind) and
make sure to clean up the object at the end of the request, or keep the
state in $r->pnotes() where it will be cleared at the end of the
request.

- Perrin


Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2005-08-20 at 15:17 -0400, Christopher H. Laco wrote:
> I do have a start_document sub, so if I always reset my @context = () in 
> that, I should be fine.

This will only work if you do something like this:

my @context;

sub do_something {
    push @context....
}

sub reset_context {
    @context = ();
}

You need to keep a reference to the @context used by do_something() in
order to reset it, and the reset_context() sub does that.

- Perrin


Re: Scope Question

Posted by "Christopher H. Laco" <cl...@chrislaco.com>.
Christopher H. Laco wrote:
> So, let's reverse this.
> 
> I have a module that will be used in perl.
> 
> I want all subs in the module to share the stagte of one variable...in 
> my case @context.
> 
> I also want any changes to @context to only effect the current 
> user/request under mod_perl.
> 
> What's the best approach?
> 
> -=Chris

99% of the time...I'd put context in $self->{context} of a bless hash 
instance. But in my case, I'm dealing with a taglib that has stateless 
calls to parse_start/parse_end and I need to maintain state between 
those calls on each page request.

I do have a start_document sub, so if I always reset my @context = () in 
that, I should be fine. It's not like my @context is share umongst all 
apache child processes.

Re: Scope Question

Posted by Badai Aqrandista <ba...@hotmail.com>.

>So, let's reverse this.
>
>I have a module that will be used in perl.
>
>I want all subs in the module to share the stagte of one variable...in my 
>case @context.
>
>I also want any changes to @context to only effect the current user/request 
>under mod_perl.
>
>What's the best approach?
>

1. declare @context with 'my' in the package

2. add this to your module:

sub clean_context { @context = () }

3. add this to your config file:

PerlCleanupHandler MyApp::clean_context

I think that'll do the trick...

---
Badai Aqrandista
Cheepy (?)

_________________________________________________________________
REALESTATE: biggest buy/rent/share listings   
http://ninemsn.realestate.com.au


Re: Scope Question

Posted by "Christopher H. Laco" <cl...@chrislaco.com>.
Perrin Harkins wrote:
> On Sat, 2005-08-20 at 19:44 +0200, Frank Maas wrote:
> 
>>package A::B::C;
>>my %CFG = ( some_tag => some_config, ... }
>>...
>>sub do_something {
>>  ...
>>  do_something_else( $CFG{some_tag} );
>>  ...
>>}
>>...
>>1;
>>
>>After reading much of this discussion I am beginning to wonder if I am 
>>creating closures?
> 
> 
> Your do_something() sub will keep a persistent value for %CFG here.  If
> this is intended to be static data, that's not a problem.  I would use
> an "our" here instead though for static data.
> 
> 
>>(I must admit that the concept of closures is -for
>>some reason- very hard for me to grasp)
> 
> 
> You're not the only one.  Closures are my least favorite Perl feature
> because they're so easy to use by accident and so hard to fully
> understand.
> 
> - Perrin
> 
> 
So, let's reverse this.

I have a module that will be used in perl.

I want all subs in the module to share the stagte of one variable...in 
my case @context.

I also want any changes to @context to only effect the current 
user/request under mod_perl.

What's the best approach?

-=Chris

RE: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2005-08-20 at 19:44 +0200, Frank Maas wrote:
> package A::B::C;
> my %CFG = ( some_tag => some_config, ... }
> ...
> sub do_something {
>   ...
>   do_something_else( $CFG{some_tag} );
>   ...
> }
> ...
> 1;
> 
> After reading much of this discussion I am beginning to wonder if I am 
> creating closures?

Your do_something() sub will keep a persistent value for %CFG here.  If
this is intended to be static data, that's not a problem.  I would use
an "our" here instead though for static data.

> (I must admit that the concept of closures is -for
> some reason- very hard for me to grasp)

You're not the only one.  Closures are my least favorite Perl feature
because they're so easy to use by accident and so hard to fully
understand.

- Perrin


Re: Scope Question

Posted by Boysenberry Payne <bo...@humaniteque.com>.
Generally I make it a habit to pass the data explicitly:

> package A::B::C;
> my %CFG = ( some_tag => some_config, ... }
> ...

do_something( \%CFG );

> sub do_something {
>   ...
	my $CFG = shift;
	do_something_else( $CFG->{some_tag} );
>   ...
> }
> ...
> 1;


Boysenberry

boysenberrys.com | habitatlife.com | selfgnosis.com

On Aug 20, 2005, at 12:44 PM, Frank Maas wrote:

> Guys,
>
> I am getting a bit nervous and since it is Saturday and I am -1 day 
> before
> a deadline, this is not good. So, although this thread (and my 
> question) is
> drifting towards basic Perl, I try my luck.
>
> I make use of the following construction
>
> package A::B::C;
> my %CFG = ( some_tag => some_config, ... }
> ...
> sub do_something {
>   ...
>   do_something_else( $CFG{some_tag} );
>   ...
> }
> ...
> 1;
>
> After reading much of this discussion I am beginning to wonder if I am
> creating closures? (I must admit that the concept of closures is -for
> some reason- very hard for me to grasp). If this is creating closures,
> then what would be a better approach?
>
> Regards,
>
> Frank
>
>


RE: Scope Question

Posted by Frank Maas <fr...@cheiron-it.nl>.
Guys,

I am getting a bit nervous and since it is Saturday and I am -1 day before
a deadline, this is not good. So, although this thread (and my question) is
drifting towards basic Perl, I try my luck.

I make use of the following construction

package A::B::C;
my %CFG = ( some_tag => some_config, ... }
...
sub do_something {
  ...
  do_something_else( $CFG{some_tag} );
  ...
}
...
1;

After reading much of this discussion I am beginning to wonder if I am 
creating closures? (I must admit that the concept of closures is -for
some reason- very hard for me to grasp). If this is creating closures,
then what would be a better approach?

Regards,

Frank

Re: Scope Question

Posted by Todd Finney <tf...@boygenius.com>.
At 12:22 PM 8/20/2005 -0400, Perrin Harkins wrote:
>Todd Finney wrote:
>>Perhaps I'm just underinformed, but using a variable in a subroutine 
>>without declaring it or passing it in seems like asking for weird bugs.
>
>Or asking for a persistent variable.  Usually "our $color" is a bit 
>clearer for that though.

This is probably just an issue of style, but avoiding stuff like this...

our $x=10;

....600 lines of code...

sub foo {
         return $x++;
}
&foo();

...strikes me as common sense.  Sure, it'll run under strict; that doesn't 
mean that it isn't going to take your foot off eventually.







Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
Todd Finney wrote:
> Perhaps I'm just underinformed, but using a variable in a subroutine 
> without declaring it or passing it in seems like asking for weird bugs.

Or asking for a persistent variable.  Usually "our $color" is a bit 
clearer for that though.

- Perrin

Re: Scope Question

Posted by Todd Finney <tf...@boygenius.com>.
At 10:25 AM 8/20/2005 -0400, Perrin Harkins wrote:
>my $cgi = CGI->new();
>my $color = $cgi->param('color');
>
>sub print_color {
>     print $color;
>}

Am I the only one who never, ever does this (intentionally, anyway)?

Perhaps I'm just underinformed, but using a variable in a subroutine 
without declaring it or passing it in seems like asking for weird bugs.





Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
Christopher H. Laco wrote:
> You're right. This got convoluted. Let's start over.
> 
>   package MyApp;
>   my @context;
> 
>    sub dosomething {
>     push @context, 'doingsomething';
>    };
> 
>   1;
> 
> 
> When this module is loaded into MP then given to each child process, for 
> each request the user makes to a page that calls dosomething(), what is 
> the INITIAL value of @context before dosomething() is called and alters it?
> 
> I'm assuming that it's always () before dosomething() changes it since 
> it's declared as my.

No, the first time you call this it will be ().  Then dosomething() will 
become a closure around @context, and when you call dosomething() again 
it will still have the old value for @context.  If you don't want it to 
retain that value, don't make it a closure, i.e. don't reference a 
variable declared in the enclosing scope.

- Perrin

Re: Scope Question

Posted by "Christopher H. Laco" <cl...@chrislaco.com>.
You're right. This got convoluted. Let's start over.

   package MyApp;
   my @context;

    sub dosomething {
	push @context, 'doingsomething';
    };

   1;


When this module is loaded into MP then given to each child process, for 
each request the user makes to a page that calls dosomething(), what is 
the INITIAL value of @context before dosomething() is called and alters it?

I'm assuming that it's always () before dosomething() changes it since 
it's declared as my.

-=CHris



Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Sat, 2005-08-20 at 08:28 -0400, Christopher H. Laco wrote:
> Huh? Now I'm really confused. MyMod2 calls MyMod1::dosomething alters 
> it's own @context (it should've been push @context, 'foo')...
> 
> MyMod2 calls MyMod1::pushcontext('anotherfoo') to also alter MyMod1s 
> @context.

I've lost track of your example.  If you want feedback on specific code,
please post a working example and I'll look at it.

> Why would each modules @context persist since they're declare 
> as my and their our of closure?

That's what closures do.  Here's a classic example:

my $cgi = CGI->new();
my $color = $cgi->param('color');

sub print_color {
    print $color;
}

This sub will always print whatever $color was the first time the script
was run.  It is a closure, and keeps a private copy of $color.

> Even if they did, that's a moot point as I can resent them during every 
> page request (via AxKits start_document function).

You can't unless you create another closure with access to the same
variable.  These are not globals -- setting $color here later will not
change the $color that print_color() is looking at.

> What I'm more worried about is that I just want those MyMod1 and MyMod2 
> @context changes to be effecting the current request only, not other 
> requests in other apache child processes.

It's impossible for them to affect other child processes.  Processes are
totally separated from each other and run their own perl interpreters.

- Perrin


Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
Boysenberry Payne wrote:
> If you worried about @context persisting my @context ought to keep it 
> local to the scope
> @context is declared in unless you register it as a global while 
> manipulating it.

This is technically true, but confusing.  Because the pushcontext() sub 
below refers to a variable declared in the enclosing scope, it will keep 
a private copy of that variable, and it will persist for the lifetime of 
the perl interpreter.

> package MyMod1;
> my @context = qw( test_mymod1 );
> sub pushcontext {
>     push @context @_;
> }
> 
> package MyMod2;
> my @context = qw( test_mymod2 );
> MyMod1::pushcontext( @context );
> 
> Ought to end up having each @context being:
> 
> MyMod1:: $context[ 0 ] = "test_mymod1"
> MyMod1:: $context[ 1 ] = "test_mymod2"
> MyMod2:: $context[ 0 ] = "test_mymod2"

Yes, and calling MyMod1::pushcontext() again, even on a later request, 
will add to that, putting the new value in $context[2].

- Perrin

Re: Scope Question

Posted by Boysenberry Payne <bo...@humaniteque.com>.
If you worried about @context persisting my @context ought to keep it 
local to the scope
@context is declared in unless you register it as a global while 
manipulating it.  In other
words it should stay only as long as the scope it's declared in.  So 
that:

package MyMod1;
my @context = qw( test_mymod1 );
sub pushcontext {
	push @context @_;
}

package MyMod2;
my @context = qw( test_mymod2 );
MyMod1::pushcontext( @context );

Ought to end up having each @context being:

MyMod1:: $context[ 0 ] = "test_mymod1"
MyMod1:: $context[ 1 ] = "test_mymod2"
MyMod2:: $context[ 0 ] = "test_mymod2"

Each @context would persist as long as the packages exist above.
Each apache child ought to have it's own @context arrays regardless
of whether it's compiled by including it in a startup.pl type use at
apache start up, or required at runtime.

Thanks,
Boysenberry

boysenberrys.com | habitatlife.com | selfgnosis.com

On Aug 20, 2005, at 8:52 AM, Christopher H. Laco wrote:

> Boysenberry Payne wrote:
>
>> Each @context would persist as long as the packages exist above.
>> Each apache child ought to have it's own @context arrays regardless
>> of whether it's compiled by including it in a startup.pl type use at
>> apache start up, or required at runtime.
>
>
> Then what's the point of using my @context vs. @MyApp::context if my 
> @context persists as long as the package does?
>
> From practical mod_perl:
>
>
> 6.4.3. Global Variable Persistence
>
> Under mod_perl a child process doesn't exit after serving a single 
> request. Thus, global variables persist inside the same process from 
> request to request. This means that you should be careful not to rely 
> on the value of a global variable if it isn't initialized at the 
> beginning of each request. For example:
>
> # the very beginning of the script
> use strict;
> use vars qw($counter);
> $counter++;
>
> relies on the fact that Perl interprets an undefined value of $counter 
> as a zero value, because of the increment operator, and therefore sets 
> the value to 1. However, when the same code is executed a second time 
> in the same process, the value of $counter is not undefined any more; 
> instead, it holds the value it had at the end of the previous 
> execution in the same process. Therefore, a cleaner way to code this 
> snippet would be:
>
> use strict;
> use vars qw($counter);
> $counter = 0;
> $counter++;
>
> In practice, you should avoid using global variables unless there 
> really is no alternative. Most of the problems with global variables 
> arise from the fact that they keep their values across functions, and 
> it's easy to lose track of which function modifies the variable and 
> where. This problem is solved by localizing these variables with 
> local( ). But if you are already doing this, using lexical scoping 
> (with my( )) is even better because its scope is clearly defined, 
> whereas localized variables are seen and can be modified from anywhere 
> in the code. Refer to the perlsub manpage for more details. Our 
> example will now be written as:
>
> use strict;
> my $counter = 0;
> $counter++;


Re: Scope Question

Posted by Boysenberry Payne <bo...@humaniteque.com>.
I was just showing you an example to explain the scoping.
In a working situation it just depends on what you need @context for.
my @context allows you to declare @context local to where ever it's
declared.  So:

my @context = qw( test1 );
{
	@context = qw( test2 )
	print @context
	prints "test2"
}
print @context
prints "test1"

As far as using @context vs. @MyApp::context inside MyApp, there is no 
difference.
It's one of those many ways to do the same thing you hear about a lot 
in perl coding.
I myself use an OO approach:

package MyApp;

sub new {
	my $class = shift;
	my $this = {};
	bless( $class, $this )
	return $this;
}

package SomeOtherApp;

my $app_obj = new MyApp;
$app_obj->{context} = [ "test" ];

It makes it easier to keep it all sorted out for me...

Boysenberry

boysenberrys.com | habitatlife.com | selfgnosis.com

On Aug 20, 2005, at 8:52 AM, Christopher H. Laco wrote:

> Boysenberry Payne wrote:
>
>> Each @context would persist as long as the packages exist above.
>> Each apache child ought to have it's own @context arrays regardless
>> of whether it's compiled by including it in a startup.pl type use at
>> apache start up, or required at runtime.
>
>
> Then what's the point of using my @context vs. @MyApp::context if my 
> @context persists as long as the package does?
>
> From practical mod_perl:
>
>
> 6.4.3. Global Variable Persistence
>
> Under mod_perl a child process doesn't exit after serving a single 
> request. Thus, global variables persist inside the same process from 
> request to request. This means that you should be careful not to rely 
> on the value of a global variable if it isn't initialized at the 
> beginning of each request. For example:
>
> # the very beginning of the script
> use strict;
> use vars qw($counter);
> $counter++;
>
> relies on the fact that Perl interprets an undefined value of $counter 
> as a zero value, because of the increment operator, and therefore sets 
> the value to 1. However, when the same code is executed a second time 
> in the same process, the value of $counter is not undefined any more; 
> instead, it holds the value it had at the end of the previous 
> execution in the same process. Therefore, a cleaner way to code this 
> snippet would be:
>
> use strict;
> use vars qw($counter);
> $counter = 0;
> $counter++;
>
> In practice, you should avoid using global variables unless there 
> really is no alternative. Most of the problems with global variables 
> arise from the fact that they keep their values across functions, and 
> it's easy to lose track of which function modifies the variable and 
> where. This problem is solved by localizing these variables with 
> local( ). But if you are already doing this, using lexical scoping 
> (with my( )) is even better because its scope is clearly defined, 
> whereas localized variables are seen and can be modified from anywhere 
> in the code. Refer to the perlsub manpage for more details. Our 
> example will now be written as:
>
> use strict;
> my $counter = 0;
> $counter++;


Re: Scope Question

Posted by "Christopher H. Laco" <cl...@chrislaco.com>.
Perrin Harkins wrote:
> On Fri, 2005-08-19 at 20:55 -0400, Christopher H. Laco wrote:
> 
>>So, changing to
>>
>>package MyMod;
>>
>>    my @context;
>>    sub dosomething {
>>      push @context;
>>      #...do other stuff..
>>    };
>>    sub pushcontext {
>>      push @context, shift;
>>    };
>>
>>1;
>>
>>
>>Woudld fix the persistance issue?
> 
> 
> No.  If you want to call dosomething without making @context persist,
> you need to do it like this:
> 
> my @context;
> dosomething(\@context);
> 
> sub dosomething {
>     my $context_ref = shift;
>     push @{ $context_ref }, ....
> }


Huh? Now I'm really confused. MyMod2 calls MyMod1::dosomething alters 
it's own @context (it should've been push @context, 'foo')...

MyMod2 calls MyMod1::pushcontext('anotherfoo') to also alter MyMod1s 
@context. Why would each modules @context persist since they're declare 
as my and their our of closure?

Even if they did, that's a moot point as I can resent them during every 
page request (via AxKits start_document function).

What I'm more worried about is that I just want those MyMod1 and MyMod2 
@context changes to be effecting the current request only, not other 
requests in other apache child processes.

-=Chris

Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2005-08-19 at 20:55 -0400, Christopher H. Laco wrote:
> So, changing to
> 
> package MyMod;
> 
>     my @context;
>     sub dosomething {
>       push @context;
>       #...do other stuff..
>     };
>     sub pushcontext {
>       push @context, shift;
>     };
> 
> 1;
> 
> 
> Woudld fix the persistance issue?

No.  If you want to call dosomething without making @context persist,
you need to do it like this:

my @context;
dosomething(\@context);

sub dosomething {
    my $context_ref = shift;
    push @{ $context_ref }, ....
}

> I want and changes to contect to only be applicable to the current 
> request...

Don't make closures then.  Pass the value to the sub.

- Perrin


Re: Scope Question

Posted by "Christopher H. Laco" <cl...@chrislaco.com>.
Perrin Harkins wrote:
> On Fri, 2005-08-19 at 20:24 -0400, Christopher H. Laco wrote:
> 
>>I'm assuming that each tome dosomething() changes @contect, it's only 
>>changing @contect for that MP child process.
> 
> 
> Correct.  However, dosomething() is a closure in your code, so @context
> will persist.
> 

So, changing to

package MyMod;

    my @context;
    sub dosomething {
      push @context;
      #...do other stuff..
    };
    sub pushcontext {
      push @context, shift;
    };

1;


Woudld fix the persistance issue?

> 
>>Now, I need to have another module used in that same request, modify the 
>>context of the first...
>>
>>package MyMod;
>>{
>>   my @context;
>>   sub dosomething {
>>     push @context;
>>     #...do other stuff..
>>   };
>>   sub pushcontext {
>>     push @context, shift;
>>   };
>>};
>>1;
>>
>>package MyMod2;
>>{
>>   my @context;
>>   sub dosomething {
>>     push @context;
>>     #...do other stuff..
>>     MyMod::poshcontext('foo');
>>   };
>>};
>>1;
> 
> 
> Those are not referring to the same @context.

Corect, and that's intentional. Each module has it's own @context, but I 
need to have MoMod2 change it's own, as well as the one in MyMod..

>>Is that safe across requests?
> 
> 
> Define safe...

I want and changes to contect to only be applicable to the current 
request...

> 
> 
>>I'm assuming that MyMod is change MyMod2 
>>in the same request/child process?
> 
> 
> No, I don't think so.  They are two different variables named @context
> in different scopes.
> 

Reverse that. MyMod2 is change it's @context, as well as calling MyMod 
and having MyMod change its context as well..

> 
>> FOr that matter, what is the lifetime 
>>of my @context in that situation?
> 
> 
> Forever, since it's used in a closure.
> 
> - Perrin
> 
> 



Re: Scope Question

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2005-08-19 at 20:24 -0400, Christopher H. Laco wrote:
> I'm assuming that each tome dosomething() changes @contect, it's only 
> changing @contect for that MP child process.

Correct.  However, dosomething() is a closure in your code, so @context
will persist.

> Now, I need to have another module used in that same request, modify the 
> context of the first...
> 
> package MyMod;
> {
>    my @context;
>    sub dosomething {
>      push @context;
>      #...do other stuff..
>    };
>    sub pushcontext {
>      push @context, shift;
>    };
> };
> 1;
> 
> package MyMod2;
> {
>    my @context;
>    sub dosomething {
>      push @context;
>      #...do other stuff..
>      MyMod::poshcontext('foo');
>    };
> };
> 1;

Those are not referring to the same @context.

> Is that safe across requests?

Define safe...

> I'm assuming that MyMod is change MyMod2 
> in the same request/child process?

No, I don't think so.  They are two different variables named @context
in different scopes.

>  FOr that matter, what is the lifetime 
> of my @context in that situation?

Forever, since it's used in a closure.

- Perrin