BMaximus has asked for the wisdom of the Perl Monks concerning the following question:

I just got a new job and I'm going back to programing Perl full time. However they don't use mod_perl at all and I'm used to programing for it. One of the things I used to do is dynamicaly make functions that work on MySQL or whatever database I'm using at the time and at startup those functions are generated and stuck in memory for use so that all Apache children for mod_perl have access to those functions. In CGI you can't really do this and you have to generate those functions each time the script is run. So is there a way to generate functions on the fly, stick them in memory and share them to different CGI processes without having to generate them each and every time the script is run? I would think generating them each time would pose a significant and unnecessary load on the server and bog it down. I looked at Cache::FileCache since Cache::SharedMemoryCache said it was pretty much obsolete and had limitations but I'm not sure if either of those would help.

BMaximus
  • Comment on Is it possible to mimic mod_perl like startup for CGI?

Replies are listed 'Best First'.
Re: Is it possible to mimic mod_perl like startup for CGI?
by eyepopslikeamosquito (Bishop) on Oct 06, 2005 at 20:39 UTC

    Apart from trying to persuade them to use mod_perl ;-) have you had a look at CGI::SpeedyCGI? I've not used it, so I can't endorse it, but it seems to be close to what you're looking for and is described in this YAPC 2001 paper. Another persistent Perl environment I'm aware of is PPerl. Another general approach to speeding up CGI is FastCGI.

Re: Is it possible to mimic mod_perl like startup for CGI?
by chromatic (Archbishop) on Oct 06, 2005 at 20:41 UTC

    With the standard CGI model, where your web server forks off a process to run an external program, it's possible to do such a thing, but it seems really tricky and fragile. The web server doesn't necessarily know that it'll run a Perl program and the perl binary it launches almost definitely doesn't know that it would have subroutines already in memory somewhere. Even if it did, it would have to do a substantial amount of bookkeeping to bring that code in to the rest of the program.

    Perhaps CGI::FastCGI would work more to your liking.

      With the standard CGI model, where your web server forks off a process to run an external program, it's possible to do such a thing, but it seems really tricky and fragile.

      It's not really that tricky or fragile so long as the external program knows it's going to run a perl program. That's how CGI::SpeedyCGI (a.k.a PersistentPerl) does it. It has a small binary front-end that looks for the persistent perl process, starts it if it doesn't exist already, and then hands off control to it. All you have to do is change your shebang line to point at that front-end. (And code for its persistent environment.)

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Is it possible to mimic mod_perl like startup for CGI?
by TedYoung (Deacon) on Oct 06, 2005 at 21:11 UTC

    As a fulltime Perl CGI programmer, I can tell you that you are going to have to think 100% backwards from mod_perl. For example, instead of trying to create the function once and only once, look towards generating them on demand.

    For example, the package containing all of these functions can have a autoload that will create the function the first time it is requested.

    It is hard to say for sure the best path without knowing how you are generating these functions, but also checkout the core modues SelfLoader and AutoLoader. They will delay the compilation of functions until they are first used.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: Is it possible to mimic mod_perl like startup for CGI?
by perrin (Chancellor) on Oct 06, 2005 at 22:19 UTC
    Could you give an example of what these functions that you're talking about are?