You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Bryn Dyment <br...@hoologic.com> on 2004/07/28 03:20:29 UTC
module variables: all mine during a request?
(I'm used to using mod_perl only via Mason, so apologies in advance if some of my terminology is a little off.)
I'm wondering if this approach to using variables in Perl modules (and setting/accessing these variables during a request) is sane (both for a $dbH and a "generic" variable):
I have three "levels" of files: my .html pages, my Site:: modules, and my Gen:: modules. Site:: modules are site-specific (I have multiple sites running under virtual hosts). Gen:: modules are lower-level, and contain no site-specific code.
When my .html page is invoked, I immediately call Site::Init(), which in turn calls Gen::DB::Connect(), which opens a DBI connection. The actual $dbH variable is defined in the Gen::Init() module. No code in my Site:: modules or my .html pages directly uses/accesses $dbH. As I process the request, my page calls various Site:: functions, which in turn call Gen::DB() for all DB access. At the end of my request, I call Gen::DB::Disconnect().
So far, for small sites, this appoach is working well. I'm wondering if there are any pitfalls. Also, does anything change if my Gen::DB module is preloaded in my startup.pl file? I'm wondering if multiple incoming requests (from different virtual host sites, all using Gen::DB) will cause things to get confused.
Also, in the simpler case, if I have a $foo variable in, say, Gen::Foo, and I set it (at the beginning of the request) via Gen::Foo::Set( '123' ), is this $foo variable "all mine" for the duration of the request? That is, am I guaranteed that another request won't change this value under the first request's nose? And, is this scenario affected by whether or not Gen::Foo is preloaded via startup.pl?
(Sorry for all the question marks in this post.)
___
Bryn Dyment, bryn@hoologic.com
http://www.hoologic.com/
Re: module variables: all mine during a request?
Posted by Perrin Harkins <pe...@elem.com>.
On Tue, 2004-07-27 at 21:20, Bryn Dyment wrote:
> I'm wondering if this approach to using variables in Perl modules (and
> setting/accessing these variables during a request) is sane (both for
> a $dbH and a "generic" variable):
Putting anything stateful in globals is a questionable practice, because
these globals will not be reset if your code dies half way through a
request. It's better to use $r->pnotes() for storing this kind of
stuff, because it gets cleaned up automatically at the end of every
request, even if your code blows up.
However, there is no sharing of variables under mod_perl, so no one else
can affect your globals during a request.
- Perrin
--
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html