I’m using dynamic multilevel hashes from which I read data but also writes data.
A common pitfall for me is accessing non-existing keys (typos, db revisions etc.). I get undefs which propagate to other parts and cause problems. I would like to die whenever I try to read a non-existing key, but still be allowed to add new keys.
So the wanted behavior is:
my %hash;
$hash{A} = 5; # ok
print $hash{A}, "\n"; # ok
print $hash{X}, "\n"; # should die
$hash{B}{C}{D} = 10; # ok
print $hash{B}{C}{X}, "\n"; # should die
I previously posted a similar question and got great answers. I especially like the accepted one, which allows using the normal hash syntax. The only problem is I’m not sure how to easily generalize this to deep hashes as in the example above.
p.s.
I find this feature really useful and I wonder if I’m missing something, since it does not seem very popular. Perhaps it is not common to read/write from/to the same hash?
With
warningspragma switched on then you do getUse of uninitialized value in print at...warnings at the two lines you want to die.So if you make
warningsfatal then they would die instead:Update
Based on comments you’ve made I assume your common case issue is something along these lines:
Which won’t throw warning/error until you actually use
$xlater on.To get around this then you can do:
Unfortunately the above would mean a lot of extra typing 🙁
Here is at least a short cut:
Then below would croak!
Hopefully this and also the fatal warnings are helpful to you.
/I3az/