How can I set Perl's inclusion path for modules for a CGI script?

不问归期 提交于 2019-12-19 16:47:37

问题


I’ve got several Perl modules installed on my hosting machine in ~/perl, how do I add them to Perl module path? Setting PERL5LIB or unshifting the paths to @INC surely works, but the environment variable does not help when running as a CGI script and the @INC way is not very portable. Is there a better way? This has to be a common problem, am I missing something?


回答1:


PERL5LIB works just fine for CGI scripts. You just have to set the variable in the right place, such as the server configuration. Which webserver are you using? For Apache, I use the SetEnv directive from mod_env.

@INC is portable. The paths you put in there may not be the same on every machine, but you shouldn't have a problem the with variable itself.

Have you read the FAQ entries in perlfaq8:

  • How do I add the directory my program lives in to the module/library search path?
  • How do I add a directory to my include path (@INC) at runtime?



回答2:


A cleaner way to do it imo is :

use lib "/path/" ;  

there are other interesting and good ways to do it that can be found here:

http://www.slideshare.net/pfig/cpan-training-presentation/

Also, re: CGI scripts, you CAN define PERL5LIB paths for your CGI Env, it just depends on your webhost.

Some Webhosts permit specific control of env variables for the CGI Enviroment, and you have to just set them, others may have a "permit" list that allows variables visible at start time appear in local scope.

May help to read up on how to do this on your specific HTTP server.

There is one more option, if setting ENV is not to your taste: local::lib

Which searches some predefined paths automatically.



来源:https://stackoverflow.com/questions/355267/how-can-i-set-perls-inclusion-path-for-modules-for-a-cgi-script

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