views:

196

answers:

4

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.

+3  A: 
use File::Basename;
use lib dirname( __FILE__ );
David
Sinan Ünür
This works: use File::Basename; use lib dirname( __FILE__ );Your suggestion doesn't work, but it put me on the right track.Thanks.
Mathieu Longtin
David's suggestion did not work because of the colon after `BEGIN`.
Sinan Ünür
Be careful, I have had mod_perl empty out %INC on me. Hartmut Behrens suggestion is how we fixed it.
Chas. Owens
+1  A: 

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>
Sinan Ünür
I assume you then do something like "use lib $ENV{THISONE_LIB}"?
Mathieu Longtin
@Mathieu Yes. In this case David's answer is better though.
Sinan Ünür
+1  A: 

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

Gurunandan
+1  A: 

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 !

That makes those directory global. I have many apps running on the same httpd server, I'd rather not mix the include directories.
Mathieu Longtin