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

Hi,
here a perl newbie with very little English knowledge, currently using mod_perl and trying to import all needed modules at Apache startup.
I am not able to find out if there is a way to pass to PerlModule directive the functions and/or parameters I want to import/set in my namespace. For example, instead of importing modules inside my script like this:
use DBI; use CGI qw(:cgi -compile -utf8);
I am wondering if there is a way to do it in Apache conf., like this:
PerlModule Apache::DBI PerlModule DBI PerlModule CGI ...and??
thank you in advance for the help you will want to give.

Replies are listed 'Best First'.
Re: importing functions with "PerlModule" Apache directive
by Khen1950fx (Canon) on May 09, 2012 at 09:32 UTC
    PerlModule is equivalent to require. For example:
    #!usr/bin/perl use strict; use warnings; require Apache::DBI; require DBI; require CGI;
    In your conf:
    PerlModule Apache::DBI DBI CGI
    Note that require is for "after" configuration. To use your modules at startup, use PerlLoadModule:
    PerlLoadModule Apache::DBI DBI CGI
Re: importing functions with "PerlModule" Apache directive
by Anonymous Monk on May 09, 2012 at 10:50 UTC

    If you are trying to move out the use statements from the code into the Apache config, I don't think it makes much sense. A goal of mod_perl is to load the required modules at Apache startup, which will improve performance and memory use. The subsequent use at the actual Perl script costs very little -- it only imports the module's functions into the script's namespace.

    Besides, if you want to erase those use statements from the actual scripts, it will be a maintenance nightmare should any of the scripts ever have to be moved.

      I agree about the maintenance issue. In this case the application being developed is strictly tied up with the system and its own peculiar configuration.
Re: importing functions with "PerlModule" Apache directive
by tobyink (Canon) on May 09, 2012 at 14:24 UTC

    This is really not what PerlModule is for, but depending on how your script is structured, it should be doable.

    Find your modules directory - something like:

    /usr/lib/perl5/site_perl/5.14.2 (or whatever Perl version you're using)

    And then create a file called ApacheAutoload.pm. In that file, include:

    package main; # this is important use DBI; use CGI qw(:cgi -compile -utf8); # ... and so on

    Then in your Apache config file, do this:

    PerlModule ApacheAutoload
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      package main; # this is important
      important to add to any Registry scripts as well :) otherwise __PACKAGE__ is ModPerl::ROOT::ModPerl::Registry::_path_to_file
      In your opinion is it more cleaner to use this method or the PerlPostConfigRequire directive?

        I'd be wanting to take a long, hot shower after either method.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: importing functions with "PerlModule" Apache directive
by Anonymous Monk on May 09, 2012 at 10:50 UTC
      I was just thinking about to use the PerlPostConfigRequire directive, I actually just switched to it in my apache.conf and everything is working fine:
      PerlPostConfigRequire /etc/apache2/startup.pl
      It is not yet clear to me the behavior with exported functions, I mean, I put in startup.pl
      use CGI qw(-compile -utf8);
      and avoided to import CGI in my test script, but I can still call Vars() function directly, which from CGI documentation should not be visible:
      #!/usr/bin/perl $ENV{PATH}=''; local our $o; $o->{authenticated}=0; local our $cookie; local our $duration=600; local our $r=new CGI; local our $p=Vars; ...
      What's going on?

        and avoided to import CGI in my test script, but I can still call Vars() function directly, which from CGI documentation should not be visible:

        What's going on?

        You're imagining things :)

        Even if you used  use CGI qw(-compile -utf8 Vars ); there is no way Vars would be available to your script , unless you're using something ( its not ModPerl::Registry ) to make it available