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



Reply via email to