How do I add a script specific lib path in mod_perl?

3.5k Views Asked by At

I'm trying to migrate CGI scripts to mod_perl using ModPerl::Registry.

The scripts use modules that are in the same directory as the script, but since mod_perl current directory is elsewhere, that doesn't work.

I tried using FindBin to add on to the @INC, but here's what FindBin looks like:

$FindBin::Bin: /usr/sbin
$FindBin::Script: httpd

Which is no use at all.

So, is there a way for the script to figure out where it is, and add that directory to @INC? Ideally, all the other scripts using the same Apache server wouldn't get that directory added to their @INC.

4

There are 4 best solutions below

4
On BEST ANSWER
use File::Basename;
use lib dirname( __FILE__ );
0
On

Do look at lib::abs. It converts a relative path into an absolute path and is probably ideal for use under mod_perl.

2
On

Do you have a separate Location or Directory for each script or do they all live in the same place? If the former, I would use PerlSetEnv

Alias /apps/thisone/ /srv/http/site/apps/thisone/
<Location /apps/thisone/>
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    PerlSetEnv MYLIB /srv/http/site/apps/thisone/lib
    Options +ExecCGI
    Order allow,deny
    Allow from all 
</Location>

If the latter:

Alias /apps/ /srv/http/site/apps/
<Location /apps/thisone/>
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    PerlSetEnv THISONE_LIB /srv/http/site/apps/thisone/lib
    PerlSetEnv THATONE_LIB /srv/http/site/apps/thisone/lib
    Options +ExecCGI
    Order allow,deny
    Allow from all 
</Location>
1
On

In your httpd.conf add the following line close to the top:

PerlRequire /location/of/this/script/startup.pl

Then in startup.pl, specify the required modules, like so:

use lib qw(/location/of/module1 /location/of/module1); 1;

And Presto !