How do I set Perl's @INC for a CGI script?

被刻印的时光 ゝ 提交于 2019-12-11 05:29:43

问题


I have the following, simplest Perl CGI script:

use strict;
use warnings;
use CGI();
use CGI::Carp qw(fatalsToBrowser);

use Template;

print CGI::header();

foreach(@INC) {
    print "$_\n";
}

When called (http://[..]/cgi-bin/p.cgi) I am given the following error:

Can't locate Template.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /home/pistacchio/webapps/htdocs/cgi-bin/p.cgi line 8.
BEGIN failed--compilation aborted at /home/pistacchio/webapps/htdocs/cgi-bin/p.cgi line 8.

I made sure that Template is installed and indeed when running this program from shell it works (loads Template) and outputs:

Content-Type: text/html; charset=ISO-8859-1

/home/pistacchio/lib/perl5
/home/pistacchio/lib/perl5/lib/i386-linux-thread-multi
/home/pistacchio/lib/perl5/lib
/home/pistacchio/lib/perl5/lib/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.8
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.8
/usr/lib/perl5/vendor_perl
/usr/lib/perl5/5.8.8/i386-linux-thread-multi
/usr/lib/perl5/5.8.8

Template is installed in /home/pistacchio/lib/perl5/lib/i386-linux-thread-multi

[pistacchio@web118 i386-linux-thread-multi]$ pwd
/home/pistacchio/lib/perl5/lib/i386-linux-thread-multi
[pistacchio@web118 i386-linux-thread-multi]$ ls
auto  perllocal.pod  Template  Template.pm

This directory is correctly listed in env and, as previously posted, in @INC. In @INC it is shown twice, so I even tried to pop it out before calling use Template, but without result. From env:

[pistacchio@web118 i386-linux-thread-multi]$ env
[..]
PERL5LIB=/home/pistacchio/lib/perl5:/home/pistacchio/lib/perl5/lib:/home/pistacchio/lib/perl5/lib/i386-linux-thread-multi
[..]

Removing use Template gets rid of the problem.


回答1:


I would suggest adding the following to your CGI

  use lib "/home/pistacchio/lib/" ;

The PERL5LIB env variable is presumably not available to CGI programs.

Edit What I meant, any value you have set in PERL5LIB from a shell will not be available.




回答2:


The webserver doesn't run as your user, so its environment isn't your user environment. You can set that up in a variety of ways depending on your webserver. In Apache, you can use the SetEnv directive:

 SetEnv PERL5LIB /path/to/your/libs

That then applies to everything under it. If you have it in a .htaccess file, for instance, it applies to everything under that directory.

If you can't do something like that, you're stuck setting the value of @INC in each script yourself with the lib pragma.



来源:https://stackoverflow.com/questions/2680997/how-do-i-set-perls-inc-for-a-cgi-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!