我编写了一个代码来访问perl中的嵌套数据(非常复杂的)数据结构。
<%method searchFeatureInFG>
<%args>
$featureGroup
$featureNameHash
</%args>
<%init>
my $foundFeature = 0;
my $foundGroup = undef;
my $foundIndex = undef;
if(defined $featureGroup && defined $featureNameHash){
for(my $i = 0; $i < @$featureGroup; $i++){
#print "<pre>".Data::Dumper::Dumper($featureGroup->[$i]->{'features'})."</pre>";
if(ref($featureGroup->[$i]) eq 'HASH'){
if(defined $featureNameHash->{$featureGroup->[$i]->{'name'}}){
$foundGroup = $featureGroup;
$foundIndex = $i;
$foundFeature = 1;
}
elsif(defined $featureGroup->[$i]->{'features'}){
for(my $j = 0; $j<scalar @{$featureGroup->[$i]->{'features'}} ; $j++){
for(my $k=0;$k<scalar @{$featureGroup->[$i]->{'features'}->[$j]->{'features'}};$k++) {
if (defined $featureGroup->[$i]->{'features'}->[$j]->{'features'}->[$k]->{'name'}) {
print $featureGroup->[$i]->{'features'}->[$j]->{'features'}->[$k]->{'name'}."\n";
}
}
last if !defined $featureGroup->[$i+1]->{'features'};
}
}
}
}elsif(defined $featureNameHash->{$featureGroup->[$i]}){
$foundFeature = 1;
$foundGroup = $featureGroup;
$foundIndex = $i;
}
last if($foundFeature);
}
}
return ($foundFeature, $foundGroup, $foundIndex);
</%init>
</%method>
这给了我以下错误。
Global symbol "$i" requires explicit package name at
Global symbol "$foundFeature" requires explicit package name at
Global symbol "$foundGroup" requires explicit package name at
Global symbol "$featureGroup" requires explicit package name at
Global symbol "$foundIndex" requires explicit package name at
这些错误意味着什么?
答案 0 :(得分:2)
由于范围问题,您收到了这些错误。看看第一个
Global symbol "$i" requires explicit package name at
您最初在行
中定义$i
for(my $i = 0; $i < @$featureGroup; $i++){
但后来尝试在for循环之外使用它。 Perl给你这些错误(可能是因为你使用严格的,因为你应该使用严格),因为$i
只在for循环中定义。
要解决此问题,请将这些变量定义移到任何作用域循环之外,即在<%init>
声明之后立即
my $i
my $foundFeature
...