Subject: | document (or fix?) CAVEAT: hash autovivification |
Hi there!
I found something of a caveat while using Const::Fast. Hash
autovivification in Perl creates keys when testing definedness. So if
you have a constant hash you will *not* be able to check if a value is
defined unless you make sure it exists first. Otherwise you'll get this:
$ perl -MConst::Fast -Mstrict -Mwarnings -E 'const my %a => (foo => 1,
bar => 2); say 1 unless $a{baz}'
Attempt to access disallowed key 'baz' in a restricted hash at -e line 1.
Unless there's a way to suppress this warning with Const::Fast, those
checks on constant hashes should probably use 'exists' instead:
$ perl -MConst::Fast -Mstrict -Mwarnings -E 'const my %a => (foo => 1,
bar => 2); say 1 unless exists $a{baz}'
1
If it's not possible (or not desired) to remove that message, I really
think the 'defined x exists' issue in constant hashes should be
mentioned under the CAVEATS section of the documentation.
Thanks for such a nice module, btw :)