I have these two snippets of code which seemingly should produce the same result, but the latter results in an error.
1:
my $href = undef;
my @values = values %{ $href };
# OK - @values is empty
2:
my $href = undef;
my %hash = %{ $href }; # <-- Error here
my @values = values %hash;
# ERROR: Can't use an undefined value as a HASH reference
Why does having values in the same line allow it to work? I'd rather them both throw an error, since using an undefined value as a hash reference is clearly a mistake. I don't have any more recent versions of perl to test on, but this was reproducible in 5.8.8 and 5.10.1.
perldoc -f valuessays "Note that the values are not copied, which means modifying them will modify the contents of the hash." - ThisSuitIsBlackNotno autovivificationto cause an error in the first snippet. It does not. - AKHollandautovivificationsimply leaves it asundef. Use thewarnorstrictoption, e.g.no autovivification qw(fetch delete exists strict); $h = undef; values %$h;givesReference vivification forbidden. See brian d foy's article Turn off autovivification when you don’t want it - ThisSuitIsBlackNot