You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Mike Carlton <mi...@thecarltons.org> on 2003/12/09 18:45:42 UTC

Shared package globals?

I'm running a web application under Apache::Register and mod_perl 1 --
using CGI::Application, Class::DBI and Template Toolkit.

I have a question on package globals.  My expectation is that each
instance of the Apache intrepeter will have its own copy of the package
globals.
I.e. in any given thread I can assign to a package variable and have it
be unchanged for the remainder of the request.  However I'm seeing weird
behavior that could be explained by the value changing.

I am extracting the currently logged in user from an encoded cookie in
my CGI::App code and setting a package variable value on every invocation:
    package TestApp;
    use base 'CGI::Application';
    use vars qw( $user );
 


    sub cgiapp_prerun
    {
        my $self = shift;
        my $runmode = shift;
        $user = $self->query->cookie('user');
    ...

I want to keep the currently logged in user as a global because I have
Template toolkit callbacks to the Class::DBI object and they perform
authorization checks (e.g. an employee's ssn should not be visible to
other users):
    package Employee;
    use base 'Class::DBI::mysql';
     __PACKAGE__->set_db('Main', "dbi:mysql:test", $dbacct, $dbpw);
    __PACKAGE__->set_up_table('employee');
    sub ssn
    {
        my $self = shift;
       return $self->name eq $TestApp::user ? $self->get('ssn') : "Private";
    }


My users have seen instances where they see the private info of other
users, as if the package var $user had been set to something other than
themself.

I would expect $user to remain unchanged, even if another user hit the
website simultaneously -- their access would set $user of another thread.
Or am I missing something big (would the different apache children share
package globals???)

If my understanding is correct, then I've got a bug elsewhere in my
code.  I'm in the progress of migrating to modperl 2/Apache 2, so
comments on the same scenario in MP2 are of interest too...

Thanks,
--Mike Carlton




-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Shared package globals?

Posted by Stas Bekman <st...@stason.org>.
Mike Carlton wrote:

>>> I'm running a web application under Apache::Register and mod_perl 1 --
>>> using CGI::Application, Class::DBI and Template Toolkit.
>>>
>>> I have a question on package globals.  My expectation is that each
>>> instance of the Apache intrepeter will have its own copy of the package
>>> globals.
>>> I.e. in any given thread I can assign to a package variable and have it
>>> be unchanged for the remainder of the request.  However I'm seeing weird
>>> behavior that could be explained by the value changing.
>>>
>>> I am extracting the currently logged in user from an encoded cookie in
>>> my CGI::App code and setting a package variable value on every 
>>> invocation:
>>>    package TestApp;
>>>    use base 'CGI::Application';
>>>    use vars qw( $user );
>>>
>>>
>>>
>>>    sub cgiapp_prerun
>>>    {
>>>        my $self = shift;
>>>        my $runmode = shift;
>>>        $user = $self->query->cookie('user');
>>>    ...
>>
>>
>>
>> is it possible that cgiapp_prerun() is not always run and you end up 
>> with the data from the previous request/user (since global variables 
>> persist and don't get destroyed at the end of request)?.
>>
>> If you have an entry point that is always executed at the beginning of 
>> a request, like 'sub handler { ... } ', you should make sure to reset 
>> all your globals before using them.
> 
> 
> I'm running this under Modperl::Registry, so don't have my own 
> handler().  

Your top level code is the one running from handler. M::R just wraps your 
script into sub handler {}.

> CGI::App does guarantee that cgiapp_prerun is always run, so 
> that shouldn't be the problem.

I'd double check. It's either is not always running or the user get 
initialized from elsewhere. Or worse you may have closures, which will 
remember a username from the very first execution.

>> Even better, drop the usage of globals and create a class an instance 
>> of which will contain all the objects you will ever want to access 
>> (e.g. using Singleton), which will get destroyed at the end of 
>> request. So you don't have to worry about resetting globals.
>> http://search.cpan.org/search?query=singleton&mode=module
>>
> Aha.  That sounds like a perfect solution (I don't like the globals 
> either).  Many thanks.   I ran into one little quirk -- 
> Apache::Singleton from CPAN won't install on RH 9 with Apache2 (it's 
> tests are looking for Apache.pm, not Apache2).  I guess I'll have to do 
> a little patching there.

I don't think it was ported to mp2, I suppose it shouldn't be too hard to do 
it. Or you can use Class::Singleton. Or even write your own ;)

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Shared package globals?

Posted by Mike Carlton <mi...@thecarltons.org>.

Stas Bekman wrote:

> Mike Carlton wrote:
>
>> I'm running a web application under Apache::Register and mod_perl 1 --
>> using CGI::Application, Class::DBI and Template Toolkit.
>>
>> I have a question on package globals.  My expectation is that each
>> instance of the Apache intrepeter will have its own copy of the package
>> globals.
>> I.e. in any given thread I can assign to a package variable and have it
>> be unchanged for the remainder of the request.  However I'm seeing weird
>> behavior that could be explained by the value changing.
>>
>> I am extracting the currently logged in user from an encoded cookie in
>> my CGI::App code and setting a package variable value on every 
>> invocation:
>>    package TestApp;
>>    use base 'CGI::Application';
>>    use vars qw( $user );
>>
>>
>>
>>    sub cgiapp_prerun
>>    {
>>        my $self = shift;
>>        my $runmode = shift;
>>        $user = $self->query->cookie('user');
>>    ...
>
>
> is it possible that cgiapp_prerun() is not always run and you end up 
> with the data from the previous request/user (since global variables 
> persist and don't get destroyed at the end of request)?.
>
> If you have an entry point that is always executed at the beginning of 
> a request, like 'sub handler { ... } ', you should make sure to reset 
> all your globals before using them.

I'm running this under Modperl::Registry, so don't have my own 
handler().  CGI::App does guarantee that cgiapp_prerun is always run, so 
that shouldn't be the problem. 

>
> Even better, drop the usage of globals and create a class an instance 
> of which will contain all the objects you will ever want to access 
> (e.g. using Singleton), which will get destroyed at the end of 
> request. So you don't have to worry about resetting globals.
> http://search.cpan.org/search?query=singleton&mode=module
>
Aha.  That sounds like a perfect solution (I don't like the globals 
either).  Many thanks.   I ran into one little quirk -- 
Apache::Singleton from CPAN won't install on RH 9 with Apache2 (it's 
tests are looking for Apache.pm, not Apache2).  I guess I'll have to do 
a little patching there.

Cheers,
--Mike Carlton



-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html


Re: Shared package globals?

Posted by Stas Bekman <st...@stason.org>.
Mike Carlton wrote:
> I'm running a web application under Apache::Register and mod_perl 1 --
> using CGI::Application, Class::DBI and Template Toolkit.
> 
> I have a question on package globals.  My expectation is that each
> instance of the Apache intrepeter will have its own copy of the package
> globals.
> I.e. in any given thread I can assign to a package variable and have it
> be unchanged for the remainder of the request.  However I'm seeing weird
> behavior that could be explained by the value changing.
> 
> I am extracting the currently logged in user from an encoded cookie in
> my CGI::App code and setting a package variable value on every invocation:
>    package TestApp;
>    use base 'CGI::Application';
>    use vars qw( $user );
> 
> 
> 
>    sub cgiapp_prerun
>    {
>        my $self = shift;
>        my $runmode = shift;
>        $user = $self->query->cookie('user');
>    ...

is it possible that cgiapp_prerun() is not always run and you end up with the 
data from the previous request/user (since global variables persist and don't 
get destroyed at the end of request)?.

If you have an entry point that is always executed at the beginning of a 
request, like 'sub handler { ... } ', you should make sure to reset all your 
globals before using them.

Even better, drop the usage of globals and create a class an instance of which 
will contain all the objects you will ever want to access (e.g. using 
Singleton), which will get destroyed at the end of request. So you don't have 
to worry about resetting globals.
http://search.cpan.org/search?query=singleton&mode=module


> If my understanding is correct, then I've got a bug elsewhere in my
> code.  I'm in the progress of migrating to modperl 2/Apache 2, so
> comments on the same scenario in MP2 are of interest too...

It isn't any different under mp2.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html