Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: How best to validate the keys of hashref arguments?by haukex (Archbishop) |
on Mar 16, 2017 at 09:39 UTC ( [id://1184851]=note: print w/replies, xml ) | Need Help?? |
If I just want to check that no unknown args are passed, I'll sometimes write something like this:
Sometimes, to prevent typos, I'll use restricted hashes via Hash::Util:
However, both of these solutions only cover validating the keys, not the values of the hashes. For more advanced validation, I'd suggest Params::Validate. Or, as you suggested, you can make a simple object, for this I'd recommend Moo over Moose, as the former is (with a few minor exceptions) a subset of the latter, and is more lightweight. You can implement constraints on the values manually using coderefs, or with something like Type::Tiny.
In Section
Seekers of Perl Wisdom
|
|