in reply to Preventing unintended list expansion inside hash literals.
%$hash_ref or return; line is redundant, it doesn't guard from not a hashref being passed to sub, and each returns undef in scalar context on empty hash, anyway.
If sub is guaranteed a hashref as an argument, then iterator could be reset BEFORE using each, and, not sure if "idiomatic", but definitely shorter sub can be
sub some_key { keys %{$_[0]} ? scalar each %{$_[0]} : undef }
p.s. Shorter:
sub some_key { keys %{$_[0]}; scalar each %{$_[0]} }
|
|---|