我需要测试我的 hashref 是否包含 0 个元素。我使用了这段代码:
$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.