Hash::Util's lock_keys

Joel Bernstein joel at fysh.org
Wed Feb 19 12:16:51 GMT 2014

So you're really asking how to add method signatures that declare the
parameters expected by the method? The question about restricted hash keys
seems like a red herring in that case.

Take a look at modules like Params::Validate, MooseX::Method::Signatures
etc. In general, if you're passing complex data structures as method
arguments, perhaps consider turning those structures into objects which can
validate themselves, and passing those around instead.


On 19 February 2014 13:06, Robert Rothenberg <robrwo at gmail.com> wrote:

> 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