我需要测试,无论我的hashref是否包含0个元素。 我用这个代码:如何检查散列引用在perl中是否为空
$self = { fld => 1 };
%h = ("a" => "b");
$self->{href} = { %h };
print STDERR $self->{href}{ "a" };
print STDERR "\n";
print "size of hash: " . keys(%h) . ".\n";
print "size of hashref: " . keys($self->{href}) . ".\n";
它用Perl 5.16效果很好,但无法用Perl 5.10:
Type of arg 1 to keys must be hash (not hash element) at - line 7, near "}) "
Execution of - aborted due to compilation errors.
如果_ref_是不是哈希有可能做到这一点'如果(键$自我! - > { '的href'}){}'中的if语句。在所有Perl版本中工作是否一样? – 2015-02-28 18:37:29
不是。那是一个放弃的实验性功能。 – ikegami 2016-11-08 20:15:25
这是否返回空或0? – landesko 2017-08-28 15:19:57