主机是Linux.我在同一目录中有多个文件.所有*.pl文件在开头都包含以下内容,仅在注释中有所不同:
#!/usr/bin/perl -w
BEGIN { chdir('/home/httpd/vhosts/mysite.com/httpdocs/newsbot'); unshift(@INC, "/home/httpd/vhosts/mysite.com/httpdocs/newsbot"); }
use Form;
use File;
use Mysite;
#Read in All Form Variables
&ReadInForm;
Run Code Online (Sandbox Code Playgroud)
该文件Form.pm包含ReadInForm子例程,没有别的.
sub ReadInForm {
}
1;
Run Code Online (Sandbox Code Playgroud)
奇怪的是,上面的输出是完全不一致的.有时它执行正常,但在脚本末尾显示"内部服务器错误"消息并将以下内容放入error_log文件中:
参数""在/usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi/ModPerl/RegistryCooker.pm第171行的子例程条目中不是数字.\n,referer:http:// www .mysite.com /了Newsbot/groupkeywords.pl
其他时候,它不会执行,并将以下内容输出到浏览器:
未定义的子例程&ModPerl :: ROOT :: ModPerl :: Registry :: home_httpd_vhosts_mysite_2ecom_httpdocs_newsbot_groupkeywords_2epl :: ReadInForm在/home/httpd/vhosts/mysite.com/httpdocs/newsbot/groupkeywords.pl第11行调用.
其他时候它正常工作,没有错误.
奇怪的是它是不一致的.我可以从文件中获取一个输出,几分钟后刷新,然后获取另一个.我甚至有一些"内部服务器错误"消息和一个500响应标题,没有实际内容.评论这条线&ReadInForm;每次都能解决这个问题,所以我把它缩小到了这个范围,但是我放入的并不重要Form.pm.我甚至可以放一个空的子程序(正如我上面所说的那样),它仍然无法解决问题.
我甚至不确定如何调试这个.它怎么可能不一致呢?perl编译器是否在幕后缓存?
显然你的主机正在使用Apache mod_perl.您必须为mod_perl显式编码.顺便说一句,你的代码闻起来有perl 4和cgi-lib.pl,1996年份.简单地说,在mod_perl下你的脚本无法工作,因为全局变量是完全禁止的,你也无法修改mod_perl下的@INC.你最好use strict; use warnings;在你的脚本中加上" ",并使用适当的CGI或mod_perl形式解析模块和函数.
但是,这种代码应该有效:
#!/usr/bin/perl
# I don't know what this BEGIN block is for, but I suspect it may not work or causes subtle bugs...
BEGIN { chdir('/home/httpd/vhosts/mysite.com/httpdocs/newsbot'); }
use strict;
use warnings;
use Form;
use File;
use Mysite;
#Read in All Form Variables
Form::ReadInForm();
Run Code Online (Sandbox Code Playgroud)
在Form.pm中使用它:
use strict;
use warnings;
package Form;
sub ReadInForm {
}
1;
Run Code Online (Sandbox Code Playgroud)
编辑:如果您的代码是旧的并且您希望为您节省大修,您可以创建一个包含所有脚本代码和变量声明的"main"子,并简单地调用它:
旧脚本:
#!/usr/bin/perl
# the following line will fail with "undeclared variable" under "use strict"
$a="test";
print "$a\n";
Run Code Online (Sandbox Code Playgroud)
新脚本:
#!/usr/bin/perl
use strict;
use warnings;
# call the main loop enclosing everything else
main();
sub main {
# first declare all variables
my $a;
# then add there all the code:
$a="test";
print "$a\n";
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,脚本可以廉价且快速地"现代化".