旧的 Perl 脚本现在出现错误:名称仅使用一次
Old Perl Script now with Error-: Name Used only once
我继承了一个旧的 perl 脚本,它从各种形式中获取搜索词并对其进行处理。
我现在在日志中看到脚本抛出以下错误:
mastersearch.cgi:名称 "main::error_fields" 仅使用一次:可能在 /var/www/cgi-bin/mastersearch.cgi 第 203 行出现错字。
我在下面指出了错误行,这里是断章取义的:
($error,@error_fields) = @_;
我读过只使用一个变量一次,所以看起来这一行,数组错误字段只使用了一次。但是为什么不行呢?
我可以使用 strict 来纠正这个问题吗?我读过其他堆栈问题,但没有看到它们之间的关系,特别是:
"Used only once" warnings can be generated when autodie or Fatal is
used with package filehandles (eg, FILE ). Scalar filehandles are
strongly recommended instead.
那么我的问题是:
- 如何消除错误,我不是 perl 人!
和不太重要的:
- 是时候转储这个脚本了吗?
提前致谢
#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
# @referers allows forms to be located only on servers which are defined
# in this field. This prevents other servers from using your SearchWWW script.
@referers = ('http://ourname.edu');
# Check Referring URL
&check_url;
# Parse Form Contents
&parse_form;
# Submit to search engine
&search;
sub check_url {
if ($ENV{'HTTP_REFERER'}) {
foreach $referer (@referers) {
if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
$check_referer = '1';
last;
}
}
}
else {
$check_referer = '1';
}
if ($check_referer != 1) {
&error('bad_referer');
}
}
sub parse_form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
}
else {
&error('request_method');
}
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex())/eg;
$value =~ s/<!--(.|\n)*-->//g;
$contents{$name} = $value;
}
}
sub search {
# Assign the value from search to search_for
$search_for = $contents{'search'};
# Places a plus between two words
$search_for =~ tr/ /+/;
# If no engine entered goto Google by default
if ($contents{'location'} eq "15"){
print "Location: http://ourname.edu/search?q=$search_for";
}
if ($contents{'location'} eq "11"){
print "Location:http://ourname.edu/search~S9/?searchtype=X& searcharg=$search_for&searchscope=39\n\n"
}
# ....snipped as redundant for question- more locations here
#resume
exit;
}
sub error {
# -->the error line, 203, is below
($error,@error_fields) = @_;
print "Content-type: text/html\n\n";
if ($error eq 'bad_referer') {
print qq~
<html>\n <head>\n <title>Bad Referrer - Access Denied</title></head>\n
<body>\n <center>\n <h1>Bad Referrer - Access Denied</h1></center>\n
The form that is trying to use this SearchWWW Program</a>\n
resides at: $ENV{'HTTP_REFERER'}, which is not allowed to access this cgi script.<p>\n
Sorry!\n
</body></html>\n ~;
}
else {
print qq~
<html>\n <head>\n <title>Error: Request Method</title>\n </head>
</head>\n <body>
<center>\n\n
<h1>Error: Request Method</h1>\n </center>\n\n
The Request Method of the Form you submitted did not match\n
either GET or POST. Please check the form, and make sure the\n
method= statement is in upper case and matches GET or POST.\n
</body></html>\n ~;
}
exit;
}
首先,这是警告,不是错误。它不会停止脚本的执行,它只是试图提醒您 可能 有问题,您应该检查一下。
要获取有关警告的更多详细信息,请将 use diagnostics;
添加到脚本的顶部。在这种情况下,您会看到:
Typographical errors often show up as unique variable names.
If you had a good reason for having a unique name, then just mention it
again somehow to suppress the message...
由于您在初始分配后从未使用 @error_fields
,Perl 认为您可能打错了字并警告您。如果您从未对其进行任何操作,则没有理由分配给 @error_fields
,因此您只需将其从该行中删除即可:
($error) = @_;
但是该脚本还有很多其他问题(例如没有 use strict;
)。我会把它扔掉。
我继承了一个旧的 perl 脚本,它从各种形式中获取搜索词并对其进行处理。
我现在在日志中看到脚本抛出以下错误: mastersearch.cgi:名称 "main::error_fields" 仅使用一次:可能在 /var/www/cgi-bin/mastersearch.cgi 第 203 行出现错字。
我在下面指出了错误行,这里是断章取义的:
($error,@error_fields) = @_;
我读过只使用一个变量一次,所以看起来这一行,数组错误字段只使用了一次。但是为什么不行呢?
我可以使用 strict 来纠正这个问题吗?我读过其他堆栈问题,但没有看到它们之间的关系,特别是:
"Used only once" warnings can be generated when autodie or Fatal is used with package filehandles (eg, FILE ). Scalar filehandles are strongly recommended instead.
那么我的问题是:
- 如何消除错误,我不是 perl 人!
和不太重要的:
- 是时候转储这个脚本了吗?
提前致谢
#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);
# @referers allows forms to be located only on servers which are defined
# in this field. This prevents other servers from using your SearchWWW script.
@referers = ('http://ourname.edu');
# Check Referring URL
&check_url;
# Parse Form Contents
&parse_form;
# Submit to search engine
&search;
sub check_url {
if ($ENV{'HTTP_REFERER'}) {
foreach $referer (@referers) {
if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
$check_referer = '1';
last;
}
}
}
else {
$check_referer = '1';
}
if ($check_referer != 1) {
&error('bad_referer');
}
}
sub parse_form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
}
else {
&error('request_method');
}
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex())/eg;
$value =~ s/<!--(.|\n)*-->//g;
$contents{$name} = $value;
}
}
sub search {
# Assign the value from search to search_for
$search_for = $contents{'search'};
# Places a plus between two words
$search_for =~ tr/ /+/;
# If no engine entered goto Google by default
if ($contents{'location'} eq "15"){
print "Location: http://ourname.edu/search?q=$search_for";
}
if ($contents{'location'} eq "11"){
print "Location:http://ourname.edu/search~S9/?searchtype=X& searcharg=$search_for&searchscope=39\n\n"
}
# ....snipped as redundant for question- more locations here
#resume
exit;
}
sub error {
# -->the error line, 203, is below
($error,@error_fields) = @_;
print "Content-type: text/html\n\n";
if ($error eq 'bad_referer') {
print qq~
<html>\n <head>\n <title>Bad Referrer - Access Denied</title></head>\n
<body>\n <center>\n <h1>Bad Referrer - Access Denied</h1></center>\n
The form that is trying to use this SearchWWW Program</a>\n
resides at: $ENV{'HTTP_REFERER'}, which is not allowed to access this cgi script.<p>\n
Sorry!\n
</body></html>\n ~;
}
else {
print qq~
<html>\n <head>\n <title>Error: Request Method</title>\n </head>
</head>\n <body>
<center>\n\n
<h1>Error: Request Method</h1>\n </center>\n\n
The Request Method of the Form you submitted did not match\n
either GET or POST. Please check the form, and make sure the\n
method= statement is in upper case and matches GET or POST.\n
</body></html>\n ~;
}
exit;
}
首先,这是警告,不是错误。它不会停止脚本的执行,它只是试图提醒您 可能 有问题,您应该检查一下。
要获取有关警告的更多详细信息,请将 use diagnostics;
添加到脚本的顶部。在这种情况下,您会看到:
Typographical errors often show up as unique variable names. If you had a good reason for having a unique name, then just mention it again somehow to suppress the message...
由于您在初始分配后从未使用 @error_fields
,Perl 认为您可能打错了字并警告您。如果您从未对其进行任何操作,则没有理由分配给 @error_fields
,因此您只需将其从该行中删除即可:
($error) = @_;
但是该脚本还有很多其他问题(例如没有 use strict;
)。我会把它扔掉。