Perl incompatibility problem with each hash hash - 5.14 -> 5.8.8

The following code works fine with Active Perl 5.14.2:

my %hzones = ();
#%hzones= (
#    'zone1' =>  {
#                  'ns1' => 'ip1',
#                  'ns1' => 'ip2',
#                  },
#    'zone2' =>  {
#                  'ns1' => 'ip1',
#                  'ns2' => 'ip2'
#                  }
#);

foreach my $k1 ( keys %hzones ) {
    debug("# $k1",$d); 
    while ( my ($key, $value) = each($hzones{ $k1 }) ) { # Problem is here   217 
        while ( my ($nsname, $nsip) = each(%$value) ) { 
            debug("## $nsname , $nsip",$d);
    }
    # Creation de la zone et ajout dans infoblox
    $session->add(createZone($k1)) or error("Add zone for ".$k1." failed: ", 
        $session->status_code(). ":" .$session->status_detail());
    }
}

Now, if I try to use this code on RedHat 5.3 with Perl 5.8.8, I have the following error:

Type of arg 1 to each must be hash (not hash element) at
  ./migration-arpa.pl line 217, near "}) "
Execution of ./migration-arpa.pl aborted due to compilation
  errors.

Question . How to fix this error? How do I go through the hash table?

+5
source share
2 answers

What Quentin said, but you can try replacing

each($hzones{ $k1 })

with

each(%{$hzones{ $k1 }})

which shares the hash link.

+17
source

The opportunity to link to each( each($hzones{ $k1 })) was presented in 5.14.0. Before that, you had to go through the hash ( each(%{ $hzones{ $k1 })). This is still working, and it will continue to work.

So, to be backward compatible, use

each(%{ $hzones{ $k1 })

instead

each($hzones{ $k1 })

Note. Passing the link to is eachmarked as experimental, and I consider it a mistake, since it does not work with all hashes.

+3
source

All Articles