user3642121
user3642121

Reputation: 13

Trouble with Perl 5.10.1 error Type of arg 1 to each must be hash

I am at my wits end on this one. I have to use a older version of Perl since our Linux standard continues to be RHEL 6. So I have Perl 5.10.1. I developed my script in 5.14 and it works great. Moving to RHEL and older Perl it barfs.

Here is the DataDump of the information brought back as $response, followed by the code used in 5.14 and the things I have tried in 5.10.1. Any help would be appreciated.

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 Version 5.14 (WORKING)

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 (DOES NOT WORK)

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

errors out with error: Type of arg 1 to each must be hash (not hash element) at

Changing to:

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

Allows it to run, but stops with Not a HASH reference at

Upvotes: 1

Views: 199

Answers (1)

ikegami
ikegami

Reputation: 385764

$response->{'response'}{'scanners'} contains a reference to an array, so your code is equivalent to the following:

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

However, each didn't work on arrays in 5.10. The following is equivalent:

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

Since you don't actually use $key, you could also use the following:

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

Note: I recommend against using each($ref). Aside from the backwards-compatibility issues, it's experimental, and it doesn't work with some special hashes and arrays.

Upvotes: 4

Related Questions