Perl使用referrer收到错误

时间:2013-10-01 19:31:15

标签: html perl

my $refer = $ENV{HTTP_REFERER};
 my $gk1 = substr($str1, -4); 
if ($gk1 = .swf) { my $antigk = "gk detected"; } 
else 
{ my $antigk = $link; }

这段代码有什么问题?编辑后我得到一些错误:|

是我的第一个perl代码所以:|我不知道我做错了什么:|

可能是因为那个

my $antigk

THIS is my new error:
Software error:

Global symbol "$str1" requires explicit package name at index_dl.pm line 680.
syntax error at index_dl.pm line 682, near "= ."
syntax error at index_dl.pm line 683, near "else"
Global symbol "$antigk" requires explicit package name at index_dl.pm line 684.
Global symbol "$direct_link" requires explicit package name at index_dl.pm line 684.
syntax error at index_dl.pm line 727, near "}"
Can't use global @_ in "my" at index_dl.pm line 731, near "= @_"
syntax error at index_dl.pm line 735, near "}"
syntax error at index_dl.pm line 761, near "}"
syntax error at index_dl.pm line 779, near "}"
index_dl.pm has too many errors.
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

Status: 500 Content-type: text/html
Software error:

[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: Global symbol "$str1" requires explicit package name at index_dl.pm line 680.
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: syntax error at index_dl.pm line 682, near "= ."
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: syntax error at index_dl.pm line 683, near "else"
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: Global symbol "$antigk" requires explicit package name at index_dl.pm line 684.
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: Global symbol "$direct_link" requires explicit package name at index_dl.pm line 684.
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: syntax error at index_dl.pm line 727, near "}"
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: Can't use global @_ in "my" at index_dl.pm line 731, near "= @_"
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: syntax error at index_dl.pm line 735, near "}"
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: syntax error at index_dl.pm line 761, near "}"
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: syntax error at index_dl.pm line 779, near "}"
[Wed Oct  2 20:40:40 2013] fast_index_dl.cgi: index_dl.pm has too many errors.
Compilation failed in require at ./fast_index_dl.cgi line 53.
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

3 个答案:

答案 0 :(得分:1)

您可能在index_dl.pm文件的顶部附近:

use strict;

这意味着您需要在使用my之前声明每个变量:

my $str1;

您可能想在if-else块之前声明$antigk,也许您真的想要:

my $antigk = $link;
if ($gk1 eq '.swf') { $antigk = "gk detected"; } 
else 
{ $antigk = $link; }

答案 1 :(得分:1)

# In this line you store the HTTP referer in the variable $refer
my $refer = $ENV{HTTP_REFERER};

# Then, in this link you take a substring of the variable $str1.
# But you don't have a variable called $str1.
# Perhaps you actually wanted to take a substring of $refer?
my $gk1 = substr($str1, -4); 

# This next line will be a syntax error because you need to quote
# strings (and .swf is a string. This should be '.swf'.
# Also, you are using '='. This is the assignment operator. This
# will set $pgk1 to the value '.swf'. That's not what you want to do.
# You want to compare $pgk1 and '.swf'. For that you need the comparison
# operator - which is 'eq'.
# So this whole line should be:
# if ($gk1 eq '.swf') {
if ($gk1 = .swf) {
  # It's good that you are declaring $antigk using 'my'. But 'my' only
  # works within a code block. So you're setting $antigk here, but the
  # value will be lost as soon as you pass the end of the code block
  # (which is the '}' on the very next line).
  # Better to declare $antigk (with 'my $antigk') before the 'if' statement.
  my $antigk = "gk detected";
} else {
  my $antigk = $link;
}

通常,您在此处制作了一些非常基本的编程错误。我认为你认为你通过跳入问题和Google搜索帮助来学习最好。我不认为这种方法对你有用。在继续这个项目之前,我强烈建议您花几天时间来学习一本初学Perl书(比如“学习Perl”)。

答案 2 :(得分:0)

除了工具的答案之外,我想你必须改变这些界限:

my $refer = $ENV{HTTP_REFERER};
my $gk1 = substr($str1, -4); 

这些:

my $refer = $ENV{HTTP_REFERER};
my $gk1   = substr($refer, -4); 
#                  ^^^^^^