Best way to run module initialization code from mod_perl2 for every new HTTP request?

493 views Asked by At

I have "inherited" a web server that has about 200 cgi scripts, all of them written in perl. One of my tasks is converting them to run unter mod_perl2, without losing CGI runnability.

Now, there are about 10 perl modules, each of which is used from some, but not all, of the CGI scripts; and some of the modules use each other as well.

In the CGI version, some of the modules do their own initialization when they're loaded, with the initialization depending on the script, the QUERY_STRING, PATH_INFO and the like. Think of something like (stripped down a lot to explain, i wouldn't trust PATH_INFO to specify anything written to in a real live situation)

package MyCompany::Directories;
our ($datadir);
sub getDataDir() { return $datadir; }
$datadir=$ENV{PATH_INFO};


package MyCompany::MyModule2
use MyCompany::Directories;
....

# main CGI script
use MyCompany::Directories;
use MyCompany::MyModule2;
....
open(LOG, ">".MyCompany::Directories::getDataDir()."log.txt";
....

In CGI perl, all is well. MyCompany::Directories initializes $datadir the first time it's used, and the initialization code won't get called more than once even if several modules use MyCompany::Directories. In mod_perl2, the module gets 'use'd only once, so $datadir never gets reset on subsequent requests.

To fix this, i could add a MyCompany::Directories::init() function that i call in every CGI script. Or i could convert the MyCompany::Directories module to a "real" OO module, calling new whenever i want to use it. But both cases require me to modify all my CGIs, which i'd like to avoid. Is there any other good way to achieve re-initialization on every new request, preferrably one that requires touching the module only, not the plethora of scripts that use it?

0

There are 0 answers