Yes, but that's not what I said. I didn't say a Seahorse hash, I said Seahorse *entry*: Whoops! I certainly did!
$ cat autovivify.pl #!/usr/bin/perl use strict; use warnings; use Data::Dump qw(dump); my %h = (A=>0); print "Foo" if defined $h{Apple}{Banana}{Seahorse}; $h{Apple}{Banana}{Seahorse}=undef; print "Bar" if defined $h{Apple}{Banana}{Seahorse}; print dump(\%h); $ perl autovivify.pl { A => 0, Apple => { Banana => { Seahorse => undef } } }
As you can see, if the Seahorse entry exists defined can still return false.
In my (clearly incorrect) mental model of hashes, the defined function and the perl compiler, defined would be passed the contents of the *value* slot of the Seahorse entry. So I expected that the Seahorse entry would be created so that the compiler could hand defined the undefined value. This is one of perl's irksome (to me) quirks.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
Update: Repaired, per tobyink's note.
In reply to Re^3: gotchas with hash element autovivification
by roboticus
in thread gotchas with hash element autovivification
by raybies
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |