Posted on 2007-04-26 22:22:09-07 by philjackson
Actions upon hit and miss
I am using Data::Domain to validate a complex (and relatively large) YAML file. Once validated I need to count the elements meaning a double pass. To do this I would love to see '-hit' and '-miss' options which take a coderef to be run upon a hit and miss respectively. Laurent reminded me of lazy constructors as a possibility but I think that this might cause the Data::Domain structure(s) to end up looking ugly. My proposal (which I've implemented, patch available if Laurent would like it) would make a very simple example look something like:
return Struct( headers => Struct( -optional => 1, header_is => List( -optional => 1, -all => String( -hit => sub { warn "got it!" }, -miss => sub { warn "missed" } ) ), ) );
The theory is already possible at the moment just with a little more hassle. Although I'm going to keep using my patched version I'd love to know what others think i.e. am I making DD do too much?
Direct Responses: 5015 | Write a response
Posted on 2007-04-30 07:11:18-07 by dami in response to 4997
Re: Actions upon hit and miss

Thanks Phil for the proposal and for going as far as providing a patch.

However, after thinking a lot about it, I came to the conclusion that it is not a good idea to include it as is in the distribution, because it would be misleading. You want a notion of "hit", meaning "something is present in the data"; but what you actually get is "at some point, a domain and a subtree were compared", which is different. For example in

List(-all => $condition1, -any => $condition2)
some initial items in the list will be inspected by both conditions and you may have 2 "hits" for the same item. On the other hand, in
Any_of($condition1, $condition2)
$condition2 will not be inspected if $condition1 was successful, so you may miss a "hit".
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.