Perl错误全局符号需要明确的包名



我在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 

由于范围问题,您会得到这些错误。看看第一个

Global symbol "$i" requires explicit package name at

您最初在

行中定义$i
  for(my $i = 0; $i < @$featureGroup; $i++){

但是稍后尝试在for循环之外使用它。Perl会给你这些错误(可能是因为你应该使用strict),因为$i只在for循环中定义。

要解决这个问题,将这些变量定义移到任何作用域循环之外,即立即在<%init>声明

之后
my $i
my $foundFeature
...

相关内容

  • 没有找到相关文章

最新更新