TUWF/Misc.pm

filter: Statementstmt 59.2% 77 / 130 Branchbran 58.1% 57 / 98 Conditioncond 80.3% 41 / 51 MC/DCmcdc 78.7% 26 / 33 Subroutinesub 50.0% 11 / 22 Podpod 100% 2 / 2 totaltotal 63.6% 214 / 336 CC 72 SCAR 34.9
CC
72
Cov
63%
validate
110.0
mail
90.0
_JSON
56.0
CRAP 32.7
« TUWF/Request.pm ↑ Summary TUWF/XML.pm »
1  
2 package TUWF::Misc;
3 # Yeah, just put all miscellaneous functions in one module!
4 # Geez, talk about being sloppy...
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use TUWF::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 1 our $VERSION = '1.6';
15 1 our @EXPORT_OK = ('uri_escape', 'kv_validate');
16  
17  
18 0 ▶sub uri_escape {
Subroutine uri_escape: not called CC=1 SCAR=6.9
Pod documented
19 0 utf8::encode(local $_ = shift);
20 0 s/([^A-Za-z0-9._~-])/sprintf '%%%02X', ord $1/eg;
21 0 return $_;
22 }
23  
24  
25  
26  
27 13 ▶sub _template_validate_num {
Subroutine _template_validate_num: called CC=5 SCAR=16.1
28 13 $_[0] *= 1; # Normalize to perl number
29 13 ▶ return 0 if defined($_[1]{min}) && $_[0] < $_[1]{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1]{"min"} and $_[0] < $_[1]{"min"} 4 9 13
Condition100%3/3
defined $_[1]{"min"} and $_[0] < $_[1]{"min"}
!ll&&!rl&&r
814
MC/DC100%2/2
defined $_[1]{"min"} and $_[0] < $_[1]{"min"}
defined $_[1]{"min"} $_[0] < $_[1]{"min"}
Truth table100%3/3
A and B
A= defined $_[1]{"min"}B= $_[0] < $_[1]{"min"}
ABresult
0X0
100
111
30 9 ▶ return 0 if defined($_[1]{max}) && $_[0] > $_[1]{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1]{"max"} and $_[0] > $_[1]{"max"} 3 6 9
Condition100%3/3
defined $_[1]{"max"} and $_[0] > $_[1]{"max"}
!ll&&!rl&&r
513
MC/DC100%2/2
defined $_[1]{"max"} and $_[0] > $_[1]{"max"}
defined $_[1]{"max"} $_[0] > $_[1]{"max"}
Truth table100%3/3
A and B
A= defined $_[1]{"max"}B= $_[0] > $_[1]{"max"}
ABresult
0X0
100
111
31 6 return 1;
32 }
33  
34  
35 1 my %default_templates = (
36 # JSON number format, regex from http://stackoverflow.com/questions/13340717/json-numbers-regular-expression
37 num => { func => \&_template_validate_num, regex => qr/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/, inherit => ['min','max'] },
38 int => { func => \&_template_validate_num, regex => qr/^-?(?:0|[1-9]\d*)$/, inherit => ['min','max'] },
39 uint => { func => \&_template_validate_num, regex => qr/^(?:0|[1-9]\d*)$/, inherit => ['min','max'] },
40 ascii => { regex => qr/^[\x20-\x7E]*$/ },
41 email => { regex => $TUWF::Validate::re_email, maxlength => 254 },
42 weburl => { regex => $TUWF::Validate::re_weburl, maxlength => 65536 }, # the maxlength is a bit arbitrary, but better than unlimited
43 );
44  
45  
46 92 ▶sub kv_validate {
Subroutine kv_validate: called CC=15 SCAR=27.1
Pod documented
47 92 my($sources, $templates, $params) = @_;
48 92 $templates = { %default_templates, %$templates };
49  
50 92 my @err;
51 my %ret;
52  
53 92 for my $f (@$params) {
54 # Inherit some options from templates.
55 !exists($f->{$_}) && _val_from_tpl($f, $_, $templates, $f)
56 92 ▶ for(qw|required default rmwhitespace multi mincount maxcount|);
Branch: 1 branch
BranchTrueFalseTotal
exists $f->{$_} or _val_from_tpl($f, $_, $templates, $f) 18 534 552
57  
58 92 my $src = (grep $f->{$_}, keys %$sources)[0];
59 92 my @values = $sources->{$src}->($f->{$src});
60 92 ▶ @values = ($values[0]) if !$f->{multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $f->{"multi"} 81 11 92
61  
62 # check each value and add it to %ret
63 92 for (@values) {
64 97 ▶ my $errfield = _validate_early($_, $f) || _validate($_, $templates, $f);
Condition100%3/3
_validate_early($_, $f) || _validate($_, $templates, $f)
l!l&&r!l&&!r
94147
MC/DC100%2/2
_validate_early($_, $f) || _validate($_, $templates, $f)
_validate_early($_, $f) _validate($_, $templates, $f)
Truth table100%3/3
A || B
A= _validate_early($_, $f)B= _validate($_, $templates, $f)
ABresult
1X1
011
000
65 97 ▶ next if !$errfield || $errfield eq 'default';
Branch: 1 branch
BranchTrueFalseTotal
if not $errfield or $errfield eq "default" 50 47 97
Condition100%3/3
not $errfield or $errfield eq "default"
l!l&&r!l&&!r
47347
MC/DC100%2/2
not $errfield or $errfield eq "default"
not $errfield $errfield eq "default"
Truth table100%3/3
not(A) or B
A= not $errfieldB= $errfield eq "default"
ABresult
1X1
011
000
66 47 push @err, [ $f->{$src}, $errfield, $f->{$errfield} ];
67 47 last;
68 }
69 92 ▶ $ret{$f->{$src}} = $f->{multi} ? \@values : $values[0];
Branch: 1 branch
BranchTrueFalseTotal
$f->{'multi'} ? : 11 81 92
70  
71 # check mincount/maxcount
72 92 ▶ push @err, [ $f->{$src}, 'mincount', $f->{mincount} ] if $f->{mincount} && @values < $f->{mincount};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"mincount"} and @values < $f->{"mincount"} 2 90 92
Condition100%3/3
$f->{"mincount"} and @values < $f->{"mincount"}
!ll&&!rl&&r
8732
MC/DC100%2/2
$f->{"mincount"} and @values < $f->{"mincount"}
$f->{"mincount"} @values < $f->{"mincount"}
Truth table100%3/3
A and B
A= $f->{"mincount"}B= @values < $f->{"mincount"}
ABresult
0X0
100
111
73 92 ▶ push @err, [ $f->{$src}, 'maxcount', $f->{maxcount} ] if $f->{maxcount} && @values > $f->{maxcount};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"maxcount"} and @values > $f->{"maxcount"} 2 90 92
Condition100%3/3
$f->{"maxcount"} and @values > $f->{"maxcount"}
!ll&&!rl&&r
8732
MC/DC100%2/2
$f->{"maxcount"} and @values > $f->{"maxcount"}
$f->{"maxcount"} @values > $f->{"maxcount"}
Truth table100%3/3
A and B
A= $f->{"maxcount"}B= @values > $f->{"maxcount"}
ABresult
0X0
100
111
74 }
75  
76 92 ▶ $ret{_err} = \@err if @err;
Branch: 1 branch
BranchTrueFalseTotal
if @err 51 41 92
77 92 return \%ret;
78 }
79  
80  
81 896 ▶sub _val_from_tpl {
Subroutine _val_from_tpl: called CC=3 SCAR=11.0
82 896 my($top_rules, $field, $tpls, $rules) = @_;
83 896 ▶ return if !$rules->{template};
Branch: 1 branch
BranchTrueFalseTotal
unless $rules->{"template"} 517 379 896
84 379 my $tpl = $tpls->{$rules->{template}};
85 379 ▶ if(exists $tpl->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tpl->{$field}) { } 17 362 379
86 17 $top_rules->{$field} = $tpl->{$field};
87 } else {
88 362 _val_from_tpl($top_rules, $field, $tpls, $tpl);
89 }
90 }
91  
92  
93 # Initial validation of a value. Same as _validate() below, but this one
94 # validates options that need to be checked only once. (The checks in
95 # _validate() may run several times when templates are used).
96 97 ▶sub _validate_early { # value, \%rules
Subroutine _validate_early: called CC=9 SCAR=22.0
97 97 my($v, $r) = @_;
98  
99 97 ▶ $r->{required}++ if not exists $r->{required};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $r->{"required"} 86 11 97
100 97 ▶ $r->{rmwhitespace}++ if not exists $r->{rmwhitespace};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $r->{"rmwhitespace"} 88 9 97
101  
102 # remove whitespace
103 97 ▶ if($v && $r->{rmwhitespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v and $r->{"rmwhitespace"}) 75 22 97
Condition100%3/3
$v and $r->{"rmwhitespace"}
!ll&&!rl&&r
15775
MC/DC100%2/2
$v and $r->{"rmwhitespace"}
$v $r->{"rmwhitespace"}
Truth table100%3/3
A and B
A= $vB= $r->{"rmwhitespace"}
ABresult
0X0
100
111
104 75 $_[0] =~ s/\r//g;
105 75 $_[0] =~ s/^[\s\n]+//;
106 75 $_[0] =~ s/[\s\n]+$//;
107 75 $v = $_[0]
108 }
109  
110 # empty
111 97 ▶ if(!defined($v) || length($v) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $v or length $v < 1) 9 88 97
Condition100%3/3
not defined $v or length $v < 1
l!l&&r!l&&!r
3688
MC/DC100%2/2
not defined $v or length $v < 1
not defined $v length $v < 1
Truth table100%3/3
not(A) or B
A= not defined $vB= length $v < 1
ABresult
1X1
011
000
112 9 ▶ return 'required' if $r->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"required"} 6 3 9
113 3 ▶ $_[0] = $r->{default} if exists $r->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{"default"} 2 1 3
114 3 return 'default';
115 }
116 88 return undef;
117 }
118  
119  
120 # Internal function used by kv_validate, checks one value on the validation
121 # rules, the name of the failed rule on error, undef otherwise
122 155 ▶sub _validate { # value, \%templates, \%rules
Subroutine _validate: called CC=19 SCAR=29.5
123 155 my($v, $t, $r) = @_;
124  
125 155 ▶ croak "Template $r->{template} not defined." if $r->{template} && !$t->{$r->{template}};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"template"} and not $$t{$r->{"template"}} 0 155 155
Condition66%2/3
$r->{"template"} and not $$t{$r->{"template"}}
!ll&&!rl&&r
87680
MC/DC0%0/2
$r->{"template"} and not $$t{$r->{"template"}}
$r->{"template"} not $$t{$r->{"template"}}
Truth table66%2/3
A and not(B)
A= $r->{"template"}B= not $$t{$r->{"template"}}
ABresult
0X0
100
111
126  
127 # length
128 155 ▶ return 'minlength' if $r->{minlength} && length $v < $r->{minlength};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"minlength"} and length $v < $r->{"minlength"} 2 153 155
Condition100%3/3
$r->{"minlength"} and length $v < $r->{"minlength"}
!ll&&!rl&&r
15032
MC/DC100%2/2
$r->{"minlength"} and length $v < $r->{"minlength"}
$r->{"minlength"} length $v < $r->{"minlength"}
Truth table100%3/3
A and B
A= $r->{"minlength"}B= length $v < $r->{"minlength"}
ABresult
0X0
100
111
129 153 ▶ return 'maxlength' if $r->{maxlength} && length $v > $r->{maxlength};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"maxlength"} and length $v > $r->{"maxlength"} 3 150 153
Condition100%3/3
$r->{"maxlength"} and length $v > $r->{"maxlength"}
!ll&&!rl&&r
117333
MC/DC100%2/2
$r->{"maxlength"} and length $v > $r->{"maxlength"}
$r->{"maxlength"} length $v > $r->{"maxlength"}
Truth table100%3/3
A and B
A= $r->{"maxlength"}B= length $v > $r->{"maxlength"}
ABresult
0X0
100
111
130 # enum
131 150 ▶ return 'enum' if $r->{enum} && !grep $_ eq $v, @{$r->{enum}};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"enum"} and not grep(($_ eq $v), @{$$r{"enum"};}) 1 149 150
Condition100%3/3
$r->{"enum"} and not grep(($_ eq $v), @{$$r{"enum"};})
!ll&&!rl&&r
14721
MC/DC100%2/2
$r->{"enum"} and not grep(($_ eq $v), @{$$r{"enum"};})
$r->{"enum"} not grep(($_ eq $v), @{$$r{"enum"};})
Truth table100%3/3
A and not(B)
A= $r->{"enum"}B= not grep(($_ eq $v), @{$$r{"enum"};})
ABresult
0X0
100
111
132 # regex
133 149 ▶ return 'regex' if $r->{regex} && (ref($r->{regex}) eq 'ARRAY' ? ($v !~ m/$r->{regex}[0]/) : ($v !~ m/$r->{regex}/));
Branch: 2 branches
BranchTrueFalseTotal
if $r->{"regex"} and ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/) 25 124 149
ref $r->{'regex'} eq 'ARRAY' ? : 1 52 53
Condition100%3/3
$r->{"regex"} and ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
!ll&&!rl&&r
962825
MC/DC100%2/2
$r->{"regex"} and ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
$r->{"regex"} ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
Truth table100%3/3
A and B
A= $r->{"regex"}B= ref $r->{"regex"} eq "ARRAY" ? !($v =~ /$r->{"regex"}[0]/i) : !($v =~ /$r->{"regex"}/)
ABresult
0X0
100
111
134 # template
135 124 ▶ if($r->{template}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"template"}) 67 57 124
136 67 my $in = $t->{$r->{template}}{inherit};
137 67 ▶ my %r = (($in ? (map exists($r->{$_}) ? ($_,$r->{$_}) : (), @$in) : ()), %{$t->{$r->{template}}});
Branch: 2 branches
BranchTrueFalseTotal
$in ? : 18 49 67
exists $r->{$_} ? : 11 23 34
138 67 ▶ return 'template' if _validate($_[0], $t, \%r);
Branch: 1 branch
BranchTrueFalseTotal
if _validate($_[0], $t, \%r) 32 35 67
139 }
140 # function
141 92 ▶ return 'func' if $r->{func} && (ref($r->{func}) eq 'ARRAY' ? !$r->{func}[0]->($_[0], $r) : !$r->{func}->($_[0], $r));
Branch: 2 branches
BranchTrueFalseTotal
if $r->{"func"} and ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r) 10 82 92
ref $r->{'func'} eq 'ARRAY' ? : 1 18 19
Condition100%3/3
$r->{"func"} and ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
!ll&&!rl&&r
73910
MC/DC100%2/2
$r->{"func"} and ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
$r->{"func"} ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
Truth table100%3/3
A and B
A= $r->{"func"}B= ref $r->{"func"} eq "ARRAY" ? !$r->{"func"}[0]->($_[0], $r) : !$r->{"func"}->($_[0], $r)
ABresult
0X0
100
111
142 # passed validation
143 82 return undef;
144 }
145  
146  
147  
148  
149 0 ▶sub TUWF::Object::formValidate {
Subroutine formValidate: not called CC=2 SCAR=17.9
150 0 my($self, @fields) = @_;
151 return kv_validate(
152 0 ▶ { post => sub { $self->reqPosts(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 ▶ get => sub { $self->reqGets(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 ▶ param => sub { $self->reqParams(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 ▶ cookie => sub { $self->reqCookie(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 }, $self->{_TUWF}{validate_templates} || {},
157 \@fields
158 0 ▶ );
Condition0%0/2
$self->{'_TUWF'}{'validate_templates'} || {}
l!l
00
MC/DC0%0/1
$self->{'_TUWF'}{'validate_templates'} || {}
$self->{'_TUWF'}{'validate_templates'}
Truth table0%0/2
A
A= $self->{'_TUWF'}{'validate_templates'}
Aresult
11
00
159 }
160  
161  
162  
163 # A simple mail function, body and headers as arguments. Usage:
164 # $self->mail('body', header1 => 'value of header 1', ..);
165 0 ▶sub TUWF::Object::mail {
Subroutine mail: not called CC=9 SCAR=45.0
166 0 my $self = shift;
167 0 my $body = shift;
168 0 my %hs = @_;
169  
170 0 ▶ croak "No To: specified!\n" if !$hs{To};
Branch: 1 branch
BranchTrueFalseTotal
unless $hs{"To"} 0 0 0
171 0 ▶ croak "No Subject: specified!\n" if !$hs{Subject};
Branch: 1 branch
BranchTrueFalseTotal
unless $hs{"Subject"} 0 0 0
172 0 ▶ $hs{'Content-Type'} ||= 'text/plain; charset=\'UTF-8\'';
Condition0%0/2
$hs{"Content-Type"} ||= "text/plain; charset='UTF-8'"
l!l
00
MC/DC0%0/1
$hs{"Content-Type"} ||= "text/plain; charset='UTF-8'"
$hs{"Content-Type"}
Truth table0%0/2
A
A= $hs{"Content-Type"}
Aresult
11
00
173 0 ▶ $hs{From} ||= $self->{_TUWF}{mail_from};
Condition0%0/3
$hs{"From"} ||= $self->{"_TUWF"}{"mail_from"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hs{"From"} ||= $self->{"_TUWF"}{"mail_from"}
$hs{"From"} $self->{"_TUWF"}{"mail_from"}
Truth table0%0/3
A ||= B
A= $hs{"From"}B= $self->{"_TUWF"}{"mail_from"}
ABresult
1X1
011
000
174 0 $body =~ s/\r?\n/\n/g;
175  
176 0 my $mail = '';
177 0 foreach (keys %hs) {
178 0 $hs{$_} =~ s/[\r\n]//g;
179 0 $mail .= sprintf "%s: %s\n", $_, $hs{$_};
180 }
181 0 $mail .= sprintf "\n%s", $body;
182  
183 0 ▶ if($self->{_TUWF}{mail_sendmail} eq 'log') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_TUWF'}{'mail_sendmail'} eq 'log') { } 0 0 0
elsif (open my $mailer, '|-:utf8', "$self->{'_TUWF'}{'mail_sendmail'} -t -f '$hs{'From'}'") { } 0 0 0
184 0 $self->log("tuwf->mail(): The following mail would have been sent:\n$mail");
185 } elsif(open(my $mailer, '|-:utf8', "$self->{_TUWF}{mail_sendmail} -t -f '$hs{From}'")) {
186 0 print $mailer $mail;
187 0 ▶ croak "Error running sendmail ($!)"
Branch: 1 branch
BranchTrueFalseTotal
if not close $mailer 0 0 0
188 if !close($mailer);
189 } else {
190 0 croak "Error opening sendail ($!)";
191 }
192 }
193  
194  
195 0 ▶sub TUWF::Object::compile {
Subroutine compile: not called CC=1 SCAR=6.9
196 0 TUWF::Validate::compile($_[0]{_TUWF}{custom_validations}, $_[1]);
197 }
198  
199  
200 0 ▶sub _compile {
Subroutine _compile: not called CC=2 SCAR=17.9
201 0 ▶ ref $_[0] eq 'TUWF::Validate' ? $_[0] : $TUWF::OBJ->compile($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'TUWF::Validate' ? : 0 0 0
202 }
203  
204  
205 0 ▶sub TUWF::Object::validate {
Subroutine validate: not called CC=10 SCAR=47.0
206 0 my $self = shift;
207 0 my $what = shift;
208  
209 0 ▶ return _compile($_[0])->validate($self->reqJSON) if $what eq 'json';
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "json" 0 0 0
210  
211 # 'param' is special, and not really encouraged. Create a new hash based on
212 # reqParam() and cache the result.
213 $self->{_TUWF}{Req}{PARAM} ||= {
214 0 ▶ map { my @v = $self->reqParams($_); +($_, @v > 1 ? \@v : $v[0]) } $self->reqParams()
Branch: 2 branches
BranchTrueFalseTotal
if $what eq "param" 0 0 0
@v > 1 ? : 0 0 0
Condition0%0/2
$self->{"_TUWF"}{"Req"}{"PARAM"} ||= {map({my(@v) = $self->reqParams($_); $_, @v > 1 ? \@v : $v[0];} $self->reqParams)}
l!l
00
MC/DC0%0/1
$self->{"_TUWF"}{"Req"}{"PARAM"} ||= {map({my(@v) = $self->reqParams($_); $_, @v > 1 ? \@v : $v[0];} $self->reqParams)}
$self->{"_TUWF"}{"Req"}{"PARAM"}
Truth table0%0/2
A
A= $self->{"_TUWF"}{"Req"}{"PARAM"}
Aresult
11
00
215 } if $what eq 'param';
216  
217 my $source =
218 $what eq 'get' ? $self->{_TUWF}{Req}{GET} :
219 $what eq 'post' ? $self->{_TUWF}{Req}{POST} :
220 $what eq 'param' ? $self->{_TUWF}{Req}{PARAM}
221 0 ▶ : croak "Invalid source type '$what'";
Branch: 3 branches
BranchTrueFalseTotal
$what eq 'get' ? : 0 0 0
$what eq 'post' ? : 0 0 0
$what eq 'param' ? : 0 0 0
222  
223 # Multi-value, schema hash or object
224 0 ▶ return _compile($_[0])->validate($source) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
225  
226 # Single value
227 0 ▶ return _compile($_[1])->validate($source->{$_[0]}) if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 0 0 0
228  
229 # Multi-value, separate params
230 0 _compile({ type => 'hash', keys => { @_ } })->validate($source);
231 }
232  
233  
234 # Internal function used by other TUWF modules to find an appropriate JSON
235 # module. Kinda like JSON::MaybeXS, but without an extra dependency.
236 0 ▶sub _JSON {
Subroutine _JSON: not called CC=7 SCAR=40.3
237 0 ▶ return 'JSON::XS' if $INC{'JSON/XS.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"JSON/XS.pm"} 0 0 0
238 0 ▶ return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Cpanel/JSON/XS.pm"} 0 0 0
239 0 ▶ return 'JSON::PP' if $INC{'JSON/PP.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"JSON/PP.pm"} 0 0 0
240 0 ▶ return 'JSON::XS' if eval { require JSON::XS; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require JSON::XS; 1 } } 0 0 0
241 0 ▶ return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Cpanel::JSON::XS; 1 } } 0 0 0
242 0 ▶ die "Unable to load a suitable JSON module: $@" if !eval { require JSON::PP; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require JSON::PP; 1 } } 0 0 0
243 0 'JSON::PP'
244 }
245  
246 1 1;
« TUWF/Request.pm ↑ Summary TUWF/XML.pm »