File Coverage

blib/lib/Params/Validate/Dependencies/all_or_none_of.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition 12 12 100.0
subroutine 8 8 100.0
pod 1 3 33.3
total 48 50 96.0


line stmt bran cond sub pod time code
1             package Params::Validate::Dependencies::all_or_none_of;
2              
3 3     3   813683 use strict;
  3         9  
  3         172  
4 3     3   18 use warnings;
  3         7  
  3         200  
5              
6 3     3   20 use base qw(Exporter Params::Validate::Dependencies::Documenter);
  3         6  
  3         1042  
7              
8 3     3   719 use vars qw($VERSION @EXPORT @EXPORT_OK);
  3         9  
  3         1111  
9              
10             $VERSION = '1.02';
11             @EXPORT_OK = @EXPORT = ('all_or_none_of');
12              
13             =head1 NAME
14              
15             Params::Validate::Dependencies::all_or_none_of - validate that either all or none of a list of params are present
16              
17             =head1 SYNOPSIS
18              
19             In this example, the 'foo' function takes named arguments, of which
20             the 'day', 'month', and 'year' args must either all be present or
21             none of them be present.
22              
23             use Params::Validate::Dependencies qw(:all);
24             use Params::Validate::Dependencies::all_or_none_of;
25              
26             sub foo {
27             validate(@_,
28             { ... normal Params::Validate stuff ...},
29             all_or_none_of(qw(day month year))
30             );
31             }
32              
33             =head1 SUBROUTINES and EXPORTS
34              
35             =head2 all_or_none_of
36              
37             This is exported by default. It takes a list of scalars and code-refs
38             and returns a code-ref which checks that the hashref it receives matches
39             either all or none of the options given.
40              
41             =cut
42              
43             sub all_or_none_of {
44 3     3 1 566048 my @options = @_;
45             return bless sub {
46 32     32   8531 my $hashref = shift;
47 32 100       106 if($Params::Validate::Dependencies::DOC) {
48 10         39 return $Params::Validate::Dependencies::DOC->_doc_me(list => \@options);
49             }
50 22         45 my $count = 0;
51 22         49 foreach my $option (@options) {
52             $count++ if(
53 60 100 100     295 (!ref($option) && exists($hashref->{$option})) ||
      100        
      100        
54             (ref($option) && $option->($hashref))
55             );
56             }
57 22   100     285 return ($count == 0 || $count == $#options + 1);
58 3         71 }, __PACKAGE__;
59             }
60              
61 10     10 0 449 sub join_with { return 'and'; }
62 11     11 0 234 sub name { return 'all_or_none_of'; }
63              
64             =head1 LIES
65              
66             Some of the above is incorrect. If you really want to know what's
67             going on, look at L.
68              
69             =head1 BUGS, LIMITATIONS, and FEEDBACK
70              
71             I like to know who's using my code. All comments, including constructive
72             criticism, are welcome.
73              
74             Please report any bugs either by email
75             or at L.
76              
77             Bug reports should contain enough detail that I can replicate the
78             problem and write a test. The best bug reports have those details
79             in the form of a .t file. If you also include a patch I will love
80             you for ever.
81              
82             =head1 SEE ALSO
83              
84             L
85              
86             L
87              
88             =head1 SOURCE CODE REPOSITORY
89              
90             L
91              
92             L
93              
94             =head1 COPYRIGHT and LICENCE
95              
96             Copyright 2024 David Cantrell EFE
97              
98             This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
99              
100             =head1 CONSPIRACY
101              
102             This module is also free-as-in-mason.
103              
104             =cut
105              
106             1;