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.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.
http://search.cpan.org/search?query=singleton&mode=module
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:[EMAIL PROTECTED] 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