Data/Sah/Compiler/perl/TH/hash.pm

filter: Statementstmt 96.5% 195 / 202 Branchbran 77.0% 57 / 74 Conditioncond 100% 16 / 16 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 18 totaltotal 87.8% 303 / 345 CC 45 SCAR 28.1
CC
45
Cov
92%
superclause_has_elems
28.3
_clause_keys_or_re_keys
25.0
superclause_comparable
3.0
CRAP 16.6
« Test/Data/Sah/Perl.pm ↑ Summary Data/Sah/Compiler/perl/TH/array.pm »
1 package Data::Sah::Compiler::perl::TH::hash;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any '$log';
7  
8 3 ▶use Data::Dmp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 extends 'Data::Sah::Compiler::perl::TH';
14 3 with 'Data::Sah::Type::hash';
15  
16 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
17 3 our $DATE = '2024-02-16'; # DATE
18 3 our $DIST = 'Data-Sah'; # DIST
19 3 our $VERSION = '0.917'; # VERSION
20  
21 742 ▶sub handle_type {
Subroutine handle_type: called CC=1 SCAR=0.0
Pod undocumented
22 742 my ($self, $cd) = @_;
23 742 my $c = $self->compiler;
24  
25 742 my $dt = $cd->{data_term};
26 742 $cd->{_ccl_check_type} = "ref($dt) eq 'HASH'";
27 }
28  
29 288 ▶sub superclause_comparable {
Subroutine superclause_comparable: called CC=3 SCAR=11.0
Pod undocumented
30 288 my ($self, $which, $cd) = @_;
31 288 my $c = $self->compiler;
32 288 my $ct = $cd->{cl_term};
33 288 my $dt = $cd->{data_term};
34  
35 288 $c->add_runtime_module($cd, $cd->{args}{dump_module});
36  
37 288 ▶ if ($which eq 'is') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'is') { } 150 138 288
elsif ($which eq 'in') { } 138 0 138
38 150 $c->add_ccl($cd, $c->expr_dump($cd, $dt).' eq '.$c->expr_dump($cd, $ct));
39 } elsif ($which eq 'in') {
40 138 $c->add_ccl($cd, "do { my \$_sahv_dt_str = ".$c->expr_dump($cd, $dt)."; my \$_sahv_res = 0; " .
41 "for my \$_sahv_el (\@{ $ct }) { my \$_sahv_el_str = ".$c->expr_dump($cd, "\$_sahv_el")."; ".
42 "if (\$_sahv_dt_str eq \$_sahv_el_str) { \$_sahv_res = 1; last } } \$_sahv_res }");
43 }
44 }
45  
46 115 ▶sub superclause_has_elems {
Subroutine superclause_has_elems: called CC=17 SCAR=33.4
Pod undocumented
47 115 my ($self_th, $which, $cd) = @_;
48 115 my $c = $self_th->compiler;
49 115 my $cv = $cd->{cl_value};
50 115 my $ct = $cd->{cl_term};
51 115 my $dt = $cd->{data_term};
52  
53 115 ▶ if ($which eq 'len') {
Branch: 11 branches
BranchTrueFalseTotal
if ($which eq 'len') { } 6 109 115
elsif ($which eq 'min_len') { } 34 75 109
elsif ($which eq 'max_len') { } 21 54 75
elsif ($which eq 'len_between') { } 6 48 54
elsif ($which eq 'has') { } 18 30 48
elsif ($which eq 'each_index') { } 12 18 30
elsif ($which eq 'each_elem') { } 18 0 18
elsif ($which eq 'check_each_index') { } 0 0 0
elsif ($which eq 'check_each_elem') { } 0 0 0
elsif ($which eq 'uniq') { } 0 0 0
elsif ($which eq 'exists') { } 0 0 0
54 6 $c->add_ccl($cd, "keys(\%{$dt}) == $ct");
55 } elsif ($which eq 'min_len') {
56 34 $c->add_ccl($cd, "keys(\%{$dt}) >= $ct");
57 } elsif ($which eq 'max_len') {
58 21 $c->add_ccl($cd, "keys(\%{$dt}) <= $ct");
59 } elsif ($which eq 'len_between') {
60 6 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'cl_is_expr'}) { } 0 6 6
61 0 $c->add_ccl(
62 $cd, "keys(\%{$dt}) >= $ct\->[0] && ".
63 "keys(\%{$dt}) >= $ct\->[1]");
64 } else {
65 # simplify code
66 6 $c->add_ccl(
67 $cd, "keys(\%{$dt}) >= $cv->[0] && ".
68 "keys(\%{$dt}) <= $cv->[1]");
69 }
70 } elsif ($which eq 'has') {
71 18 $c->add_runtime_module($cd, $cd->{args}{dump_module});
72 18 $c->add_ccl($cd, "do { my \$_sahv_ct_str = ".$c->expr_dump($cd, $ct)."; my \$_sahv_res = 0; " .
73 "for my \$_sahv_el (values \%{ $dt }) { my \$_sahv_el_str = ".$c->expr_dump($cd, "\$_sahv_el")."; ".
74 "if (\$_sahv_ct_str eq \$_sahv_el_str) { \$_sahv_res = 1; last } } \$_sahv_res }");
75 } elsif ($which eq 'each_index') {
76 12 ▶ $self_th->set_tmp_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 12 12
77 12 $self_th->gen_each($cd, "sort keys(\%{$cd->{data_term}})", '', '$_');
78 12 ▶ $self_th->restore_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 12 12
79 } elsif ($which eq 'each_elem') {
80 18 ▶ $self_th->set_tmp_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 18 18
81 18 $self_th->gen_each($cd, "sort keys(\%{$cd->{data_term}})", '_', "$cd->{data_term}\->{\$_}");
82 18 ▶ $self_th->restore_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 0 18 18
83 } elsif ($which eq 'check_each_index') {
84 0 $self_th->compiler->_die_unimplemented_clause($cd);
85 } elsif ($which eq 'check_each_elem') {
86 0 $self_th->compiler->_die_unimplemented_clause($cd);
87 } elsif ($which eq 'uniq') {
88 0 $self_th->compiler->_die_unimplemented_clause($cd);
89 } elsif ($which eq 'exists') {
90 0 $self_th->compiler->_die_unimplemented_clause($cd);
91 }
92 }
93  
94 55 ▶sub _clause_keys_or_re_keys {
Subroutine _clause_keys_or_re_keys: called CC=25 SCAR=32.2
95 55 require Data::Sah::Normalize;
96  
97 55 my ($self_th, $which, $cd) = @_;
98 55 my $c = $self_th->compiler;
99 55 my $cv = $cd->{cl_value};
100 55 my $dt = $cd->{data_term};
101  
102 55 local $cd->{_subdata_level} = $cd->{_subdata_level} + 1;
103  
104 # we handle subdata manually here, because in generated code for
105 # {keys,re_keys}.restrict, we haven't delved into the keys
106  
107 55 my $jccl;
108 {
109 55 local $cd->{ccls} = [];
110  
111 55 my $lit_valid_keys;
112 55 ▶ if ($which eq 'keys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq 'keys') { } 46 9 55
113 46 $lit_valid_keys = $c->literal([sort keys %$cv]);
114 } else {
115 $lit_valid_keys = "[".
116 9 join(",", map { "qr/".$c->_str2reliteral($cd, $_)."/" }
117 sort keys %$cv)."]";
118 }
119  
120 55 ▶ $self_th->set_tmp_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 1 54 55
121  
122 55 ▶ if ($cd->{clset}{"$which.restrict"} // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"clset"}{"$which.restrict"} // 1) 52 3 55
Condition100%2/2
$cd->{"clset"}{"$which.restrict"} // 1
l!l
352
MC/DC100%1/1
$cd->{"clset"}{"$which.restrict"} // 1
$cd->{"clset"}{"$which.restrict"}
Truth table100%2/2
A
A= $cd->{"clset"}{"$which.restrict"}
Aresult
11
00
123 52 local $cd->{_debug_ccl_note} = "$which.restrict";
124 #$c->add_runtime_module($cd, "List::Util");
125 52 ▶ $c->add_ccl(
Branch: 2 branches
BranchTrueFalseTotal
$which eq 'keys' ? : 46 6 52
$which eq 'keys' ? : 46 6 52
126 $cd,
127 #"!defined(List::Util::first(sub { my \$ditem=\$_; !defined(List::Util::first(sub {\$ditem ".($which eq 'keys' ? 'eq' : '=~')." \$_ }, \@{ $lit_valid_keys })) }, keys %{ $dt }))",
128 "!(grep { my \$_sahv_dt_item=\$_; !(grep { \$_sahv_dt_item ".($which eq 'keys' ? 'eq' : '=~')." \$_ } \@{ $lit_valid_keys }) } keys %{ $dt })",
129 {
130 err_msg => 'TMP',
131 err_expr => join(
132 "",
133 'sprintf(',
134 $c->literal($c->_xlt(
135 $cd, "hash contains ".
136 "unknown field(s) (%s)")),
137 ',',
138 "join(', ', sort grep { my \$_sahv_dt_item=\$_; !(grep { \$_sahv_dt_item ".($which eq 'keys' ? 'eq':'=~')." \$_ } \@{ $lit_valid_keys })} keys %{ $dt })",
139 ')',
140 ),
141 },
142 );
143 52 ▶ $self_th->restore_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 1 51 52
144 }
145 55 delete $cd->{uclset}{"$which.restrict"};
146  
147 55 my $cdef;
148 55 ▶ if ($which eq 'keys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($which eq "keys") 46 9 55
149 46 ▶ $cdef = $cd->{clset}{"keys.create_default"} // 1;
Condition100%2/2
$cd->{"clset"}{"keys.create_default"} // 1
l!l
640
MC/DC100%1/1
$cd->{"clset"}{"keys.create_default"} // 1
$cd->{"clset"}{"keys.create_default"}
Truth table100%2/2
A
A= $cd->{"clset"}{"keys.create_default"}
Aresult
11
00
150 46 delete $cd->{uclset}{"keys.create_default"};
151 }
152  
153 55 ▶ $self_th->set_tmp_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 1 54 55
154  
155 55 my $nkeys = scalar(keys %$cv);
156 55 my $i = 0;
157 55 for my $k (sort keys %$cv) {
158 100 my $kre = $c->_str2reliteral($cd, $k);
159 100 local $cd->{spath} = [@{ $cd->{spath} }, $k];
160 100 ++$i;
161 100 my $nsch = Data::Sah::Normalize::normalize_schema($cv->{$k});
162 100 my $kdn = $k; $kdn =~ s/\W+/_/g;
163 100 ▶ my $klit = $which eq 're_keys' ? '$_' : $c->literal($k);
Branch: 1 branch
BranchTrueFalseTotal
$which eq 're_keys' ? : 9 91 100
164 100 my $kdt = "$dt\->{$klit}";
165 100 my %iargs = %{$cd->{args}};
166 100 $iargs{outer_cd} = $cd;
167 100 $iargs{data_name} = $kdn;
168 100 $iargs{data_term} = $kdt;
169 100 $iargs{schema} = $nsch;
170 100 $iargs{schema_is_normalized} = 1;
171 100 $iargs{cache} = $cd->{args}{cache};
172 100 $iargs{indent_level}++;
173 100 ▶ $iargs{data_term_includes_topic_var} = 1 if $which eq 're_keys';
Branch: 1 branch
BranchTrueFalseTotal
if $which eq "re_keys" 9 91 100
174 100 my $icd = $c->compile(%iargs);
175  
176 # should we set default for hash value?
177 100 ▶ my $sdef = $cdef && defined($nsch->[1]{default});
Condition100%3/3
$cdef && defined $nsch->[1]{'default'}
!ll&&!rl&&r
21736
MC/DC100%2/2
$cdef && defined $nsch->[1]{'default'}
$cdef defined $nsch->[1]{'default'}
Truth table100%3/3
A && B
A= $cdefB= defined $nsch->[1]{'default'}
ABresult
0X0
100
111
178  
179 # stack is used to store (non-bool) subresults
180 100 ▶ $c->add_var($cd, '_sahv_stack', []) if $cd->{use_dpath};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"use_dpath"} 67 33 100
181  
182 100 ▶ $c->add_runtime_module($cd, "List::Util") if $which eq 're_keys'; # for re_keys
Branch: 1 branch
BranchTrueFalseTotal
if $which eq "re_keys" 9 91 100
183  
184 my @code = (
185 ($c->indent_str($cd), "(push(@\$_sahv_dpath, undef), push(\@\$_sahv_stack, undef), \$_sahv_stack->[-1] = \n")
186 x !!($cd->{use_dpath} && $i == 1),
187  
188 # for re_keys, we iterate over all data's keys which match regex
189 ('(!defined(List::Util::first(sub {!(')
190 x !!($which eq 're_keys'),
191  
192 $which eq 're_keys' ? "\$_ !~ /$kre/ || (" :
193 ($sdef ? "" : "!exists($kdt) || ("),
194  
195 ($c->indent_str($cd), "(\$_sahv_dpath->[-1] = ".
196 ($which eq 're_keys' ? '$_' : $klit)."),\n")
197 x !!$cd->{use_dpath},
198 $icd->{result}, "\n",
199  
200 $which eq 're_keys' || !$sdef ? ")" : "",
201  
202 # close iteration over all data's keys which match regex
203 (")}, sort keys %{ $dt })))")
204 x !!($which eq 're_keys'),
205  
206 ($c->indent_str($cd), "), pop(\@\$_sahv_dpath), pop(\@\$_sahv_stack)\n")
207 100 ▶ x !!($cd->{use_dpath} && $i == $nkeys),
Branch: 4 branches
BranchTrueFalseTotal
$which eq 're_keys' ? : 9 91 100
$sdef ? : 6 85 91
$which eq 're_keys' ? : 9 91 100
$which eq 're_keys' || !$sdef ? : 94 6 100
Condition100%9/9
$cd->{'use_dpath'} && $i == 1
!ll&&!rl&&r
333037
$which eq "re_keys" or not $sdef
l!l&&r!l&&!r
9856
$cd->{'use_dpath'} && $i == $nkeys
!ll&&!rl&&r
333037
MC/DC100%2/2
$cd->{'use_dpath'} && $i == 1
$cd->{'use_dpath'} $i == 1
MC/DC100%2/2
$which eq "re_keys" or not $sdef
$which eq "re_keys" not $sdef
MC/DC100%2/2
$cd->{'use_dpath'} && $i == $nkeys
$cd->{'use_dpath'} $i == $nkeys
Truth table100%3/3
A && B
A= $cd->{'use_dpath'}B= $i == 1
ABresult
0X0
100
111
Truth table100%3/3
A or not(B)
A= $which eq "re_keys"B= not $sdef
ABresult
1X1
011
000
Truth table100%3/3
A && B
A= $cd->{'use_dpath'}B= $i == $nkeys
ABresult
0X0
100
111
208 );
209 100 my $ires = join("", @code);
210 100 local $cd->{_debug_ccl_note} = "$which: ".$c->literal($k);
211 100 $c->add_ccl($cd, $ires);
212 }
213  
214 55 ▶ $self_th->restore_data_term($cd) if $cd->{args}{data_term_includes_topic_var};
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"data_term_includes_topic_var"} 1 54 55
215  
216 $jccl = $c->join_ccls(
217 55 $cd, $cd->{ccls}, {err_msg => ''});
218 }
219 55 $c->add_ccl($cd, $jccl, {});
220 }
221  
222 46 ▶sub clause_keys {
Subroutine clause_keys: called CC=1 SCAR=0.0
Pod undocumented
223 46 my ($self, $cd) = @_;
224 46 $self->_clause_keys_or_re_keys('keys', $cd);
225 }
226  
227 9 ▶sub clause_re_keys {
Subroutine clause_re_keys: called CC=1 SCAR=0.0
Pod undocumented
228 9 my ($self, $cd) = @_;
229 9 $self->_clause_keys_or_re_keys('re_keys', $cd);
230 }
231  
232 45 ▶sub clause_req_keys {
Subroutine clause_req_keys: called CC=1 SCAR=0.0
Pod undocumented
233 45 my ($self, $cd) = @_;
234 45 my $c = $self->compiler;
235 45 my $ct = $cd->{cl_term};
236 45 my $dt = $cd->{data_term};
237  
238 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
239  
240 45 $c->add_runtime_module($cd, "List::Util");
241 45 $c->add_ccl(
242 $cd,
243 "do { my \$_sahv_h = $dt; !defined(List::Util::first(sub {!exists(\$_sahv_h\->{\$_})}, \@{ $ct })) }",
244 {
245 err_msg => 'TMP',
246 err_expr =>
247 "sprintf(".
248 $c->literal($c->_xlt($cd, "hash has missing required field(s) (%s)")).
249 ",join(', ', do { my \$_sahv_h = $dt; grep { !exists(\$_sahv_h\->{\$_}) } \@{ $ct } }))"
250 }
251 );
252 }
253  
254 9 ▶sub clause_allowed_keys {
Subroutine clause_allowed_keys: called CC=1 SCAR=0.0
Pod undocumented
255 9 my ($self, $cd) = @_;
256 9 my $c = $self->compiler;
257 9 my $ct = $cd->{cl_term};
258 9 my $dt = $cd->{data_term};
259  
260 #$c->add_runtime_module($cd, "List::Util");
261 9 $c->add_ccl(
262 $cd,
263 #"!defined(List::Util::first(sub { my \$_sahv_dt_item=\$_; !defined(List::Util::first!(sub { \$_sahv_dt_item eq \$_ }, \@{ $ct })) }, keys \%{ $dt }))",
264 "!(grep { my \$_sahv_dt_item=\$_; !(grep { \$_sahv_dt_item eq \$_ } \@{ $ct }) } keys \%{ $dt })",
265 {
266 err_msg => 'TMP',
267 err_expr =>
268 "sprintf(".
269 $c->literal($c->_xlt($cd, "hash contains non-allowed field(s) (%s)")).
270 ",join(', ', sort grep { my \$_sahv_dt_item=\$_; !(grep { \$_sahv_dt_item eq \$_ } \@{ $ct }) } keys \%{ $dt }))"
271 }
272 );
273 }
274  
275 9 ▶sub clause_allowed_keys_re {
Subroutine clause_allowed_keys_re: called CC=2 SCAR=7.1
Pod undocumented
276 9 my ($self, $cd) = @_;
277 9 my $c = $self->compiler;
278 #my $ct = $cd->{cl_term};
279 9 my $cv = $cd->{cl_value};
280 9 my $dt = $cd->{data_term};
281  
282 9 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"cl_is_expr"}) 0 9 9
283 # i'm lazy atm and does not need expr yet
284 0 $c->_die_unimplemented_clause($cd, "with expr");
285 }
286  
287 9 my $re = $c->_str2reliteral($cd, $cv);
288 #$c->add_runtime_module($cd, "List::Util");
289 9 $c->add_ccl(
290 $cd,
291 #"!defined(List::Util::first(sub {\$_ !~ /$re/}, keys \%{ $dt }))",
292 "!(grep {\$_ !~ /$re/} keys \%{ $dt })",
293 {
294 err_msg => 'TMP',
295 err_expr =>
296 "sprintf(".
297 $c->literal($c->_xlt($cd, "hash contains non-allowed field(s) (%s)")).
298 ",join(', ', sort grep { \$_ !~ /$re/ } keys \%{ $dt }))"
299 }
300 );
301 }
302  
303 9 ▶sub clause_forbidden_keys {
Subroutine clause_forbidden_keys: called CC=1 SCAR=0.0
Pod undocumented
304 9 my ($self, $cd) = @_;
305 9 my $c = $self->compiler;
306 9 my $ct = $cd->{cl_term};
307 9 my $dt = $cd->{data_term};
308  
309 #$c->add_runtime_module($cd, "List::Util");
310 9 $c->add_ccl(
311 $cd,
312 "!(grep { my \$_sahv_dt_item=\$_; !!(grep { \$_sahv_dt_item eq \$_ } \@{ $ct }) } keys \%{ $dt })",
313 {
314 err_msg => 'TMP',
315 err_expr =>
316 "sprintf(".
317 $c->literal($c->_xlt($cd, "hash contains forbidden field(s) (%s)")).
318 ",join(', ', sort grep { my \$_sahv_dt_item=\$_; !(grep { \$_sahv_dt_item eq \$_ } \@{ $ct }) } keys \%{ $dt }))"
319 }
320 );
321 }
322  
323 9 ▶sub clause_forbidden_keys_re {
Subroutine clause_forbidden_keys_re: called CC=2 SCAR=7.1
Pod undocumented
324 9 my ($self, $cd) = @_;
325 9 my $c = $self->compiler;
326 #my $ct = $cd->{cl_term};
327 9 my $cv = $cd->{cl_value};
328 9 my $dt = $cd->{data_term};
329  
330 9 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"cl_is_expr"}) 0 9 9
331 # i'm lazy atm and does not need expr yet
332 0 $c->_die_unimplemented_clause($cd, "with expr");
333 }
334  
335 9 my $re = $c->_str2reliteral($cd, $cv);
336 #$c->add_runtime_module($cd, "List::Util");
337 9 $c->add_ccl(
338 $cd,
339 #"!defined(List::Util::first(sub {\$_ =~ /$re/}, keys \%{ $dt }))",
340 "!(grep {\$_ =~ /$re/} keys \%{ $dt })",
341 {
342 err_msg => 'TMP',
343 err_expr =>
344 "sprintf(".
345 $c->literal($c->_xlt($cd, "hash contains forbidden field(s) (%s)")).
346 ",join(', ', sort grep { \$_ =~ /$re/ } keys \%{ $dt }))"
347 }
348 );
349 }
350  
351 36 ▶sub clause_choose_one_key {
Subroutine clause_choose_one_key: called CC=1 SCAR=0.0
Pod undocumented
352 36 my ($self, $cd) = @_;
353 36 my $c = $self->compiler;
354 36 my $ct = $cd->{cl_term};
355 36 my $dt = $cd->{data_term};
356  
357 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
358  
359 36 $c->add_runtime_module($cd, "List::Util");
360 36 $c->add_ccl(
361 $cd,
362 "do { my \$_sahv_h = $dt; List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ $ct }) <= 1 }",
363 {},
364 );
365 }
366  
367 36 ▶sub clause_choose_all_keys {
Subroutine clause_choose_all_keys: called CC=1 SCAR=0.0
Pod undocumented
368 36 my ($self, $cd) = @_;
369 36 my $c = $self->compiler;
370 36 my $ct = $cd->{cl_term};
371 36 my $dt = $cd->{data_term};
372  
373 # we assign to $h first to avoid variable clashing if $dt is '$_'.
374  
375 36 $c->add_runtime_module($cd, "List::Util");
376 36 $c->add_ccl(
377 $cd,
378 "do { my \$_sahv_h = $dt; my \$_sahv_keys = $ct; my \$_sahv_tot = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@\$_sahv_keys); \$_sahv_tot==0 || \$_sahv_tot==\@\$_sahv_keys }",
379 {},
380 );
381 }
382  
383 24 ▶sub clause_req_one_key {
Subroutine clause_req_one_key: called CC=1 SCAR=0.0
Pod undocumented
384 24 my ($self, $cd) = @_;
385 24 my $c = $self->compiler;
386 24 my $ct = $cd->{cl_term};
387 24 my $dt = $cd->{data_term};
388  
389 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
390  
391 24 $c->add_runtime_module($cd, "List::Util");
392 24 $c->add_ccl(
393 $cd,
394 "do { my \$_sahv_h = $dt; List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ $ct }) == 1 }",
395 {},
396 );
397 }
398  
399 60 ▶sub clause_req_some_keys {
Subroutine clause_req_some_keys: called CC=1 SCAR=0.0
Pod undocumented
400 60 my ($self, $cd) = @_;
401 60 my $c = $self->compiler;
402 60 my $cv = $cd->{cl_value};
403 60 my $dt = $cd->{data_term};
404  
405 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
406  
407 60 $c->add_runtime_module($cd, "List::Util");
408 60 $c->add_ccl(
409 $cd,
410 "do { my \$_sahv_h = $dt; my \$_sahv_n = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ ".$c->literal($cv->[2])." }); \$_sahv_n >= $cv->[0] && \$_sahv_n <= $cv->[1] }",
411 {},
412 );
413 }
414  
415 18 ▶sub clause_dep_any {
Subroutine clause_dep_any: called CC=1 SCAR=0.0
Pod undocumented
416 18 my ($self, $cd) = @_;
417 18 my $c = $self->compiler;
418 18 my $ct = $cd->{cl_term};
419 18 my $dt = $cd->{data_term};
420  
421 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
422  
423 18 $c->add_runtime_module($cd, "List::Util");
424 18 $c->add_ccl(
425 $cd,
426 "do { my \$_sahv_h = $dt; my \$_sahv_ct = $ct; ".
427 "my \$_sahv_has_prereq = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ \$_sahv_ct->[1] }) ? 1:0; ".
428 "my \$_sahv_has_dep = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} (ref(\$_sahv_ct->[0]) eq 'ARRAY' ? \@{ \$_sahv_ct->[0] } : (\$_sahv_ct->[0]))) ? 1:0; ".
429 "!\$_sahv_has_dep || \$_sahv_has_prereq }",
430 {},
431 );
432 }
433  
434 18 ▶sub clause_dep_all {
Subroutine clause_dep_all: called CC=1 SCAR=0.0
Pod undocumented
435 18 my ($self, $cd) = @_;
436 18 my $c = $self->compiler;
437 18 my $ct = $cd->{cl_term};
438 18 my $dt = $cd->{data_term};
439  
440 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
441  
442 18 $c->add_runtime_module($cd, "List::Util");
443 18 $c->add_ccl(
444 $cd,
445 "do { my \$_sahv_h = $dt; my \$_sahv_ct = $ct; ".
446 "my \$_sahv_has_prereq = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ \$_sahv_ct->[1] }) == \@{ \$_sahv_ct->[1] } ? 1:0; ".
447 "my \$_sahv_has_dep = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} (ref(\$_sahv_ct->[0]) eq 'ARRAY' ? \@{ \$_sahv_ct->[0] } : (\$_sahv_ct->[0]))) ? 1:0; ".
448 "!\$_sahv_has_dep || \$_sahv_has_prereq }",
449 {},
450 );
451 }
452  
453 24 ▶sub clause_req_dep_any {
Subroutine clause_req_dep_any: called CC=1 SCAR=0.0
Pod undocumented
454 24 my ($self, $cd) = @_;
455 24 my $c = $self->compiler;
456 24 my $ct = $cd->{cl_term};
457 24 my $dt = $cd->{data_term};
458  
459 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
460  
461 24 $c->add_runtime_module($cd, "List::Util");
462 24 $c->add_ccl(
463 $cd,
464 "do { my \$_sahv_h = $dt; my \$_sahv_ct = $ct; ".
465 "my \$_sahv_has_prereq = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ \$_sahv_ct->[1] }) ? 1:0; ".
466 "my \$_sahv_has_dep = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} (ref(\$_sahv_ct->[0]) eq 'ARRAY' ? \@{ \$_sahv_ct->[0] } : (\$_sahv_ct->[0]))) ? 1:0; ".
467 "\$_sahv_has_dep || !\$_sahv_has_prereq }",
468 {},
469 );
470 }
471  
472 24 ▶sub clause_req_dep_all {
Subroutine clause_req_dep_all: called CC=1 SCAR=0.0
Pod undocumented
473 24 my ($self, $cd) = @_;
474 24 my $c = $self->compiler;
475 24 my $ct = $cd->{cl_term};
476 24 my $dt = $cd->{data_term};
477  
478 # we assign to $_sahv_h first to avoid variable clashing if $dt is '$_'.
479  
480 24 $c->add_runtime_module($cd, "List::Util");
481 24 $c->add_ccl(
482 $cd,
483 "do { my \$_sahv_h = $dt; my \$_sahv_ct = $ct; ".
484 "my \$_sahv_has_prereq = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} \@{ \$_sahv_ct->[1] }) == \@{ \$_sahv_ct->[1] } ? 1:0; ".
485 "my \$_sahv_has_dep = List::Util::sum(map {exists(\$_sahv_h\->{\$_}) ? 1:0} (ref(\$_sahv_ct->[0]) eq 'ARRAY' ? \@{ \$_sahv_ct->[0] } : (\$_sahv_ct->[0]))) ? 1:0; ".
486 "\$_sahv_has_dep || !\$_sahv_has_prereq }",
487 {},
488 );
489 }
490  
491 3 1;
492 # ABSTRACT: perl's type handler for type "hash"
493  
494 __END__
« Test/Data/Sah/Perl.pm ↑ Summary Data/Sah/Compiler/perl/TH/array.pm »