perl cgi 脚本的环境变量
我有一个类似于以下内容的 cgi 脚本:
BEGIN {
unshift (@INC, "$ENV{'HOME'}/www/cgi-bin/SiteSpecific");
}
print "Content-type: text/html\n\n";
use SiteObject;
my $siteObjInst = SiteObject->instance();
print $siteObjInst->{HideFields};
这可以从命令提示符处正常运行,但从浏览器作为 CGI 脚本运行时会失败。 $ENV{'HOME'} 可能未设置,因为脚本找不到该模块。
是不是CGI脚本没有在shell中运行并且没有找到环境变量?
如果上述情况成立,我是否需要使用其他方式在 BEGIN 块中设置所需的变量?
感谢您的帮助。
I have a cgi script similar to the following:
BEGIN {
unshift (@INC, "$ENV{'HOME'}/www/cgi-bin/SiteSpecific");
}
print "Content-type: text/html\n\n";
use SiteObject;
my $siteObjInst = SiteObject->instance();
print $siteObjInst->{HideFields};
This would run fine from the command prompt, but fails when run as a CGI script from a browser. The $ENV{'HOME'} is perhaps not set as the script cannot find the module.
Is it that the CGI scripts are not run within a shell and do not find the environment variables?
If the above is true, do I need to set the desired variables within the BEGIN block using some other means?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CGI 程序的环境由 Web 服务器设置。 HOME 是否设置取决于您的 Web 服务器的设置方式,如果设置了它,它可能会指向 Web 服务器运行的用户的主目录,而不是您的主目录。
您可以从 CGI 程序打印 $ENV{HOME} 的值,或者更好的是,打印整个 %ENV 哈希值以查看到底发生了什么。
根据我的经验,最好将额外库的完整路径硬编码或在外部设置路径(例如使用 PERL5LIB)。如果您从程序内部设置它,请使用“lib”编译指示,而不是直接修改@INC:
A CGI program will have its environment set by the web server. HOME may or not be set depending on how your web server is set up, and if it is set it will probably point to the home directory of the user that the web server is running as, not your home directory.
You can print the value of $ENV{HOME} from the CGI program, or even better, print the entire %ENV hash to see what's really happening.
In my experience its better to either hardcode the full path to extra libraries or set the path externally (eg using PERL5LIB). If you're setting it from inside the program, use the "lib" pragma rather than modifying @INC directly:
您绝对不能保证您的 shell 和运行 Web 服务器的 id 具有相同的
$ENV{HOME}
变量。暂时忘记所有这些代码并尝试这个:或者甚至这个:
You definitely can't guarantee that your shell and the id running the web server have the same variable for
$ENV{HOME}
. Forget all of that code for a little while and try this:Or even this:
对于一些问题和潜在答案的列表,我将从 < em>如何对 Perl CGI 脚本进行故障排除?。
For a list of some issues and potential answers, I'd start with How can I troubleshoot my Perl CGI script?.