0

我已经编写了一个代码来访问 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 

这些错误是什么意思?

4

1 回答 1

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
...
于 2014-07-23T14:46:40.757 回答