Setting environment variables for CGI scripts

software development



I am looking for a way to pass certain environment variables to my CGI scripts.

I have tried both using the Apache SetEnv directive in my .htaccess and also using the [E] flag on a dummy RewriteRule, but neither technique works, I believe because the CGI scripts are running as their own processes via suExec rather than inside mod_perl or the equivalent.

In any case, my particular need is to set the PERL5LIB env. variable to point to my locally installed Perl modules.

I know that I could edit the CGI scripts to have the appropriate “use lib” declarations, but I want to enable my local modules for 3rd party CGI scripts that I am using and am likely to upgrade frequently, so modifying the scripts is not a palatable solution.

Many thanks for any suggestions.



To set environment variables in your Perl script:

$ENV{‘PATH’} = “/bin:/usr/bin:/usr/local/bin”;

yerba# rm -rf /etc


Thanks, guice, but I think that you did not quite understand my problem.

I want to set environment variables for my Perl CGI processes externally to those CGI scripts.

i.e., I want to do this the way I normally would with an Apache SetEnv directive, in order that I do not have to modify the CGI scripts themselves, since they are third-party scripts that I plan to frequently upgrade.




Let me get this right. You have a directory you want to put into the library path, something like: /home/name/libs/perl/Library-2.1/

And you want a way to – use lib ("/home/name/libs/perl/Library-2.1") – w/out having to modify the use lib if you update to Library-2.2?

If that’s the case, there’s a simple solution:

ln -s /home/name/libs/perl/Library-2.1 /home/name/libs/perl/Library
use lib("/home/name/libs/perl/Library");[/quote]

Am I still missing what you’re trying to do? Using a soft link is the preferred way and is extremely commonly used, especially on system level file. Java is a good example as many systems will be known to have multiple versions. /usr/local/java will be a soft link to /usr/local/java-1.5jre while somebody might also have /usr/local/java-1.5sdk.

Does that solve your problem?

yerba# rm -rf /etc