1

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?

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
  • If you want some code run every time the module is [`use`d](http://perldoc.perl.org/functions/use.html "perldoc -f use") you would put it inside of `sub import {...}`. I don't know if this will work for you though. – Brad Gilbert Dec 05 '13 at 18:22
  • Thank you for your time - unfortunately, that didn't help. Here's what i did: – Guntram Blohm Dec 06 '13 at 15:55
  • Bah, no rep to edit my question or first comment. Thank you for your time - unfortunately, that didn't help. I wrote a test script "a.pl" that 'use'es b.pm, with b.pm having an import function that prints out some stuff. The first few reload's in the browser show this printed text; after that (when each subprocess has compiled my a.pl once), the import function isn't called anymore, and no text printed. But you just gave me an idea -- there should be an apache hook for "new request just incoming", and i guess i should be able to call some init function in each of the modules from there. – Guntram Blohm Dec 06 '13 at 16:03

0 Answers0