3 ms·
I prefer to counteract these potential bugs by locking the keys of a hash. For eg. in Perl: use Hash::Util 'lock_keys'; my %hash = (key1 => 'foo', key2 =
by draegtun 13y ago
I prefer to counteract these potential bugs by locking the keys of a hash.
For eg. in Perl:
use Hash::Util 'lock_keys';
my %hash = (key1 => 'foo', key2 => 'bar', key3 => 'baz');
lock_keys %hash;
Now any attempts to lookup or amend a key that doesn't exist produces a runtime error...
$hash{key1} = "FOO"; # updates fine
$hash{key4} = "XXX"; # throws error