Perl 5.10.1错误的麻烦每个arg 1的类型必须是散列

时间:2014-05-15 18:44:54

标签: perl hash

我在这个问题上的斗智尽头。我必须使用旧版本的Perl,因为我们的Linux标准仍然是RHEL 6.所以我有Perl 5.10.1。我在5.14开发了我的脚本,效果很好。移动到RHEL和更旧的Perl barfs。

以下是作为$ response返回的信息的DataDump,其次是5.14中使用的代码以及我在5.10.1中尝试过的内容。任何帮助将不胜感激。

response   => {
                  md5 => {
                    organizations => "b157f81f9469e88fd1ac2435559f558e",
                    scanners => "40e782276cc521ef799cc111a9472cf4",
                    zones => "a41b4d543756320418fce473d97a3b8d",
                  },
                  organizations => [
                    { description => "", id => 1, name => "Our Corporation" },
                  ],
                  scanners => [
                    { description => "", id => 5, name => "BR549-A", status => 1 },
                    {
                      description => "Test VM Scanner in BFE",
                      id => 16,
                      name => "BFENessus01",
                      status => 1,
                    },
                    {
                      description => "Our other Nessus Scanner VM",
                      id => 17,
                      name => "OHTHERNESSUS01",
                      status => 1,
                    },
                    { description => "", id => 49, name => "NYCNESSUS02", status => 1 },
                    { description => "", id => 50, name => "LAX1NESSUS03", status => 1 },
                    { description => "", id => 51, name => "LAX1NESSUS04", status => 1 },
                    { description => "", id => 52, name => "LAX1NESSUS05", status => 1 },
                    { description => "", id => 54, name => "MK-NESSUS", status => 1 },
                    {
                      description => "Networking team's scanner",
                      id => 55,
                      name => "NETEAMNESSUS06",
                      status => 1,
                    },
                },
  timestamp  => 1400177639,
  type       => "regular",
  warnings   => [],
}                   

PERL版本5.14(工作)

while (($key, $value) = each($response->{'response'}{'scanners'} )){
          switch ($value->{'status'}){
          case 1 {print "Status OK \t\t\t"}
          case 2 {print " !! CLOSED !! \t\t\t"}
          case 4 {print " !! TIMEOUT !! \t\t\t"}
          case 16384 {print " ** DISABLED **\t\t\t"}
          case 1024  {print " Updating Plug-Ins \t\t\t"}
          case 1025  {print " ** Updating Plug-Ins **\t\t"}
          case 1281  {print " Attempting to Update\t\t"}
          case 256  {print " !! Out of Date !! \t\t\t"}
          case 257  {print "Plug-Ins Out of Sync !! \t\t"}
          else { print "Status BAD ($value->{'status'}) \t\t"}

          }

          print "$value->{'name'}  $value->{'description'} \n";
}

PERL VERSION 5.10.1(不工作)

while (($key, $value) = each($response->{'response'}{'scanners'} )){

错误输出错误:每个arg 1的类型必须是

的哈希(而不是哈希元素)

更改为:

while (($key, $value) = each(%{$response->{'response'}{'scanners'}} )){

允许它运行,但在

处停止使用非HASH参考

1 个答案:

答案 0 :(得分:4)

$response->{'response'}{'scanners'}包含对数组的引用,因此您的代码等效于以下内容:

while (my ($key, $value) = each(@{ $response->{response}{scanners} })) {
   ...
}

但是,each对5.10中的数组无效。以下是等效的:

for my $key (0..$#{ $response->{response}{scanners} }) {
   my $value = $response->{response}{scanners}[$key];
   ...
}

由于您实际上并未使用$key,因此您还可以使用以下内容:

for my $value (@{ $response->{response}{scanners} }) {
   ...
}

注意:我建议不要使用each($ref)。除了向后兼容性问题,它是实验性的,并且它不适用于某些特殊的哈希和数组。