Hash::Util's lock_keys
Robert Rothenberg
robrwo at gmail.com
Wed Feb 19 12:06:45 GMT 2014
At $work, we're having a debate about using restricted hashes
https://metacpan.org/pod/Hash::Util#Restricted-hashes
Basically, we have code of the form
$obj->some_method( \%args );
The method can use restricted hashes to catch typos in argument keys, e.g.
sub some_method {
my ($self, $args) = @_;
lock_keys( %{$args}, qw/ type foo bar /);
...
}
so when somebody calls
$obj->some_method( { typo => '...' } );
the method dies quickly instead of doing the wrong thing. (This is
especially helpful when the devs don't notice the typos right away.)
Cons are that (1) it's additional code that has to be maintained and kept
in sync with POD, even if it's just one line; (2) it can break legacy code
that passes attributes that are no longer used; (3) unnecessary if POD
examples are kept up-to-date, as some devs prefer to copy examples and
modify them vs type in new code; (4) most of CPAN isn't using this, notably
Moo/Moose.
So, what are your opinions about this?
More information about the london.pm
mailing list