The exists
function can unexpectedly autovivify entries in hashes.
What surprises me is that this behavior carries over to constants as well:
use strict;
use warnings;
use Data::Dump 'dump';
use constant data => {
'foo' => {
'bar' => 'baz',
},
'a' => {
'b' => 'c',
}
};
dump data; # Pre-modified
print "No data for 'soda->cola->pop'\n" unless exists data->{soda}{cola}{pop};
dump data; # data->{soda}{cola} now sprung to life
Output
{ a => { b => "c" }, foo => { bar => "baz" } } No data for 'soda->cola->pop' { a => { b => "c" }, foo => { bar => "baz" }, soda => { cola => {} } }
I suspect this is a bug. Is this something 5.10.1-specific, or do other versions of Perl behave similarly?