This will eliminate autovivification:
my $hash = undef; print "shouldn't print\n" if $hash; # still prints nothing if( ref( $hash ) =~ /HASH/ and exists( $hash->{1} ) ) { print "Autovivified\n"; # Won't print; } print "Still shouldn't print.\n";
If you don't mind $hash suddenly becoming a hashref (despite being an empty hash), you can eliminate the $ref( $hash ) =~ /HASH/ portion of the check. Do see exists.
You're just getting bitten by autovivification. It's documented behavior.
Dave
In reply to Re: Testing a key creates a hash
by davido
in thread Why does testing a key create a hash?
by johnnywang
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |