3

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.

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
Mathieu Longtin
  • 15,922
  • 6
  • 30
  • 40

4 Answers4

6
use File::Basename;
use lib dirname( __FILE__ );
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
David
  • 802
  • 5
  • 6
  • 1
    I am not sure if `__FILE__` remains unmolested by `mod_perl`. If it is safe, then there is no need for `BEGIN` & `unshift` (besides, there should be no colon after BEGIN. – Sinan Ünür Jul 22 '09 at 14:44
  • 1
    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 Jul 22 '09 at 14:45
  • David's suggestion did not work because of the colon after `BEGIN`. – Sinan Ünür Jul 22 '09 at 14:46
  • Be careful, I have had mod_perl empty out %INC on me. Hartmut Behrens suggestion is how we fixed it. – Chas. Owens Jul 28 '09 at 19:21
2

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 Jul 29 '09 at 04:55
1

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
  • 116,958
  • 15
  • 196
  • 339
1

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

Gurunandan Bhat
  • 3,544
  • 3
  • 31
  • 43