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

G'Day Monks,

This may be a little offtopic as it's mostly an Apache question, but I thought one of you monks might have run into this before and could help.

I'm in a corporate environment in which perl and perl modules are housed on a central server and moutned to workstations via NFS. The upshot of this is that IT controls what modules are installed. So to add perl modules, I crated a local module directory on my workstation and made the PERL5LIB environmental variable point to it. This works great...until I try to write CGI scripts run from Apache on my workstation. Perk works fine, of course, but it doesn't seem to have picked up the PERL5LIB variable--Apache throws an error telling me that one of the modules I need for a script isn't in @INC. When it shows me @INC, it doesn't include my local module directory.

Any idea how I can make Apache pick up my PERL5LIB? I tried becoming root, setting PERL5LIB, and restarting Apache, but to no avail. I'm on a RedHat system if it makes any difference.

Replies are listed 'Best First'.
Re: PERL5LIB with Apache
by Abigail-II (Bishop) on Oct 29, 2003 at 17:31 UTC
    First you should realize that most good CGI program will run with taint checking enabled. If taint checking is enabled, PERL5LIB will be ignored (and rightly so).

    As for setting PERL5LIB and restarting Apache, how did you set it, and how did you restart it? Did you export PERL5LIB from your shell, and did you start Apache from that shell? Perhaps Apache shrubs its environment, but I've never heard it did.

    However, you are probably better off by using "use lib qw {...};" on top of your programs.

    Abigail

Re: PERL5LIB with Apache
by bart (Canon) on Oct 29, 2003 at 17:46 UTC
    Check out PassEnv and SetEnv as part of mod_env.

    Without PassEnv, environment variables from outside get stripped away. That's why you can't see them.

    You can set environment with SetEnv, which seems like the more interesting approach to me for CGI scripts.

    Both can be used both in a .htaccess file, or in httpd.conf.

Re: PERL5LIB with Apache
by Beechbone (Friar) on Oct 29, 2003 at 17:52 UTC
    Don't restart Apache. Stop it and start it.

    If you restart it, you really signal the running Apache to recycle---it won't know anything about the environment of your shell.

    But I'd really recommend to use a "startup.pl" module that "use lib"s the required path(s).

    PerlModule lib/perl/startup.pl

    if I remember correctly...


    Search, Ask, Know
Re: PERL5LIB with Apache (PM's)
by tye (Sage) on Oct 29, 2003 at 20:33 UTC

    PerlMonks now uses this:

    use lib "/home/onthe/range"; # For when this is compiled lib->import("/home/onthe/range"); # For subsequent hits
    The first is needed for if you have any 'use' statements happening at compile-time of your main script. The second is needed if you have any use/require statements happening at run time (and are using mod_perl).

    We used to have a config file that set this up seperately for all of our scripts but it just disappeared one day (or I'd tell what it did to provide you another alternative). (And before that we had a bunch of 'use lib' statements everywhere we did use/require at run time.)

    Note: The terms "compile time" and "run time" are a bit ambiguous. Sometimes they are used to refer to the per-statement compile vs. run times. Sometimes they are used to distinguish between before or after when the main script starts running "regular" code. I don't usually have difficulty telling which is meant so I'll just leave it at that. (:

                    - tye
Re: PERL5LIB with Apache
by TomDLux (Vicar) on Oct 29, 2003 at 18:06 UTC
Re: PERL5LIB with Apache
by nite_man (Deacon) on Oct 30, 2003 at 07:31 UTC

    You can create file .htaccess and define there path to your library:

    PerlSetEnv PERL5LIB /path/to/your/library
    But notice that this should be allowed do in http.conf:
    <Directory "/var/www/html"> Options Indexes FollowSymLink AllowOverride All # !!!!!! Order allow,deny Allow from all </Directory>
    In this case you can change this path without restart Apache.

    Also, you can create some start up script, where you can define path to your libraries:

    use lib qw('/path/to/your/library');
    But notice that if you change this path you should restart Apache!

    I think it will be useful for you to see .htaccess files howto and CGI to mod_perl Porting. mod_perl Coding guidelines.

    Hope it will help you.

    _ _ _ _ _ _
      M i c h a e l

Re: PERL5LIB with Apache
by Anonymous Monk on Oct 30, 2003 at 13:43 UTC
    Thanks for all the great suggestions! I have an additional one to add: change the shebang line.

    !/path/to/perl -I /path/to/local/modules

    Is there any particular reason that this would be better/worse than "use lib qw{...}"? It seems to me that if I do it this way I'll get all of my locally cached modules and only have to change one line of the code, whereas with "use lib qw{...}" I would need to change several lines of code (or am I miinterpreting this?). Changing the shebang seems more portable (since we shuffle servers/directories fairly regularly). Any pros/cons?

    Thanks again!