如何在mod_perl中添加特定于脚本的lib路径?

时间:2022-10-05 17:37:44

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

我正在尝试使用ModPerl :: Registry将CGI脚本迁移到mod_perl。

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.

脚本使用与脚本位于同一目录中的模块,但由于mod_perl当前目录位于其他位置,因此无效。

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

我尝试使用FindBin添加到@INC,但这是FindBin的样子:

$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.

那么,有没有办法让脚本找出它的位置,并将该目录添加到@INC?理想情况下,使用相同Apache服务器的所有其他脚本都不会将该目录添加到其@INC中。

4 个解决方案

#1


use File::Basename;
use lib dirname( __FILE__ );

#2


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

在你的httpd.conf中,在顶部附近添加以下行:

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

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

然后在startup.pl中,指定所需的模块,如下所示:

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

使用lib qw(/ location / of / module1 / location / of / module1); 1;

And Presto !

和Presto!

#3


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

您是否为每个脚本都有单独的位置或目录,或者它们是否都位于同一个位置?如果是前者,我会使用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>

#4


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

看看lib :: abs。它将相对路径转换为绝对路径,并且可能适合在mod_perl下使用。

#1


use File::Basename;
use lib dirname( __FILE__ );

#2


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

在你的httpd.conf中,在顶部附近添加以下行:

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

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

然后在startup.pl中,指定所需的模块,如下所示:

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

使用lib qw(/ location / of / module1 / location / of / module1); 1;

And Presto !

和Presto!

#3


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

您是否为每个脚本都有单独的位置或目录,或者它们是否都位于同一个位置?如果是前者,我会使用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>

#4


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

看看lib :: abs。它将相对路径转换为绝对路径,并且可能适合在mod_perl下使用。