=head1 NAME Hash::Match - match contents of a hash against rules =head1 REQUIREMENTS This module requires Perl v5.10 or newer, and the following non-core modules: =over =item L =item L =back =head1 SYNOPSIS use Hash::Match; my $m = Hash::Match->new( rules => { key => qr/ba/ } ); $m->( { key => 'foo' } ); # returns false $m->( { key => 'bar' } ); # returns true $m->( { foo => 'bar' } ); # returns false my $n = Hash::Match->new( rules => { -any => [ key => qr/ba/, key => qr/fo/, ], } ) $n->( { key => 'foo' } ); # returns true =head1 DESCRIPTION This module allows you to specify complex matching rules for the contents of a hash. =head1 METHODS =head2 C my $m = Hash::Match->new( rules => $rules ); Returns a function that matches a hash reference against the C<$rules>, e.g. if ( $m->( \%hash ) ) { ... } =head3 Rules The rules can be a hash or array reference of key-value pairs, e.g. { k_1 => 'string', # k_1 eq 'string' k_2 => qr/xyz/, # k_2 =~ qr/xyz/ k_3 => sub { ... }, # k_3 exists and sub->($hash->{k_3}) is true } For a hash reference, all keys in the rule must exist in the hash and match the criteria specified by the rules' values. For an array reference, some (any) key must exist and match the criteria specified in the rules. You can specify more complex rules using special key names: =over =item C<-all> { -all => $rules, } All of the C<$rules> must match, where C<$rules> is an array or hash reference. =item C<-any> { -any => $rules, } Any of the C<$rules> must match. =item C<-notall> { -notall => $rules, } Not all of the C<$rules> can match (i.e., at least one rule must fail). =item C<-notany> { -any => $rules, } None of the C<$rules> can match. =back Note that rules can be specified arbitrarily deep, e.g. { -any => [ -all => { ... }, -all => { ... }, ], } or { -all => [ -any => [ ... ], -any => [ ... ], ], } =head1 SEE ALSO The following modules have similar functionality: =over =item L =item L =back =head1 AUTHOR Robert Rothenberg, C<< >> =head1 ACKNOWLEDGEMENTS =over =item Foxtons, Ltd. =back =head1 LICENSE AND COPYRIGHT Copyright 2014 Robert Rothenberg. This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at: L