OptionHash - Checking of option hashes
version 0.2.0
use OptionHash; my $cat_def = ohash_define( keys => [qw< tail nose claws teeth>]); sub cat{ my %options = @_; ohash_check( $cat_def, \%options); # ... } cat( teeth => 'sharp' ); cat( trunk => 'long'); # Boom, will fail. Cats dont expect to have a trunk.
I like to pass options around in hash form because it's clear and flexible. However it can lead to sloppy mistakes when you typo the keys. OptionHash quickly checks your hashes against a definition and croaks if you've passed in bad keys.
Currently.. That's it! Simple but effective.
Define an optionhash specification, sort-of a type:
my $cat_def = ohash_define( keys => [ qw< teeth claws > ]);
Check a hash against a definition:
ohash_check( $cat_def, \%options);
If everything is okay things will proceed, otherwise ohash_check will croak (see Carp).
Maybe do the checking part with XS, although honestly it's fast in perl. Might do an extra module as it's nice to have a pure perl version anyway.
Also other checks, like mandatory keys.
Joe Higton <draxil@gmail.com>
This software is copyright (c) 2017 by Joe Higton <draxil@gmail.com>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install OptionHash, copy and paste the appropriate command in to your terminal.
cpanm
cpanm OptionHash
CPAN shell
perl -MCPAN -e shell install OptionHash
For more information on module installation, please visit the detailed CPAN module installation guide.