So why can't I be Lazy and do this?
Because you're trying to dereference the hash that $obj->method returns, which won't work seeing as how it's already a hash. You could just change your code so that you can dereference your return value e.g
# assuming $obj->{attr_name} holds a hashref
return $obj->{attr_name};
}
# code passes
foreach my $item (keys %{ $obj->method() }) {
My guess is that the hash gets flattened into list context by the return and somehow doesn't get coerced back into a hash by the keys function
A hash is indeed flattened to a list in list context, which is provided by your hash assignment in your second example. However keys expects a % sigil, so while you are returning a hash1 from $obj->method keys is none the wiser. So you have to enforce the sigil by either using a hash or dereferencing a hashref e.g
my %hash = qw(foo bar baz quux);
my $hashref = \%hash;
my @list = qw(ichi ni san shi);
my $lisref = \@list;
keys %hash; # valid
keys %$hashref; # valid
keys %{{@list}}; # valid
keys %{{@$listref}}; # valid
keys $hashref; # invalid
keys %{@list}; # invalid
HTH
_________ broquaint
update: added further clarification about hashes
1 you're not really returning a hash, just a list which is being stuffed into a hash |