全局符号“%formsequence”在第37行需要明确的软件包名称 [英] Global symbol "%formsequence" requires explicit package name at line 37
问题描述
我正在尝试执行Perl CGI脚本,但是出现错误:
I am trying to execute a Perl CGI script, but I am getting an error:
Global symbol "%formsequence" requires explicit package name at line 37.
我做了一些研究,发现 use strict
迫使我在使用变量或存储任何数据之前先声明变量,但是在程序中我已经声明了变量,这就是为什么我不理解错误的原因。这是我的脚本:
I did some research and found that use strict
forces me to declare the variables before I use them or store any data, but in my program I have declared them and that is why I don't understand the error. Here is my script:
#!/usr/bin/perl -w
use strict;
my %errors;
my %form;
my @formsequence;
my %fields = (
"lname" => "Last Name",
"phone" => "Phone",
"fname" => "Fist Name"
);
my %patterns = (
"lname" => '[A-Z][a-z]{2,50}',
"phone" => '\d{3}-\d{3}-\d{4}',
"fname" => '[A-Z][A-Za-z]{2,60}'
);
@formsequence = ("lname", "phone", "phone");
print "content-type/html\n\n";
if ($ENV{REQUEST_METHOD} eq "POST") {
&readformdata;
if (&checkrequiredfields) {
print "Form Data validated successfully!";
}
else {
foreach (keys (%fields)) {
if ($fields{$_} != $formsequence{$_}) { <-- line 37
$errors{$_}="Not in correct sequence\n";
}
}
}
推荐答案
我怀疑您可能是从PHP开发人员的角度看待数组的概念。在Perl中,哈希和数组是单独的数据结构。
I suspect you may be viewing the concept of an 'array' from the perspective of a PHP developer. In Perl a hash and an array are separate data structures.
数组使用 @
前缀声明,您可以引用
Arrays are declared using the @
prefix and you refer to elements using square brackets around an integer index:
my @names = ('Tom', 'Dick', 'Larry');
say $names[0]; # 'Tom'
say $names[-1]; # 'Larry'
my $count = @names; # $count now equals 3
foreach my $i (0..$#names) {
say $names[$i];
}
使用%$ c声明哈希$ c>前缀,您可以在字符串键周围使用花括号来引用元素:
Hashes are declared using the %
prefix and you refer to elements using curly braces around a string key:
my %rgb = (
red => '#ff0000',
white => '#ffffff',
blue => '#0000ff',
);
say $rgb{'red'}; # '#ff0000'
say $rgb{blue}; # '#0000ff' quotes optional around bareword keys
foreach my $k (keys %rgb) {
say $rgb{$k};
}
通常您不会使用键
在数组上的功能-实际上,旧版本的Perl甚至不支持它,新版本将返回一定范围的整数(例如: 0..2
)。
You wouldn't normally use the keys
function on an array - in fact older versions of Perl don't even support it, newer versions will return a range of integers (e.g.: 0..2
).
在散列上调用键
时,键没有固有顺序,并且顺序可能会更改
When you call keys
on a hash the keys have no inherent order, and the order may change.
其他值得注意的事情:
使用&
调用函数是真正的旧样式(即90年代初),这些天我们将使用 readformdata()
而不是& readformdata
。
Using &
to call a function is really old style (i.e. early 90s), these days we'd use readformdata()
instead of &readformdata
.
!=
运算符是 numeric 比较运算符,因此仅在您要比较的值实际上是数字时才使用它。如果要检查两个字符串是否相等,则使用 ne
代替(例如: if($ thing1 ne $ thing2){... }
)。
The !=
operator is a numeric comparison operator so only use it when the values you're comparing are actually numbers. If you want to check two strings are 'not equal' then use ne
instead (e.g.: if($thing1 ne $thing2) { ... }
).
这篇关于全局符号“%formsequence”在第37行需要明确的软件包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!