我需要測試,無論我的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