Brick/Composers.pm

filter: Statementstmt 37.7% 40 / 106 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 48.2% 14 / 29 Podpod n/a totaltotal 28.6% 55 / 192 CC 25 SCAR 33.9
CC
25
Cov
27%
__ANON__
90.0
__ANON__
54.7
__ANON__
42.0
CRAP 29.6
« Brick/Result.pm ↑ Summary Brick/Dates.pm »
1 package Brick::Composers;
2 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 $VERSION = '0.904';
6  
7 5 ▶use Brick::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Brick::Bucket;
10 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Brick::Composers - This is the description
19  
20 =head1 SYNOPSIS
21  
22 use Brick::Constraints::Bucket;
23  
24 =head1 DESCRIPTION
25  
26 This module defines composing functions in the
27 Brick::Constraints package. Each function takes a list of code
28 refs and returns a single code ref that wraps all of them. The single
29 code ref returns true or false (but defined), as with other
30 constraints.
31  
32 If a composer cannot create the single code ref (for instance, due to
33 bad input) it returns C<undef> of the empty list, indicating a failure
34 in programming rather than a failure of the data to validate.
35  
36 =cut
37  
38 =over 4
39  
40 =item __and( LIST OF CODEREFS )
41  
42 =item __compose_satisfy_all( LIST OF CODEREFS )
43  
44 This is AND with NO short-circuiting.
45  
46 ( A && B && C )
47  
48 This function creates a new constraint that returns true if all of its
49 constraints return true. All constraints are checked so there is no
50 short-circuiting. This allows you to get back all of the errors at
51 once.
52  
53 =cut
54  
55 sub __compose_satisfy_all {
56 6 ▶ my $bucket = shift;
Subroutine __compose_satisfy_all: called CC=1 SCAR=0.0
57 6 $bucket->__compose_satisfy_N( scalar @_, @_ );
58 }
59  
60 BEGIN {
61 5 ▶*__and = *__compose_satisfy_all;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 }
63  
64 =item __or( LIST OF CODEREFS )
65  
66 =item __compose_satisfy_any( LIST OF CODEREFS )
67  
68 This is OR but with NO short-circuiting.
69  
70 ( A || B || C )
71  
72 This function creates a new constraint that returns true if all of its
73 constraints return true. All constraints are checked so there is no
74 short-circuiting.
75  
76 =cut
77  
78 sub __compose_satisfy_any {
79 0 ▶ my $bucket = shift;
Subroutine __compose_satisfy_any: not called CC=1 SCAR=6.9
80 0 $bucket->__compose_satisfy_N_to_M( 1, scalar @_, @_ );
81 }
82  
83 BEGIN {
84 5 ▶*__or = *__compose_satisfy_any;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 }
86  
87 =item __none( LIST OF CODEREFS )
88  
89 =item __compose_satisfy_none( LIST OF CODEREFS )
90  
91  
92 ( NOT A && NOT B && NOT C )
93  
94 NOT ( A || B || C )
95  
96 This function creates a new constraint that returns true if all of its
97 constraints return false. All constraints are checked so there is no
98 short-circuiting.
99  
100 =cut
101  
102 sub __compose_satisfy_none {
103 0 ▶ my $bucket = shift;
Subroutine __compose_satisfy_none: not called CC=1 SCAR=6.9
104 0 $bucket->__compose_satisfy_N_to_M( 0, 0, @_ );
105 }
106  
107 BEGIN {
108 5 ▶*__none = *__compose_satisfy_none;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 }
110  
111 =item __compose_satisfy_N( SCALAR, LIST OF CODEREFS )
112  
113 This function creates a new constraint that returns true if exactly N
114 of its constraints return true. All constraints are checked so there
115 is no short-circuiting.
116  
117 =cut
118  
119 sub __compose_satisfy_N {
120 6 ▶ my( $bucket, $n, @subs ) = @_;
Subroutine __compose_satisfy_N: called CC=1 SCAR=0.0
121  
122 6 $bucket->__compose_satisfy_N_to_M( $n, $n, @subs );
123 }
124  
125 =item __compose_satisfy_N_to_M( LIST OF CODEREFS )
126  
127 This function creates a new constraint that returns true if between N
128 and M (inclusive) of its constraints return true. All constraints are
129 checked so there is no short-circuiting.
130  
131 =cut
132  
133 sub __compose_satisfy_N_to_M {
134 6 ▶ my( $bucket, $n, $m, @subs ) = @_;
Subroutine __compose_satisfy_N_to_M: called CC=2 SCAR=12.5
135  
136 6 ▶ if( grep { ref $_ ne ref sub {} } @subs )
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_ ne ref sub { } ;} @subs) 0 6 6
137 {
138 0 croak "Got something else when expecting code ref!";
139 0 ▶ return sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 }
141  
142 6 my @caller = $bucket->__caller_chain_as_list();
143  
144 6 my @composers = grep { /^__compose/ } map { $_->{sub} } @caller;
145  
146 6 my $max = @subs;
147  
148 my $sub = $bucket->add_to_bucket( {
149 name => $composers[-1], # forget the chain of composers
150 code => sub {
151 0 ▶ my $count = 0;
Subroutine __ANON__: not called CC=9 SCAR=45.0
152 0 my @dies = ();
153 0 foreach my $sub ( @subs )
154 {
155 0 my $result = eval { $sub->( @_ ) };
156 0 my $at = $@;
157 0 ▶ $count++ unless $at;
Branch: 1 branch
BranchTrueFalseTotal
unless $at 0 0 0
158 #print STDERR "\n!!!!Sub died!!!!\n" if ref $at;
159 #print STDERR "\n", Data::Dumper->Dump( [$at], [qw(at)]) if ref $at;
160 0 ▶ die if( ! ref $at and $at );
Branch: 1 branch
BranchTrueFalseTotal
if not ref $at and $at 0 0 0
Condition0%0/3
not ref $at and $at
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $at and $at
not ref $at $at
Truth table0%0/3
not(A) and B
A= not ref $atB= $at
ABresult
0X0
100
111
161 0 ▶ push @dies, $at if ref $at;
Branch: 1 branch
BranchTrueFalseTotal
if ref $at 0 0 0
162 };
163  
164 0 ▶ my $range = $n == $m ? "exactly $n" : "between $n and $m";
Branch: 1 branch
BranchTrueFalseTotal
$n == $m ? : 0 0 0
165  
166 die {
167 message => "Satisfied $count of $max sub-conditions, needed to satisfy $range",
168 0 ▶ handler => $caller[0]{'sub'},
Branch: 1 branch
BranchTrueFalseTotal
unless $n <= $count and $count <= $m 0 0 0
Condition0%0/3
$n <= $count and $count <= $m
!ll&&!rl&&r
000
MC/DC0%0/2
$n <= $count and $count <= $m
$n <= $count $count <= $m
Truth table0%0/3
A and B
A= $n <= $countB= $count <= $m
ABresult
0X0
100
111
169 errors => \@dies,
170 } unless $n <= $count and $count <= $m;
171  
172 0 return 1;
173 },
174 6 });
175  
176 6 $bucket->comprise( $sub, @subs );
177  
178 6 return $sub;
179 }
180  
181 =item __not( CODEREF )
182  
183 =item __compose_not( CODEREF )
184  
185 This composers negates the sense of the code ref. If the code ref returns
186 true, this composer makes it false, and vice versa.
187  
188 =cut
189  
190  
191 sub __compose_not {
192 0 ▶ my( $bucket, $not_sub ) = @_;
Subroutine __compose_not: not called CC=1 SCAR=6.9
193  
194 my $sub = $bucket->add_to_bucket( {
195 0 ▶ code => sub { if( $not_sub->( @_ ) ) { die {} } else { return 1 } },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (&$not_sub(@_)) { } 0 0 0
196 0 } );
197  
198 0 return $sub;
199 }
200  
201  
202 =item __compose_until_pass
203  
204 =item __compose_pass_or_skip
205  
206 Go through the list of closures, trying each one until one suceeds. Once
207 something succeeds, it returns the name of the subroutine that passed.
208  
209 If
210 a closure doesn't die, but doesn't return true, this doesn't fail but
211 just moves on. Return true for the first one that passes,
212 short-circuited the rest.
213  
214 If none of the closures pass (and none of them die), return 0. This might
215 be the odd case of a several selectors (see L<Brick::Selector>), none of
216 which pass.
217  
218 If one of the subs dies, this composer still dies. This can also die
219 for programming (not logic) errors.
220  
221 =cut
222  
223 sub __compose_pass_or_skip {
224 0 ▶ my( $bucket, @subs ) = @_;
Subroutine __compose_pass_or_skip: not called CC=2 SCAR=17.9
225  
226 0 ▶ if( grep { ref $_ ne ref sub {} } @subs ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_ ne ref sub { } ;} @subs) 0 0 0
227 0 croak "Got something else when expecting code ref!";
228 0 ▶ return sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 }
230  
231 0 my @caller = $bucket->__caller_chain_as_list();
232  
233 my $sub = $bucket->add_to_bucket( {
234 code => sub {
235 0 ▶ my $count = 0;
Subroutine __ANON__: not called CC=6 SCAR=37.4
236 0 my @dies = ();
237  
238 0 foreach my $sub ( @subs )
239 {
240 0 my $result = eval { $sub->( @_ ) };
241 0 my $eval_error = $@;
242  
243 # all true values are success
244 0 ▶ return "$sub" if $result; # we know we passed
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
245  
246  
247 # we're a selector: failed with no error
248 0 ▶ return if ( ! defined $result and ! defined $eval_error );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result or defined $eval_error 0 0 0
Condition0%0/3
defined $result or defined $eval_error
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $result or defined $eval_error
defined $result defined $eval_error
Truth table0%0/3
A or B
A= defined $resultB= defined $eval_error
ABresult
1X1
011
000
249  
250 # die for everything else - validation error
251 0 ▶ die if( ref $eval_error );
Branch: 1 branch
BranchTrueFalseTotal
if ref $eval_error 0 0 0
252 };
253  
254 0 return 0;
255 },
256 0 });
257  
258 0 $bucket->comprise( $sub, @subs );
259  
260 0 return $sub;
261 }
262  
263 BEGIN {
264 5 ▶*__compose_until_pass = *__compose_pass_or_skip;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 }
266  
267 =item __compose_until_fail
268  
269 =item __compose_pass_or_stop
270  
271 Keep going as long as the closures return true.
272  
273 The closure that returns undef is a selector.
274  
275 If a closure doesn't die and doesn't don't fail, just move on. Return true for
276 the first one that passes, short-circuited the rest. If none of the
277 closures pass, die with an error noting that nothing passed.
278  
279 This can still die for programming (not logic) errors.
280  
281  
282 $result $@ what action
283 ------------------------------------------------------------
284 1 undef passed go on to next brick
285  
286 undef undef selector stop, return undef, no die
287 failed
288  
289 undef string program stop, die with string
290 error
291  
292 undef ref validator stop, die with ref
293 failed
294  
295 =cut
296  
297 sub __compose_pass_or_stop {
298 0 ▶ my( $bucket, @subs ) = @_;
Subroutine __compose_pass_or_stop: not called CC=2 SCAR=16.5
299  
300 0 ▶ if( grep { ref $_ ne ref sub {} } @subs )
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_ ne ref sub { } ;} @subs) 0 0 0
301 {
302 0 croak "Got something else when expecting code ref!";
303 0 ▶ return sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
304 }
305  
306 0 my @caller = $bucket->__caller_chain_as_list();
307  
308 0 my $max = @subs;
309  
310 my $sub = $bucket->add_to_bucket( {
311 code => sub {
312 0 ▶ my $count = 0;
Subroutine __ANON__: not called CC=8 SCAR=40.0
313 0 my @dies = ();
314  
315 0 my $last_result;
316 0 foreach my $sub ( @subs )
317 {
318 5 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
319 0 my $result = eval { $sub->( @_ ) };
320 0 my $at = $@;
321 #print STDERR "\tstop: Returned result: $result\n";
322 #print STDERR "\tstop: Returned undef!\n" unless defined $result;
323 #print STDERR "\tstop: Returned ref!\n" if ref $at;
324 0 $last_result = $result;
325  
326 0 ▶ next if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 0 0
327  
328 0 ▶ die $at if ref $at;
Branch: 1 branch
BranchTrueFalseTotal
if ref $at 0 0 0
329  
330 0 ▶ return unless( defined $result and ref $at );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result and ref $at 0 0 0
Condition0%0/3
defined $result and ref $at
!ll&&!rl&&r
000
MC/DC0%0/2
defined $result and ref $at
defined $result ref $at
Truth table0%0/3
A and B
A= defined $resultB= ref $at
ABresult
0X0
100
111
331  
332 0 ▶ die if( ref $at and $at ); # die for program errors
Branch: 1 branch
BranchTrueFalseTotal
if ref $at and $at 0 0 0
Condition0%0/3
ref $at and $at
!ll&&!rl&&r
000
MC/DC0%0/2
ref $at and $at
ref $at $at
Truth table0%0/3
A and B
A= ref $atB= $at
ABresult
0X0
100
111
333 #print STDERR "\tStill going\n";
334 };
335  
336 0 return $last_result;
337 },
338 0 });
339  
340 0 $bucket->comprise( $sub, @subs );
341  
342 0 return $sub;
343 }
344  
345 BEGIN {
346 5 ▶*__compose_until_fail = *__compose_pass_or_stop;
Subroutine BEGIN: called CC=1 SCAR=0.0
347 }
348  
349 =back
350  
351 =head1 TO DO
352  
353 TBA
354  
355 =head1 SEE ALSO
356  
357 TBA
358  
359 =head1 SOURCE AVAILABILITY
360  
361 This source is in Github:
362  
363 https://github.com/briandfoy/brick
364  
365 =head1 AUTHOR
366  
367 brian d foy, C<< <briandfoy@pobox.com> >>
368  
369 =head1 COPYRIGHT
370  
371 Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
372  
373 You may redistribute this under the terms of the Artistic License 2.0.
374  
375 =cut
376  
377 5 1;
« Brick/Result.pm ↑ Summary Brick/Dates.pm »