sub HRef { wantarray ? keys %{$_[0]} : %{$_[0]} }; my $href = { qw(a 1 b 2) }; foreach( HRef $href){ print "$_\n"; }