Perl Array References and avoiding “Type of arg 1 to keys must be hash” error

帅比萌擦擦* 提交于 2019-12-23 07:57:36

问题


I have a scalar $subscribers that could be undef, reference to a HASH, or reference to an ARRAY. I have assigned the sample values $VAR1, $VAR2 and $VAR3 for testing.

I'm only interested in $subscribers when it is a reference to an ARRAY, where by it would contain multiple values. In other cases, I'm not interested in printing anything (e.g. when $subscribers=$VAR2;

The code seems to run fine under Perl v5.16.2; however, when I move it to the target machine running Perl v5.8.8, I get a compile error:

% ./test.pl
Type of arg 1 to keys must be hash (not private variable) at ./test.pl line 23, near "$subscribers) "
Execution of ./test.pl aborted due to compilation errors.

Code below:

#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

my $VAR1 = undef;

my $VAR2 = {'msisdn' => '1234'};

my $VAR3 = [
  {'msisdn' => '1111'},
  {'msisdn' => '2222'},
  {'msisdn' => '3333'},
  {'msisdn' => '4444'},
  {'msisdn' => '5555'}
];

my @childMsisdn = ();
my $subscribers = $VAR3;

if (ref $subscribers eq ref []) { # Exclude $VAR1 && $VAR2 scenarios
  foreach my $s (keys $subscribers) {
    my $msisdn = $subscribers->[$s]->{"msisdn"};
    push (@childMsisdn, $msisdn);
  }
}
print "childMsisdn = ". join(",", @childMsisdn) ."\n";

回答1:


Replace

foreach my $s (keys $subscribers) {

with

foreach my $s (keys %$subscribers) { # $subscribers is hash ref

or

foreach my $s (0 .. $#$subscribers) { # $subscribers is array ref

From perldoc

Starting with Perl 5.14, keys can take a scalar EXPR, which must contain a reference to an unblessed hash or array. The argument will be dereferenced automatically. This aspect of keys is considered highly experimental. The exact behaviour may change in a future version of Perl.



来源:https://stackoverflow.com/questions/20824920/perl-array-references-and-avoiding-type-of-arg-1-to-keys-must-be-hash-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!