how to check if a hash reference is empty in perl
perl
Solution
If you'd use
%hash
for a hash, you'd use
%{ $hash }
for a reference, so it's
keys %{ $self->{href} }
Note: In some versions of Perl, `keys` accepts a reference. However, this was an experimental feature that was abandoned. One shouldn't use it.
Problem
I need to test, whether my hashref contains 0 elements. I used this code: ``` $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"; ``` It works well with perl 5.16, but fails with 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. ```