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

I'm planning on writing a number of scripts with mod_perl (which I am new at) that will pull various shared functions from a master function file.

now, my worry is, since these functions are in a different file, they won't 'cache' properly - some other user will call the script, and it'll have a different script's values, etc.

Up until now, I've just been keeping everything in my mod_perl scripts in one file (copy all functions bodily into each .pl file), but that is getting out of hand - I don't want to have to update every file when a function needs to be tweaked. I've found no tutorials or 'simple answers' to this issue.

Does anyone have any tutorials, or even extremely simple sample code bits ("this is the Autoloader common functions file, with one sample commonly used function. This is the script file, and here it is, using and pulling the common function"), that I could take a look at? I know it is a big thing to ask for what is probably a stupid question, but I can't even find an answer in my mod_perl books..

Thank you so much in advance if you can help!
  • Comment on Mod_Perl and Autoloader - anything special I should know?

Replies are listed 'Best First'.
Re: Mod_Perl and Autoloader - anything special I should know?
by ikegami (Patriarch) on Apr 19, 2009 at 01:36 UTC
    Correct me if I'm wrong, but the purpose of those modules is to avoid compiling functions that aren't used. That's made moot by mod_perl, where the modules are just loaded once.
      But, lets say I have 100 functions in a common functions file. doesn't it 'load each one one' for EACH mod_perl 'preloaded .pl'?

      example:
      Common Function File:
      Fcn A
      Fcn B
      Fcn C


      First .pl File uses A and B.
      Second .pl File uses A and C.
      Third .pl file uses C.


      wouldn't it 'cache' the first file as the first file with all three ABC Fcns (since they are 'required' in), and do the same with the second and the third, so it is as if 9 functions are cached - possibly using up most of the memory (assume the common file has 100's of functions)?

      Thanks again!

        so it is as if 9 functions are cached

        Once a process loads a module, it's loaded. Subsequent requests to load it are no-ops. This is the same in mod_perl as outside of mod_perl. There is no cache.

        $ cat > Foo.pm package Foo; print("Loading ", __PACKAGE__, "\n"); sleep(3); 1; $ time perl -e'use Foo; use Foo; use Foo;' Loading Foo real 0m3.007s # 3s, not 9s user 0m0.004s sys 0m0.000s

        The only difference is that mod_perl uses one process for many requests, and CGI uses a new process for each request.

        possibly using up most of the memory

        The memory's gonna get used up whether you load all the functions up front or one at a time. Since (practically) all of the functions are going to end up loaded anyway, you're not saving anything by loading them one at a time.


        So,

        Load the module with your collection of functions in mod_perl's startup script, and it'll be loaded forever more. It won't even need to be reloaded when Apache restarts one of its children.

        Even if you don't load the module from mod_perl's startup script, once a child loads the module it will stay loaded. If the child handles 10,000 requests before being restarted, you saved your module from being loaded 9,999 over a CGI script.

        Loading the functions one at a time just adds overhead since the functions are going to end up loaded anyway.

Re: Mod_Perl and Autoloader - anything special I should know?
by MashMashy (Sexton) on Apr 18, 2009 at 22:19 UTC
    further info - "Autoloader" or "SelfLoader" are both fine, as long as they work. :)