#!/usr/bin/env perl use strict; use warnings; my %hash = ( key1 => 'value', key2 => undef, key3 => {}, key4 => { foo => 99 }, ); foreach my $key (qw(key1 key2 key3 key4)) { my $reftype = ref ($hash{$key}); if ($reftype && $reftype eq 'HASH') { my $entries = scalar keys %{$hash{$key}}; print "\$hash{$key} is a hashref which is " . ($entries ? 'not ' : '') . "empty.\n"; } else { print "\$hash{$key} is not a hashref\n"; } }