旧的Perl脚本现在有错误 - :名称只使用一次

时间:2016-12-07 17:45:59

标签: perl cgi

我继承了一个旧的perl脚本,它从各种形式中获取搜索词,然后处理它们。

我现在在日志中看到脚本抛出以下错误: mastersearch.cgi:名称" main :: error_fields"仅使用一次:/var/www/cgi-bin/mastersearch.cgi第203行可能错误。

我已经指出了下面的错误行,在这里它已脱离了上下文:

 ($error,@error_fields) = @_;

我只读过一次变量,所以看起来这行,数组错误字段只使用一次。但为什么不行呢?

我可以使用strict来纠正这个问题吗?我已经阅读了其他堆栈问题,但没看到它们之间的关系,特别是:

  

"仅使用一次"当autodie或Fatal是时,可以生成警告   与包文件句柄一起使用(例如,FILE)。标量文件句柄是   强烈推荐。

我的问题是:

  • 我怎样才能摆脱错误,我不是一个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($1))/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;
}

1 个答案:

答案 0 :(得分:3)

首先,这是一个警告,而不是错误。它不会停止脚本的执行,它只是试图提醒您某些可能错误,并且您应该看一下它。

要获取有关警告的更多详细信息,请将use diagnostics;添加到脚本的顶部。在这种情况下,您将看到:

  

印刷错误通常显示为唯一的变量名称。       如果你有充分的理由拥有一个独特的名字,那就提一下吧       再次以某种方式压制消息...

由于您在初始分配后从不使用@error_fields,因此Perl认为您可能会输入错字并向您发出警告。如果您从未对其进行任何操作,则没有理由分配给@error_fields,因此您只需将其从该行中删除即可:

($error) = @_;

但该脚本还有很多其他问题(例如没有use strict;)。我会抛弃它。