blib/lib/YATT/Lite/Util.pm

filter: Statementstmt 80.2% 719 / 896 Branchbran 61.0% 254 / 416 Conditioncond 59.7% 141 / 236 MC/DCmcdc 41.3% 57 / 138 Subroutinesub 85.5% 154 / 180 Podpod 0.0% 0 / 94 totaltotal 67.6% 1325 / 1960 CC 313 SCAR 27.7
CC
313
Cov
72%
att_value_in
210.0
find_value_in
72.0
encode_query
72.0
CRAP 16.0
« blib/lib/YATT/Lite/LanguageServer.pm ↑ Summary blib/lib/YATT/Lite/Util/FindMethods.pm »
1 package YATT::Lite::Util;
2 124 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 124 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 122 ▶use constant DEBUG_LOOKUP_PATH => $ENV{DEBUG_YATT_UTIL_LOOKUP_PATH};
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 116 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 115 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 115 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 107 require Scalar::Util;
12 107 require File::Spec;
13  
14 {
15 package YATT::Lite::Util;
16 114 ▶ use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 111 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 111 $INC{'YATT/Lite/Util.pm'} = 1;
19 114 our @EXPORT = qw/numLines coalesce default globref symtab lexpand escape
20 untaint_any ckeval ckrequire untaint_unless_tainted
21 dict_sort terse_dump catch
22 nonempty
23 empty
24 subname
25 pkg2pm
26 globref_default
27 dumpout
28 /;
29 }
30 111 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12480 ▶ sub numLines {
Subroutine numLines: called CC=2 SCAR=7.2
Pod undocumented
32 12480 ▶ croak "undefined value for numLines!" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 12480 12480
33 12480 $_[0] =~ tr|\n||;
34 }
35 4217 ▶ sub coalesce {
Subroutine coalesce: called CC=3 SCAR=11.2
Pod undocumented
36 4217 foreach my $item (@_) {
37 8386 ▶ return $item if defined $item;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item 4217 4169 8386
38 }
39 0 undef;
40 }
41 107 *default = \*coalesce;
42  
43 1116 ▶ sub nonempty {
Subroutine nonempty: called CC=2 SCAR=6.9
Pod undocumented
44 1116 ▶ defined $_[0] && $_[0] ne '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] ne "" 633 483 1116
45 }
46 576 ▶ sub empty {
Subroutine empty: called CC=2 SCAR=7.6
Pod undocumented
47 576 ▶ not defined $_[0] or $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
not defined $_[0] or $_[0] eq "" 0 576 576
48 }
49  
50 10435 ▶ sub define_const {
Subroutine define_const: called CC=2 SCAR=7.0
Pod undocumented
51 10435 my ($name_or_glob, $value) = @_;
52 10435 ▶ my $glob = ref $name_or_glob ? $name_or_glob : globref($name_or_glob);
Branch: 1 branch
BranchTrueFalseTotal
ref $name_or_glob ? : 9964 471 10435
53 0 ▶ *$glob = my $const_sub = sub () { $value };
Subroutine __ANON__: not called CC=1 SCAR=1.2
54 10435 $const_sub;
55 }
56  
57 152518 ▶ sub globref {
Subroutine globref: called CC=2 SCAR=6.9
Pod undocumented
58 152518 my ($thing, @name) = @_;
59 152518 ▶ my $class = ref $thing || $thing;
Condition66%2/3
ref $thing || $thing
l!l&&r!l&&!r
99746527720
MC/DC0%0/2
ref $thing || $thing
ref $thing $thing
Truth table66%2/3
A || B
A= ref $thingB= $thing
ABresult
1X1
011
000
60 111 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 # perl -c で定義側モジュールが main になると、ここで作った glob が
62 # "used only once" (FATAL) になるため
63 111 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 152518 \*{join("::", $class, grep {defined} @name)};
65 }
66 583 ▶ sub globref_default {
Subroutine globref_default: called CC=3 SCAR=11.1
Pod undocumented
67 583 ▶ unless (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 2) 0 583 583
68 0 croak "Too few arguments";
69 }
70 583 my ($globref, $default) = @_;
71 583 my $kind = ref $default;
72 583 ▶ *{$globref}{$kind} || do {
Branch: 1 branch
BranchTrueFalseTotal
*{$globref;}{$kind} or do { *{$globref;} = $default; $default } 5 578 583
73 578 *{$globref} = $default;
74 578 $default;
75 };
76 }
77 65306 ▶ sub symtab {
Subroutine symtab: called CC=1 SCAR=0.0
Pod undocumented
78 65306 *{globref(shift, '')}{HASH}
79 65306 }
80 # XXX: Nice to have look_for_symtab, too.
81 59162 ▶ sub look_for_globref {
Subroutine look_for_globref: called CC=2 SCAR=6.9
Pod undocumented
82 59162 my ($class, $name) = @_;
83 59162 my $symtab = symtab($class);
84 59162 ▶ return undef unless defined $symtab->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $symtab->{$name} 591 58571 59162
85 58571 globref($class, $name);
86 }
87 0 ▶ sub call_ns_function_or_default {
Subroutine call_ns_function_or_default: not called CC=3 SCAR=24.8
Pod undocumented
88 0 my ($class, $funcname, $default) = @_;
89 0 my $symtab = symtab($class);
90 0 my ($glob, $code);
91 0 ▶ if (defined ($glob = $symtab->{$funcname})
Branch: 1 branch
BranchTrueFalseTotal
if (defined($glob = $symtab->{$funcname}) and $code = *{$glob;}{'CODE'}) { } 0 0 0
Condition0%0/2
defined($glob = $symtab->{$funcname}) and $code = *{$glob;}{"CODE"}
!ll
00
MC/DC0%0/1
defined($glob = $symtab->{$funcname}) and $code = *{$glob;}{"CODE"}
defined($glob = $symtab->{$funcname})
Truth table0%0/2
A
A= defined($glob = $symtab->{$funcname})
Aresult
00
11
92 0 and ($code = *{$glob}{CODE})) {
93 0 $code->()
94 } else {
95 0 $default;
96 }
97 }
98 0 ▶ sub ns_filename {
Subroutine ns_filename: not called CC=2 SCAR=17.9
Pod undocumented
99 0 my ($ns) = @_;
100 0 ▶ if (my $fn = call_ns_function_or_default($ns, 'filename')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = call_ns_function_or_default($ns, 'filename')) { } 0 0 0
101 0 "$ns \[$fn]"
102 } else {
103 0 $ns;
104 }
105 }
106 52779 ▶ sub fields_hash {
Subroutine fields_hash: called CC=2 SCAR=6.9
Pod undocumented
107 52779 ▶ my $sym = look_for_globref(shift, 'FIELDS')
Branch: 1 branch
BranchTrueFalseTotal
my $sym = look_for_globref(shift(), "FIELDS") or return undef 52740 39 52779
108 or return undef;
109 52740 *{$sym}{HASH};
110 }
111 # XXX: should be renamed to lhexpand
112 17502 ▶ sub lexpand {
Subroutine lexpand: called CC=7 SCAR=20.0
Pod undocumented
113 # lexpand can be used to counting.
114 17502 ▶ unless (defined $_[0]) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $_[0]) { } 12564 4938 17502
if (do { not ref $_[0] }) { } 6173 6391 12564
elsif (ref $_[0] eq 'ARRAY') { } 6378 13 6391
elsif (ref $_[0] eq 'HASH') { } 13 0 13
115 4938 ▶ wantarray ? () : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4914 24 4938
116 } elsif (not ref $_[0]) {
117 6173 $_[0]
118 } elsif (ref $_[0] eq 'ARRAY') {
119 6378 @{$_[0]}
120 } elsif (ref $_[0] eq 'HASH') {
121 13 %{$_[0]}
122 } else {
123 0 ▶ wantarray ? () : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
124 }
125 }
126 0 ▶ sub lsearch (&@) {
Subroutine lsearch: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my $sub = shift;
128 0 my $i = 0;
129 0 foreach (@_) {
130 0 ▶ return $i if $sub->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$sub($_) 0 0 0
131 0 } continue {$i++}
132 0 return;
133 }
134 # $fn:e
135 205 ▶ sub extname { my $fn = shift; return $1 if $fn =~ s/\.(\w+)$// }
Subroutine extname: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $fn =~ s/\.(\w+)$// 205 0 205
136 # $fn:r
137 1671 ▶ sub rootname { my $fn = shift; $fn =~ s/\.\w+$//; join "", $fn, @_ }
Subroutine rootname: called CC=1 SCAR=0.0
Pod undocumented
138 # $fn:r:t
139  
140 # GH-251: trim_ext($fn, $ext_or_list)
141 # Trims one trailing ".$ext" only when $ext is a KNOWN extension.
142 # (cf. cut_ext, which extracts ANY last extension)
143 # This follows Apache mod_mime/mod_negotiation model: "foo.tar.yatt"
144 # minus known ext "yatt" is "foo.tar", and "foo.bar" with unknown
145 # ext is left as-is.
146 29 ▶ sub trim_ext {
Subroutine trim_ext: called CC=5 SCAR=16.1
Pod undocumented
147 29 my ($fn, $ext_or_list) = @_;
148 29 ▶ return undef unless defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fn 1 28 29
149 28 ▶ foreach my $ext (lexpand($ext_or_list // 'yatt')) {
Condition100%2/2
$ext_or_list // "yatt"
l!l
271
MC/DC100%1/1
$ext_or_list // "yatt"
$ext_or_list
Truth table100%2/2
A
A= $ext_or_list
Aresult
11
00
150 29 (my $e = $ext) =~ s/^\.//;
151 29 ▶ return $fn if $fn =~ s/\.\Q$e\E\z//;
Branch: 1 branch
BranchTrueFalseTotal
if $fn =~ s/\.\Q$e\E\z// 22 7 29
152 }
153 6 $fn;
154 }
155 59 ▶ sub appname {
Subroutine appname: called CC=2 SCAR=7.1
Pod undocumented
156 59 my $fn = shift;
157 59 $fn =~ s/\.\w+$//;
158 59 ▶ return $1 if $fn =~ m{(\w+)$};
Branch: 1 branch
BranchTrueFalseTotal
if $fn =~ /(\w+)$/ 59 0 59
159 }
160 1569 ▶ sub untaint_any { $_[0] =~ m{.*}s; $& }
Subroutine untaint_any: called CC=1 SCAR=0.0
Pod undocumented
161 107 our $DEBUG_INJECT_TAINTED = 0;
162 # untaint_unless_tainted($fn, read_file($fn))
163 748 ▶ sub untaint_unless_tainted {
Subroutine untaint_unless_tainted: called CC=5 SCAR=30.0
Pod undocumented
164 748 ▶ return $_[1] unless ${^TAINT};
Branch: 1 branch
BranchTrueFalseTotal
unless ${^TAINT} 748 0 748
165 0 ▶ if (defined $_[0] and not Scalar::Util::tainted($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and not &Scalar::Util::tainted($_[0])) { } 0 0 0
Condition0%0/3
defined $_[0] and not &Scalar::Util::tainted($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and not &Scalar::Util::tainted($_[0])
defined $_[0] not &Scalar::Util::tainted($_[0])
Truth table0%0/3
A and not(B)
A= defined $_[0]B= not &Scalar::Util::tainted($_[0])
ABresult
0X0
100
111
166 0 ▶ $DEBUG_INJECT_TAINTED ? $_[1] : untaint_any($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
$YATT::Lite::Util::DEBUG_INJECT_TAINTED ? : 0 0 0
167 } else {
168 0 $_[1];
169 }
170 }
171 2797 ▶ sub ckeval {
Subroutine ckeval: called CC=9 SCAR=22.0
Pod undocumented
172 my $__SCRIPT__ = join "", grep {
173 2797 ▶ defined $_ and Scalar::Util::tainted($_) ? croak "tainted! '$_'" : 1;
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::tainted($_) ? : 0 9120 9120
Condition33%1/3
defined $_ && (&Scalar::Util::tainted($_) ? croak("tainted! '${_}'") : 1)
!ll&&!rl&&r
009120
MC/DC0%0/2
defined $_ && (&Scalar::Util::tainted($_) ? croak("tainted! '${_}'") : 1)
defined $_ (&Scalar::Util::tainted($_) ? croak("tainted! '${_}'") : 1)
Truth table33%1/3
A && B
A= defined $_B= (&Scalar::Util::tainted($_) ? croak("tainted! '${_}'") : 1)
ABresult
0X0
100
111
174 } @_;
175 2797 my @__RESULT__;
176 2797 ▶ if ($] < 5.014) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.014) { } 0 2797 2797
177 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
178 0 @__RESULT__ = eval $__SCRIPT__;
179 } else {
180 0 $__RESULT__[0] = eval $__SCRIPT__;
181 }
182 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
183 } else {
184 2797 local $@;
185 2797 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2797 2797
186 0 @__RESULT__ = eval $__SCRIPT__;
187 } else {
188 86 ▶ $__RESULT__[0] = eval $__SCRIPT__;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
189 }
190 2797 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 5 2792 2797
191 }
192 2792 ▶ wantarray ? @__RESULT__ : $__RESULT__[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2792 2792
193 }
194 1900 ▶ sub ckrequire {
Subroutine ckrequire: called CC=1 SCAR=0.0
Pod undocumented
195 1900 ckeval("require $_[0]");
196 }
197  
198 #
199 # permissive_require($modName) allows you to load other module without worrying
200 # about their internal use of `eval { require OtherMod }` which misfires the error_handler of
201 # YATT::Lite::WebMVC0::DirApp.
202 #
203 67 ▶ sub permissive_require {
Subroutine permissive_require: called CC=1 SCAR=0.0
Pod undocumented
204 71 local ($SIG{__DIE__}, $SIG{__WARN__});
205 71 ckrequire($_[0]);
206 }
207  
208 #
209 # secure_random_bytes($n) returns $n bytes from the OS CSPRNG, for
210 # session ids and tokens. Never falls back to perl's rand().
211 #
212 107 our $SECURE_RANDOM_SOURCE;
213 16 ▶ sub secure_random_bytes {
Subroutine secure_random_bytes: called CC=5 SCAR=18.8
Pod undocumented
214 20 my ($n) = @_;
215 17 ▶ $SECURE_RANDOM_SOURCE //= do {
Condition66%2/3
$YATT::Lite::Util::SECURE_RANDOM_SOURCE //= do { if (eval { do { permissive_require("Crypt::SysRandom"); 1 } }) { sub { Crypt::SysRandom::random_bytes($_[0]); } ; } elsif (eval { do { permissive_require("Crypt::URandom"); 1 } }) { sub { Crypt::URandom::urandom($_[0]); } ; } elsif (-r "/dev/urandom") { sub { open my $fh, "<:raw", "/dev/urandom" or croak("Can't open /dev/urandom: $!"); my $buf = ""; while (length $buf < $_[0]) { my $got = sysread($fh, $buf, $_[0] - length($buf), length $buf); croak("Can't read /dev/urandom: $!") unless defined $got and $got > 0; } ; $buf; } ; } else { croak("No secure random source! Please install Crypt::SysRandom"); } }
l!l&&r!l&&!r
1610
MC/DC0%0/2
$YATT::Lite::Util::SECURE_RANDOM_SOURCE //= do { if (eval { do { permissive_require("Crypt::SysRandom"); 1 } }) { sub { Crypt::SysRandom::random_bytes($_[0]); } ; } elsif (eval { do { permissive_require("Crypt::URandom"); 1 } }) { sub { Crypt::URandom::urandom($_[0]); } ; } elsif (-r "/dev/urandom") { sub { open my $fh, "<:raw", "/dev/urandom" or croak("Can't open /dev/urandom: $!"); my $buf = ""; while (length $buf < $_[0]) { my $got = sysread($fh, $buf, $_[0] - length($buf), length $buf); croak("Can't read /dev/urandom: $!") unless defined $got and $got > 0; } ; $buf; } ; } else { croak("No secure random source! Please install Crypt::SysRandom"); } }
$YATT::Lite::Util::SECURE_RANDOM_SOURCE do { if (eval { do { permissive_require("Crypt::SysRandom"); 1 } }) { sub { Crypt::SysRandom::random_bytes($_[0]); } ; } elsif (eval { do { permissive_require("Crypt::URandom"); 1 } }) { sub { Crypt::URandom::urandom($_[0]); } ; } elsif (-r "/dev/urandom") { sub { open my $fh, "<:raw", "/dev/urandom" or croak("Can't open /dev/urandom: $!"); my $buf = ""; while (length $buf < $_[0]) { my $got = sysread($fh, $buf, $_[0] - length($buf), length $buf); croak("Can't read /dev/urandom: $!") unless defined $got and $got > 0; } ; $buf; } ; } else { croak("No secure random source! Please install Crypt::SysRandom"); } }
Truth table0%0/3
A //= B
A= $YATT::Lite::Util::SECURE_RANDOM_SOURCEB= do { if (eval { do { permissive_require("Crypt::SysRandom"); 1 } }) { sub { Crypt::SysRandom::random_bytes($_[0]); } ; } elsif (eval { do { permissive_require("Crypt::URandom"); 1 } }) { sub { Crypt::URandom::urandom($_[0]); } ; } elsif (-r "/dev/urandom") { sub { open my $fh, "<:raw", "/dev/urandom" or croak("Can't open /dev/urandom: $!"); my $buf = ""; while (length $buf < $_[0]) { my $got = sysread($fh, $buf, $_[0] - length($buf), length $buf); croak("Can't read /dev/urandom: $!") unless defined $got and $got > 0; } ; $buf; } ; } else { croak("No secure random source! Please install Crypt::SysRandom"); } }
ABresult
1X1
011
000
216 2 ▶ if (eval {permissive_require('Crypt::SysRandom'); 1}) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { permissive_require('Crypt::SysRandom'); 1 } }) { } 1 0 1
elsif (eval { do { permissive_require('Crypt::URandom'); 1 } }) { } 0 0 0
elsif (-r '/dev/urandom') { } 0 0 0
217 1 ▶ sub { Crypt::SysRandom::random_bytes($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
218 0 } elsif (eval {permissive_require('Crypt::URandom'); 1}) {
219 1 ▶ sub { Crypt::URandom::urandom($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=0.0
220 } elsif (-r '/dev/urandom') {
221 1 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=21.7
222 1 ▶ open my $fh, '<:raw', '/dev/urandom'
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", "/dev/urandom" or croak("Can't open /dev/urandom: $!") 0 0 0
223 or croak "Can't open /dev/urandom: $!";
224 1 my $buf = '';
225 1 ▶ while (length $buf < $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf < $_[0]) 0 0 0
226 1 my $got = sysread $fh, $buf, $_[0] - length $buf, length $buf;
227 1 ▶ croak "Can't read /dev/urandom: $!" unless defined $got and $got > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got and $got > 0 0 0 0
Condition33%1/3
defined $got and $got > 0
!ll&&!rl&&r
100
MC/DC0%0/2
defined $got and $got > 0
defined $got $got > 0
Truth table0%0/3
A and B
A= defined $gotB= $got > 0
ABresult
0X0
100
111
228 }
229 1 $buf;
230 1 };
231 } else {
232 1 croak "No secure random source! Please install Crypt::SysRandom";
233 }
234 };
235 17 $SECURE_RANDOM_SOURCE->($n);
236 }
237  
238 17 ▶ sub secure_random_hex {
Subroutine secure_random_hex: called CC=1 SCAR=0.0
Pod undocumented
239 17 my ($nbytes) = @_;
240 17 unpack('H*', secure_random_bytes($nbytes));
241 }
242  
243 111 ▶ use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
244 1581 ▶ sub cached_in {
Subroutine cached_in: called CC=11 SCAR=24.0
Pod undocumented
245 1581 my ($dir, $dict, $nameSpec, $sys, $mark, $loader, $refresher) = @_;
246 1581 my ($name) = lexpand($nameSpec);
247 1581 ▶ if (not exists $dict->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $dict->{$name}) { } 493 1087 1580
248 494 ▶ my $item = $dict->{$name} = $loader ? $loader->($dir, $sys, $nameSpec)
Branch: 1 branch
BranchTrueFalseTotal
$loader ? : 30 463 493
249 : $dir->load($sys, $nameSpec);
250 471 ▶ $mark->{refaddr($item)} = 1 if $item and $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $item and $mark 336 134 470
Condition100%3/3
$item and $mark
!ll&&!rl&&r
1304336
MC/DC100%2/2
$item and $mark
$item $mark
Truth table100%3/3
A and B
A= $itemB= $mark
ABresult
0X0
100
111
251 471 $item;
252 } else {
253 1087 my $item = $dict->{$name};
254 1087 ▶ unless ($item and ref $item
Branch: 3 branches
BranchTrueFalseTotal
if ($item and ref $item and not $mark && $$mark{builtin::refaddr($item)}++) { } 496 591 1087
if (do { $refresher }) { } 90 406 496
elsif (my $sub = UNIVERSAL::can($item, 'refresh')) { } 402 4 406
Condition100%9/9
$item and ref $item and not $mark && $$mark{builtin::refaddr($item)}++
!ll&&!rl&&r
487104496
$item and ref $item
!ll&&!rl&&r
237250600
$mark && $$mark{builtin::refaddr($item)}++
!ll&&!rl&&r
90406104
MC/DC100%4/4
$item and ref $item and not $mark && $$mark{builtin::refaddr($item)}++
$item ref $item $mark $$mark{builtin::refaddr($item)}++
Truth table100%5/5
A and B and not(C && D)
A= $itemB= ref $itemC= $markD= $$mark{builtin::refaddr($item)}++
ABCDresult
0XXX0
10XX0
11110
110X1
11101
255 and (not $mark or not $mark->{refaddr($item)}++)) {
256 # nop
257 } elsif ($refresher) {
258 90 $refresher->($item, $sys, $nameSpec)
259 } elsif (my $sub = UNIVERSAL::can($item, 'refresh')) {
260 402 $sub->($item, $sys);
261 }
262 1087 $item;
263 }
264 }
265  
266 55 ▶ sub split_path {
Subroutine split_path: called CC=18 SCAR=29.2
Pod undocumented
267 54 my ($path, $startDir, $cut_depth, $default_ext) = @_;
268 # $startDir is $app_root.
269 # $doc_root should resides under $app_root.
270 54 ▶ $cut_depth //= 1;
Condition50%1/2
$cut_depth //= 1
l!l
540
MC/DC0%0/1
$cut_depth //= 1
$cut_depth
Truth table50%1/2
A
A= $cut_depth
Aresult
11
00
271 54 ▶ $default_ext //= "yatt";
Condition100%2/2
$default_ext //= "yatt"
l!l
3519
MC/DC100%1/1
$default_ext //= "yatt"
$default_ext
Truth table100%2/2
A
A= $default_ext
Aresult
11
00
272 54 $startDir =~ s,/+$,,;
273 54 ▶ unless ($path =~ m{^\Q$startDir\E}gxs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ /^\Q$startDir\E/gsx) 0 54 54
274 0 die "Can't split_path: prefix mismatch: $startDir vs $path";
275 }
276  
277 54 my ($dir, $pos, $file) = ($startDir, pos($path));
278 # *DO NOT* initialize $file. This loop relies on undefined-ness of $file.
279 54 ▶ while ($path =~ m{\G/+([^/]*)}gcxs and -e "$dir/$1" and not defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[\G/+([^/]*)]cgsx and -e "$dir/$1" and not defined $file) 109 54 163
Condition83%5/6
$path =~ m[\G/+([^/]*)]cgsx and -e "$dir/$1" and not defined $file
!ll&&!rl&&r
540109
$path =~ m[\G/+([^/]*)]cgsx and -e "$dir/$1"
!ll&&!rl&&r
3915109
MC/DC66%2/3
$path =~ m[\G/+([^/]*)]cgsx and -e "$dir/$1" and not defined $file
$path =~ m[\G/+([^/]*)]cgsx -e "$dir/$1" not defined $file
Truth table75%3/4
A and B and not(C)
A= $path =~ m[\G/+([^/]*)]cgsxB= -e "$dir/$1"C= not defined $file
ABCresult
0XX0
10X0
1100
1111
280 109 ▶ if (-d _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d _) { } 64 45 109
281 64 $dir .= "/$1";
282 } else {
283 45 $file = $1;
284 # *DO NOT* last. To match one more time.
285 }
286 } continue {
287 109 $pos = pos($path);
288 }
289  
290 54 ▶ $dir .= "/" if $dir !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ m[/$] 54 0 54
291 54 my $subpath = substr($path, $pos);
292 # GH-251: $request_file keeps the file name part as it appeared
293 # in $path (empty when the name is totally omitted).
294 54 my $request_file;
295 54 ▶ if (not defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file) { } 9 45 54
296 9 ▶ if ($subpath =~ m{^/(\w+)(?:/|$)} and -e "$dir/$1.$default_ext") {
Branch: 3 branches
BranchTrueFalseTotal
if ($subpath =~ m[^/(\w+)(?:/|$)] and -e "$dir/$1.$default_ext") { } 7 2 9
elsif (-e "$dir/index.$default_ext") { } 1 1 2
elsif ($subpath =~ s[^/([^/]+)$][]) { } 1 0 1
Condition66%2/3
$subpath =~ m[^/(\w+)(?:/|$)] and -e "$dir/$1.$default_ext"
!ll&&!rl&&r
207
MC/DC50%1/2
$subpath =~ m[^/(\w+)(?:/|$)] and -e "$dir/$1.$default_ext"
$subpath =~ m[^/(\w+)(?:/|$)] -e "$dir/$1.$default_ext"
Truth table66%2/3
A and B
A= $subpath =~ m[^/(\w+)(?:/|$)]B= -e "$dir/$1.$default_ext"
ABresult
0X0
100
111
297 7 $subpath = substr($subpath, 1+length $1);
298 7 $request_file = $1;
299 7 $file = "$1.$default_ext";
300 } elsif (-e "$dir/index.$default_ext") {
301 # index.yatt should subsume all subpath.
302 } elsif ($subpath =~ s{^/([^/]+)$}{}) {
303 # Note: Actually, $file is not accesible in this case.
304 # This is just for better error diag.
305 1 $file = $request_file = $1;
306 }
307 } else {
308 45 $request_file = $file;
309 }
310  
311 54 my $loc = substr($dir, length($startDir));
312 54 ▶ while ($cut_depth-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cut_depth-- > 0) 52 54 106
313 52 ▶ $loc =~ s,^/[^/]+,,
Branch: 1 branch
BranchTrueFalseTotal
$loc =~ s[^/[^/]+][] or croak("Can't cut path location: $loc") 52 0 52
314 or croak "Can't cut path location: $loc";
315 52 $startDir .= $&;
316 }
317  
318 54 ▶ ($startDir
Condition100%4/4
$file // ""
l!l
531
$request_file // ""
l!l
531
MC/DC100%1/1
$file // ""
$file
MC/DC100%1/1
$request_file // ""
$request_file
Truth table100%2/2
A
A= $file
Aresult
11
00
Truth table100%2/2
A
A= $request_file
Aresult
11
00
319 , $loc
320 , $file // ''
321 , $subpath
322 , (not defined $file)
323 , $request_file // ''
324 );
325 }
326  
327 988 ▶ sub lookup_dir {
Subroutine lookup_dir: called CC=4 SCAR=14.2
Pod undocumented
328 988 my ($loc, $dirlist) = @_;
329 988 $loc =~ s{^/*}{/};
330 988 foreach my $dir (@$dirlist) {
331 988 my $real = "$dir$loc";
332 988 ▶ next unless -d $real;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $real 0 988 988
333 988 ▶ return wantarray ? ($real, $dir) : $real;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 988 0 988
334 }
335 }
336  
337 676 ▶ sub lookup_path {
Subroutine lookup_path: called CC=19 SCAR=29.5
Pod undocumented
338 676 my ($path_info, $dirlist, $index_name, $ext_list, $use_subpath) = @_;
339 676 ▶ $index_name //= 'index';
Condition100%2/2
$index_name //= "index"
l!l
63838
MC/DC100%1/1
$index_name //= "index"
$index_name
Truth table100%2/2
A
A= $index_name
Aresult
11
00
340 676 my ($ext1, @ext) = lexpand($ext_list);
341 676 ▶ $ext1 //= "yatt";
Condition100%2/2
$ext1 //= "yatt"
l!l
63838
MC/DC100%1/1
$ext1 //= "yatt"
$ext1
Truth table100%2/2
A
A= $ext1
Aresult
11
00
342 676 my $ixfn = $index_name . ".$ext1";
343 676 ▶ my @dirlist = grep {defined $_ and -d $_} @$dirlist;
Condition33%1/3
defined $_ && -d $_
!ll&&!rl&&r
00714
MC/DC0%0/2
defined $_ && -d $_
defined $_ -d $_
Truth table33%1/3
A && B
A= defined $_B= -d $_
ABresult
0X0
100
111
344 676 print STDERR "dirlist" => terse_dump(@dirlist), "\n" if DEBUG_LOOKUP_PATH;
345 676 my $pi = normalize_path($path_info);
346 676 my ($loc, $cur, $ext) = ("", "");
347 DIG:
348 676 ▶ while ($pi =~ s{^/+([^/]+)}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pi =~ s[^/+([^/]+)][]) 617 161 778
349 617 $cur = $1;
350 617 ▶ $ext = ($cur =~ s/(\.[^\.]+)$// ? $1 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$cur =~ s/(\.[^\.]+)$// ? : 63 554 617
351 617 print STDERR terse_dump(cur => $cur, ext => $ext), "\n" if DEBUG_LOOKUP_PATH;
352 617 foreach my $dir (@dirlist) {
353 645 my $base = "$dir$loc/$cur";
354 645 ▶ if (defined $ext and -r "$base$ext") {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ext and -r "$base$ext") { } 58 587 645
elsif ($pi =~ m[^/] and -d $base) { } 97 490 587
Condition100%3/3
defined $ext and -r "$base$ext"
!ll&&!rl&&r
579858
MC/DC100%2/2
defined $ext and -r "$base$ext"
defined $ext -r "$base$ext"
Truth table100%3/3
A and B
A= defined $extB= -r "$base$ext"
ABresult
0X0
100
111
355 # If extension is specified and it is readable, use it.
356 58 return ($dir, "$loc/", "$cur$ext", $pi, 0, "$cur$ext");
357 ▶ } elsif ($pi =~ m{^/} and -d $base) {
Condition100%3/3
$pi =~ m[^/] and -d $base
!ll&&!rl&&r
4088297
MC/DC100%2/2
$pi =~ m[^/] and -d $base
$pi =~ m[^/] -d $base
Truth table100%3/3
A and B
A= $pi =~ m[^/]B= -d $base
ABresult
0X0
100
111
358 # path_info has '/' and directory exists.
359 97 next; # candidate
360 } else {
361 490 foreach my $want_ext ($ext1, @ext) {
362 615 ▶ if (-r (my $fn = "$base.$want_ext")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r (my $fn = "$base.$want_ext")) 292 323 615
363 # GH-251: request_file must be a dot-boundary prefix of
364 # the resolved file name (Apache MultiViews model), so
365 # that only substrings of real file names can flow into
366 # url reconstruction. Thus a mismatching requested ext
367 # (e.g. request "foo.html" resolved to "foo.yatt") is
368 # NOT reproduced here; $cur is used instead.
369 292 return ($dir, "$loc/", "$cur.$want_ext", $pi, 0, $cur);
370 }
371 }
372 198 ▶ if ($use_subpath
Branch: 1 branch
BranchTrueFalseTotal
if ($use_subpath and -r (my $alt = "$dir$loc/$ixfn")) { } 165 33 198
Condition100%3/3
$use_subpath and -r (my $alt = "$dir$loc/$ixfn")
!ll&&!rl&&r
321165
MC/DC100%2/2
$use_subpath and -r (my $alt = "$dir$loc/$ixfn")
$use_subpath -r (my $alt = "$dir$loc/$ixfn")
Truth table100%3/3
A and B
A= $use_subpathB= -r (my $alt = "$dir$loc/$ixfn")
ABresult
0X0
100
111
373 and -r (my $alt = "$dir$loc/$ixfn")) {
374 165 ▶ $ext //= "";
Condition50%1/2
$ext //= ""
l!l
0165
MC/DC0%0/1
$ext //= ""
$ext
Truth table50%1/2
A
A= $ext
Aresult
11
00
375 165 return ($dir, "$loc/", $ixfn, "/$cur$ext$pi", 1, '');
376 } else {
377 # Neither dir nor $cur$want_ext exists, it should be ignored.
378 33 undef $dir;
379 }
380 }
381 }
382 } continue {
383 102 $loc .= "/$cur";
384 102 print STDERR terse_dump(continuing => $loc), "\n" if DEBUG_LOOKUP_PATH;
385 102 @dirlist = grep {defined} @dirlist;
386 }
387 161 print STDERR terse_dump('end_of_loop'), "\n" if DEBUG_LOOKUP_PATH;
388  
389 161 ▶ return unless $pi =~ m{^/+$};
Branch: 1 branch
BranchTrueFalseTotal
unless $pi =~ m[^/+$] 5 156 161
390  
391 156 foreach my $dir (@dirlist) {
392 157 ▶ next unless -r "$dir$loc/$ixfn";
Branch: 1 branch
BranchTrueFalseTotal
unless -r "$dir$loc/$ixfn" 1 156 157
393 156 return ($dir, "$loc/", "$ixfn", "", 1, '');
394 }
395  
396 0 print STDERR terse_dump('at_last'), "\n" if DEBUG_LOOKUP_PATH;
397 0 return;
398 }
399  
400 # Shamelessly stolen (with slight mod) from Dancer2::FileUtils::normalize_path
401 107 our $seqregex = qr{
402 [^/]* # anything without a slash
403 /\.\.(/|\z) # that is accompanied by two dots as such
404 }x;
405 685 ▶ sub normalize_path {
Subroutine normalize_path: called CC=3 SCAR=11.0
Pod undocumented
406  
407 # this is a revised version of what is described in
408 # http://www.linuxjournal.com/content/normalizing-path-names-bash
409 # by Mitch Frazier
410 685 ▶ my $path = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or return 685 0 685
411  
412 685 $path =~ s{/\./}{/}g;
413 685 ▶ 1 while $path =~ s{$seqregex}{};
Branch: 1 branch
BranchTrueFalseTotal
$path =~ s/$YATT::Lite::Util::seqregex// and do { '???' } 15 685 700
414  
415 #see https://rt.cpan.org/Public/Bug/Display.html?id=80077
416 685 $path =~ s{^//}{/};
417 685 return $path;
418 }
419  
420 # GH-260: lexical normalization for filesystem paths (app_root and its
421 # relatives). Unlike normalize_path above (PATH_INFO semantics, where a
422 # trailing slash is significant), this collapses "." and ".." segments and
423 # strips trailing slashes. Symlinks are deliberately not resolved, so a
424 # deployment reached through a symlinked path keeps its spelling.
425 679 ▶ sub normalize_fs_path {
Subroutine normalize_fs_path: called CC=12 SCAR=25.1
Pod undocumented
426 679 my ($path) = @_;
427 679 ▶ return $path unless defined $path and $path ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path ne "" 0 679 679
Condition33%1/3
defined $path and $path ne ""
!ll&&!rl&&r
00679
MC/DC0%0/2
defined $path and $path ne ""
defined $path $path ne ""
Truth table33%1/3
A and B
A= defined $pathB= $path ne ""
ABresult
0X0
100
111
428 679 my $abs = substr($path, 0, 1) eq '/';
429 679 my @out;
430 679 foreach my $seg (split '/', $path) {
431 3703 ▶ next if $seg eq '' or $seg eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $seg eq "" or $seg eq "." 681 3022 3703
Condition100%3/3
$seg eq "" or $seg eq "."
l!l&&r!l&&!r
68013022
MC/DC100%2/2
$seg eq "" or $seg eq "."
$seg eq "" $seg eq "."
Truth table100%3/3
A or B
A= $seg eq ""B= $seg eq "."
ABresult
1X1
011
000
432 3022 ▶ if ($seg eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg eq "..") 21 3001 3022
433 21 ▶ if (@out and $out[-1] ne '..') { pop @out; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (@out and $out[-1] ne "..") 19 2 21
Condition66%2/3
@out and $out[-1] ne ".."
!ll&&!rl&&r
2019
MC/DC50%1/2
@out and $out[-1] ne ".."
@out $out[-1] ne ".."
Truth table66%2/3
A and B
A= @outB= $out[-1] ne ".."
ABresult
0X0
100
111
434 2 ▶ next if $abs; # POSIX: "/.." means "/".
Branch: 1 branch
BranchTrueFalseTotal
if $abs 1 1 2
435 }
436 3002 push @out, $seg;
437 }
438 679 my $result = join('/', @out);
439 679 ▶ $abs ? "/$result" : ($result ne '' ? $result : '.');
Branch: 2 branches
BranchTrueFalseTotal
$abs ? : 678 1 679
$result ne '' ? : 1 0 1
440 }
441  
442  
443 30 ▶ sub trim_common_suffix_from {
Subroutine trim_common_suffix_from: called CC=5 SCAR=16.7
Pod undocumented
444 30 ▶ @_ == 2 or Carp::croak "trim_common_suffix_from(FROM, COMPARE)";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or Carp::croak("trim_common_suffix_from(FROM, COMPARE)") 30 0 30
445 30 my @from = File::Spec->splitdir($_[0]);
446 30 my @comp = File::Spec->splitdir($_[1]);
447 30 ▶ while (@from and @comp and $from[-1] eq $comp[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from and @comp and $from[-1] eq $comp[-1]) 60 30 90
Condition50%3/6
@from and @comp and $from[-1] eq $comp[-1]
!ll&&!rl&&r
03060
@from and @comp
!ll&&!rl&&r
0090
MC/DC33%1/3
@from and @comp and $from[-1] eq $comp[-1]
@from @comp $from[-1] eq $comp[-1]
Truth table50%2/4
A and B and C
A= @fromB= @compC= $from[-1] eq $comp[-1]
ABCresult
0XX0
10X0
1100
1111
448 60 pop @from; pop @comp;
449 }
450 30 File::Spec->catfile(@from);
451 }
452  
453 5 ▶ sub dict_order {
Subroutine dict_order: called CC=7 SCAR=20.6
Pod undocumented
454 5 my ($a, $b, $start) = @_;
455 5 ▶ $start = 1 unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 5 0 5
456 5 my ($result, $i) = (0);
457 5 ▶ for ($i = $start; $i <= $#$a and $i <= $#$b; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $a->$#* and $i <= $b->$#*) 40 0 40
Condition33%1/3
$i <= $a->$#* and $i <= $b->$#*
!ll&&!rl&&r
0040
MC/DC0%0/2
$i <= $a->$#* and $i <= $b->$#*
$i <= $a->$#* $i <= $b->$#*
Truth table33%1/3
A and B
A= $i <= $a->$#*B= $i <= $b->$#*
ABresult
0X0
100
111
458 40 ▶ if ($a->[$i] =~ /^\d/ and $b->[$i] =~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->[$i] =~ /^\d/ and $b->[$i] =~ /^\d/) { } 20 20 40
Condition66%2/3
$a->[$i] =~ /^\d/ and $b->[$i] =~ /^\d/
!ll&&!rl&&r
20020
MC/DC50%1/2
$a->[$i] =~ /^\d/ and $b->[$i] =~ /^\d/
$a->[$i] =~ /^\d/ $b->[$i] =~ /^\d/
Truth table66%2/3
A and B
A= $a->[$i] =~ /^\d/B= $b->[$i] =~ /^\d/
ABresult
0X0
100
111
459 20 $result = $a->[$i] <=> $b->[$i];
460 } else {
461 20 $result = $a->[$i] cmp $b->[$i];
462 }
463 40 ▶ return $result unless $result == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $result == 0 5 35 40
464 }
465 0 return $#$a <=> $#$b;
466 }
467  
468 # a => ['a', 'a']
469 # q1a => ['q1a', 'q', 1, 'a']
470 # q11b => ['q11b', 'q', 11, 'b']
471 2 ▶ sub dict_sort (@) {
Subroutine dict_sort: called CC=1 SCAR=0.0
Pod undocumented
472 2 map {$_->[0]} sort {dict_order($a,$b)} map {[$_, split /(\d+)/]} @_;
473 }
474 0 ▶ sub dict_sort_by_nth ($@) {
Subroutine dict_sort_by_nth: not called CC=1 SCAR=6.9
Pod undocumented
475 0 my $nth = shift;
476 0 map {$_->[0]}
477 0 sort {dict_order($a,$b)}
478 0 map {[$_, split /(\d+)/, $$_[$nth]]} @_;
479 }
480  
481 3 ▶ sub combination (@) {
Subroutine combination: called CC=1 SCAR=0.0
Pod undocumented
482 3 ▶ my $comb; $comb = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
483 9 my $prefix = shift;
484 9 ▶ return $prefix unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 9 9
485 9 my ($list, @rest) = @_;
486 9 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) { } 3 6 9
487 3 map {$comb->([@$prefix, $_], @rest)} @$list;
488 } else {
489 6 map {[@$prefix, $_]} @$list;
490 }
491 3 };
492 3 $comb->([], @_);
493 }
494  
495 12 ▶ sub captured (&;$) {
Subroutine captured: called CC=4 SCAR=14.2
Pod undocumented
496 12 my ($code, $keep_utf8) = @_;
497 12 my $buffer = "";
498 {
499 12 ▶ open my $fh, '>:utf8', \ $buffer
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:utf8", \$buffer or die "Can't create capture buf:$!" 12 0 12
500 or die "Can't create capture buf:$!";
501 12 $code->($fh);
502 }
503 11 ▶ if ($keep_utf8 // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_utf8 // 1) 11 0 11
Condition50%1/2
$keep_utf8 // 1
l!l
011
MC/DC0%0/1
$keep_utf8 // 1
$keep_utf8
Truth table50%1/2
A
A= $keep_utf8
Aresult
11
00
504 11 Encode::_utf8_on($buffer);
505 }
506 11 $buffer;
507 }
508  
509 264 ▶ sub terse_dump {
Subroutine terse_dump: called CC=1 SCAR=0.0
Pod undocumented
510 264 require Data::Dumper;
511 join ", ", map {
512 264 Data::Dumper->new([$_])->Terse(1)->Indent(0)->Sortkeys(1)->Dump;
513 } @_;
514 }
515 20 ▶ sub indented_dump {
Subroutine indented_dump: called CC=1 SCAR=0.0
Pod undocumented
516 20 require Data::Dumper;
517 join ", ", map {
518 20 Data::Dumper->new([$_])->Terse(1)->Indent(1)->Sortkeys(1)->Dump;
519 } @_;
520 }
521  
522 389 ▶ sub is_debugging {
Subroutine is_debugging: called CC=2 SCAR=7.0
Pod undocumented
523 389 ▶ my $symtab = $main::{'DB::'} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $symtab = $main::main::{"DB::"} or return 0 389 0 389
524 389 defined ${*{$symtab}{HASH}}{cmd_b}
525 389 }
526  
527 1116 ▶ sub catch (&) {
Subroutine catch: called CC=1 SCAR=0.0
Pod undocumented
528 1116 my ($sub) = @_;
529 1116 local $@ = '';
530 1116 eval { $sub->() };
531 1116 $@;
532 }
533 }
534  
535 107 ▶sub add_entity_into {
Subroutine add_entity_into: called CC=3 SCAR=11.0
Pod undocumented
536 2070 my ($pack, $destns, $name, $sub, $allow_ignore) = @_;
537 2070 my $longname = join "::", $destns, "entity_$name";
538 2070 my $glob = globref($destns, "entity_$name");
539 2070 ▶ if (*{$glob}{CODE} and $allow_ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if (*{$glob;}{"CODE"} and $allow_ignore) 8 2062 2070
Condition66%2/3
*{$glob;}{"CODE"} and $allow_ignore
!ll&&!rl&&r
206208
MC/DC50%1/2
*{$glob;}{"CODE"} and $allow_ignore
*{$glob;}{"CODE"} $allow_ignore
Truth table66%2/3
A and B
A= *{$glob;}{"CODE"}B= $allow_ignore
ABresult
0X0
100
111
540 8 return;
541 }
542 2062 subname($longname, $sub);
543 2062 *$glob = $sub;
544 }
545  
546 53 ▶sub dofile_in {
Subroutine dofile_in: called CC=3 SCAR=14.2
Pod undocumented
547 53 my ($pkg, $file) = @_;
548 53 ▶ unless (-e $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $file) { } 53 0 53
if (do { not -r _ }) 0 53 53
549 0 croak "No such file: $file\n";
550 0 } elsif (not -r _) {
551 0 croak "Can't read file: $file\n";
552 }
553 53 ckeval("package $pkg; my \$result = do '$file'; die \$\@ if \$\@; \$result");
554 }
555  
556 7 ▶sub compile_file_in {
Subroutine compile_file_in: called CC=4 SCAR=16.6
Pod undocumented
557 7 my ($pkg, $file) = @_;
558 7 ▶ if (-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 0 7 7
559 0 croak "file '$file' is a directory!";
560 }
561 7 my $sub = dofile_in($pkg, $file);
562 5 ▶ unless (defined $sub and ref $sub eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub and ref $sub eq "CODE") 0 5 5
Condition33%1/3
defined $sub and ref $sub eq "CODE"
!ll&&!rl&&r
005
MC/DC0%0/2
defined $sub and ref $sub eq "CODE"
defined $sub ref $sub eq "CODE"
Truth table33%1/3
A and B
A= defined $subB= ref $sub eq "CODE"
ABresult
0X0
100
111
563 0 die "file '$file' should return CODE (but not)!\n";
564 }
565 5 $sub;
566 }
567  
568  
569 111 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
570 111 my %escape = (qw(< &lt;
571 > &gt;
572 --> --&gt;
573 " &quot;
574 & &amp;)
575 , "\'", "&#39;");
576  
577 114 our $ESCAPE_UNDEF = '';
578  
579 1059 ▶ sub escape {
Subroutine escape: called CC=14 SCAR=29.6
Pod undocumented
580 1059 ▶ return if wantarray && !@_;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray and not @_ 1 1058 1059
Condition100%3/3
wantarray and not @_
!ll&&!rl&&r
3210261
MC/DC100%2/2
wantarray and not @_
wantarray not @_
Truth table100%3/3
A and not(B)
A= wantarrayB= not @_
ABresult
0X0
100
111
581 1058 my @result;
582 1058 foreach my $str (@_) {
583 1058 push @result, do {
584 1058 ▶ unless (defined $str) {
Branch: 6 branches
BranchTrueFalseTotal
if (defined $str) { } 993 65 1058
if (do { not ref $str }) { } 934 59 993
elsif (ref $str eq 'SCALAR') { } 16 43 59
elsif (_is_escapable($str)) { } 7 36 43
elsif (my $sub = UNIVERSAL::can($str, 'cf_pairs')) { } 0 36 36
elsif (escape_may_dump_ref()) { } 20 16 36
585 65 $ESCAPE_UNDEF;
586 } elsif (not ref $str) {
587 934 my $copy = $str;
588 934 $copy =~ s{([<>&\"\'])}{$escape{$1}}g;
589 934 $copy;
590 } elsif (ref $str eq 'SCALAR') {
591 # PASS Thru. (Already escaped)
592 16 ▶ $$str // $ESCAPE_UNDEF; # fail safe
Condition33%1/3
$$str // $YATT::Lite::Util::ESCAPE_UNDEF
l!l&&r!l&&!r
1600
MC/DC0%0/2
$$str // $YATT::Lite::Util::ESCAPE_UNDEF
$$str $YATT::Lite::Util::ESCAPE_UNDEF
Truth table33%1/3
A // B
A= $$strB= $YATT::Lite::Util::ESCAPE_UNDEF
ABresult
1X1
011
000
593 } elsif (_is_escapable($str)) {
594 7 $str->as_escaped;
595 } elsif (my $sub = UNIVERSAL::can($str, 'cf_pairs')) {
596 ref($str).'->new('.(join(", ", map {
597 0 my ($k, $v) = @$_;
598 0 "$k => " . do {
599 0 my $esc = escape($v);
600 0 ▶ if (not defined $esc) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $esc) { } 0 0 0
elsif ($esc eq '') { } 0 0 0
601 0 'undef'
602 } elsif ($esc eq '') {
603 0 "''"
604 } else {
605 0 $esc;
606 }
607 };
608 } $sub->($str))).')';
609 } elsif (escape_may_dump_ref()) {
610 # Development aid: show the structure, fully escaped (inside <pre>
611 # the browser renders the entities back, so readability is kept).
612 # Use &yatt:dump() when you really mean to print a structure.
613 20 my $copy = indented_dump($str);
614 20 $copy =~ s{([<>&\"\'])}{$escape{$1}}g;
615 20 $copy;
616 } else {
617 # Production: never leak the contents of a structure that was
618 # (probably by accident) printed as text. Type name only.
619 16 '[' . escape(ref $str) . ']';
620 }
621 };
622 }
623 1058 ▶ wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1026 32 1058
624 }
625  
626 #
627 # Whether escape() may dump a reference. Decided per request through
628 # $CON->has_htdebug('dump') (cached in the connection), which consults
629 # $ENV{HTDEBUG_DUMP} and $app_root/.htdebug_dump. Without either,
630 # dumping is allowed only under PLACK_ENV=development.
631 #
632 36 ▶ sub escape_may_dump_ref {
Subroutine escape_may_dump_ref: called CC=8 SCAR=20.8
Pod undocumented
633 36 ▶ if (exists $ENV{HTDEBUG_DUMP}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"HTDEBUG_DUMP"}) 19 17 36
634 19 ▶ return $ENV{HTDEBUG_DUMP} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HTDEBUG_DUMP'} ? : 15 4 19
635 }
636 17 ▶ if (my $con = $YATT::Lite::Entities::CON) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $con = $YATT::Lite::Entities::CON) 15 2 17
637 15 ▶ if ($con->can('has_htdebug') and $con->has_htdebug('dump')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($con->can("has_htdebug") and $con->has_htdebug("dump")) 1 14 15
Condition66%2/3
$con->can("has_htdebug") and $con->has_htdebug("dump")
!ll&&!rl&&r
0141
MC/DC50%1/2
$con->can("has_htdebug") and $con->has_htdebug("dump")
$con->can("has_htdebug") $con->has_htdebug("dump")
Truth table66%2/3
A and B
A= $con->can("has_htdebug")B= $con->has_htdebug("dump")
ABresult
0X0
100
111
638 1 return 1;
639 }
640 }
641 16 ▶ ($ENV{PLACK_ENV} // '') eq 'development' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($ENV{'PLACK_ENV'} // '') eq 'development' ? : 4 12 16
Condition100%2/2
$ENV{"PLACK_ENV"} // ""
l!l
97
MC/DC100%1/1
$ENV{"PLACK_ENV"} // ""
$ENV{"PLACK_ENV"}
Truth table100%2/2
A
A= $ENV{"PLACK_ENV"}
Aresult
11
00
642 }
643 }
644  
645 # XXX: Since method name "as_escaped" conflicts with CGen::Perl->as_escaped,
646 # We need a informational class for everything safely escapable
647 # via "as_escape()"
648 {
649 43 ▶ sub _is_escapable {
Subroutine _is_escapable: called CC=1 SCAR=0.0
650 43 UNIVERSAL::isa($_[0], 'YATT::Lite::Util::escapable');
651 }
652 package
653 YATT::Lite::Util::escapable;
654 }
655  
656 {
657 107 package
658 YATT::Lite::Util::named_attr;
659 111 ▶ BEGIN {our @ISA = ('YATT::Lite::Util::escapable')};
Subroutine BEGIN: called CC=1 SCAR=0.0
660 111 ▶ use overload qw("" as_string eq streq ne strne);
Subroutine BEGIN: called CC=1 SCAR=0.0
661 0 ▶ sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
662 0 shift->[-1];
663 }
664 7 ▶ sub as_escaped {
Subroutine as_escaped: called CC=1 SCAR=0.0
665 7 sprintf q{ %s="%s"}, $_[0][0], $_[0][1];
666 }
667 0 ▶ sub streq {
Subroutine streq: not called CC=2 SCAR=17.9
668 0 my ($self, $other) = @_;
669 0 ▶ $self->[-1] eq (ref $other ? $other->[-1] : $other);
Branch: 1 branch
BranchTrueFalseTotal
ref $other ? : 0 0 0
670 }
671 0 ▶ sub strne {
Subroutine strne: not called CC=2 SCAR=17.9
672 0 my ($self, $other) = @_;
673 0 ▶ $self->[-1] ne (ref $other ? $other->[-1] : $other);
Branch: 1 branch
BranchTrueFalseTotal
ref $other ? : 0 0 0
674 }
675 }
676  
677 107 ▶sub named_attr {
Subroutine named_attr: called CC=3 SCAR=11.0
Pod undocumented
678 11 my $attname = shift;
679 11 ▶ my @result = grep {defined $_ && $_ ne ''} @_;
Condition100%3/3
defined $_ && $_ ne ''
!ll&&!rl&&r
4110
MC/DC100%2/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table100%3/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
680 11 ▶ return '' unless @result;
Branch: 1 branch
BranchTrueFalseTotal
unless @result 4 7 11
681 7 bless [$attname, join ' ', map {escape($_)} @result]
682 , 'YATT::Lite::Util::named_attr';
683 }
684  
685 {
686 # XXX: These functions are deprecated. Use att_value_in() instead.
687  
688 0 ▶ sub value_checked { _value_checked($_[0], $_[1], $_[2], checked => '') }
Subroutine value_checked: not called CC=1 SCAR=6.9
Pod undocumented
689 0 ▶ sub value_selected { _value_checked($_[0], $_[1], $_[2], selected => '') }
Subroutine value_selected: not called CC=1 SCAR=6.9
Pod undocumented
690  
691 0 ▶ sub _value_checked {
Subroutine _value_checked: not called CC=4 SCAR=30.0
692 0 my ($value, $hashOrArray, $isDefault, $then, $else) = @_;
693 0 sprintf q|value="%s"%s|, escape($value), do {
694 0 ▶ if (_if_checked($hashOrArray, $value) // $isDefault) {
Branch: 1 branch
BranchTrueFalseTotal
if (_if_checked($hashOrArray, $value) // $isDefault) { } 0 0 0
Condition0%0/3
_if_checked($hashOrArray, $value) // $isDefault
l!l&&r!l&&!r
000
MC/DC0%0/2
_if_checked($hashOrArray, $value) // $isDefault
_if_checked($hashOrArray, $value) $isDefault
Truth table0%0/3
A // B
A= _if_checked($hashOrArray, $value)B= $isDefault
ABresult
1X1
011
000
695 0 " $then"
696 } else {
697 0 ▶ $else // ''
Condition0%0/2
$else // ""
l!l
00
MC/DC0%0/1
$else // ""
$else
Truth table0%0/2
A
A= $else
Aresult
11
00
698 }
699 };
700 }
701  
702 0 ▶ sub _if_checked {
Subroutine _if_checked: not called CC=6 SCAR=37.4
703 0 my ($in, $value) = @_;
704 0 ▶ if (not defined $in) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $in) { } 0 0 0
elsif (not ref $in) { } 0 0 0
elsif (ref $in eq 'HASH') { } 0 0 0
elsif (ref $in eq 'ARRAY') { } 0 0 0
705 undef
706 0 } elsif (not ref $in) {
707 0 ($in eq $value)
708 } elsif (ref $in eq 'HASH') {
709 0 ▶ $in->{$value // ''}
Condition0%0/2
$value // ""
l!l
00
MC/DC0%0/1
$value // ""
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
710 } elsif (ref $in eq 'ARRAY') {
711 0 grep {$value eq $_} @$in
712 } else {
713 undef
714 0 }
715 }
716 }
717  
718 {
719 107 our %input_spec = (select => [0, 0]
720 , radio => [1, 0]
721 , checkbox => [2, 1]);
722 0 ▶ sub att_value_in {
Subroutine att_value_in: not called CC=14 SCAR=53.5
Pod undocumented
723 0 my ($in, $type, $name, $formal_value, $as_value, $is_default) = @_;
724 0 ▶ defined (my $spec = $input_spec{$type})
Branch: 1 branch
BranchTrueFalseTotal
defined(my $spec = $YATT::Lite::Util::input_spec{$type}) or croak("Unknown type: $type") 0 0 0
725 or croak "Unknown type: $type";
726  
727 0 my ($typeid, $has_sfx) = @$spec;
728  
729 0 ▶ unless (defined $name and $name ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and $name ne "") 0 0 0
Condition0%0/3
defined $name and $name ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $name ne ""
defined $name $name ne ""
Truth table0%0/3
A and B
A= defined $nameB= $name ne ""
ABresult
0X0
100
111
730 0 croak "name is empty";
731 }
732  
733 0 ▶ unless (defined $formal_value and $formal_value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $formal_value and $formal_value ne "") 0 0 0
Condition0%0/3
defined $formal_value and $formal_value ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $formal_value and $formal_value ne ""
defined $formal_value $formal_value ne ""
Truth table0%0/3
A and B
A= defined $formal_valueB= $formal_value ne ""
ABresult
0X0
100
111
734 0 croak "value is empty";
735 }
736  
737 0 my @res;
738  
739 0 ▶ if ($type and $typeid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $typeid) 0 0 0
Condition0%0/3
$type and $typeid
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $typeid
$type $typeid
Truth table0%0/3
A and B
A= $typeB= $typeid
ABresult
0X0
100
111
740 0 push @res, qq|type="$type"|;
741 }
742  
743 0 ▶ if ($typeid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($typeid) 0 0 0
744 0 ▶ my $sfx = $has_sfx ? '['.escape($formal_value).']' : '';
Branch: 1 branch
BranchTrueFalseTotal
$has_sfx ? : 0 0 0
745 0 push @res, qq|name="@{[escape($name)]}$sfx"|;
746 }
747  
748 0 ▶ if (not $has_sfx) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $has_sfx) { } 0 0 0
elsif ($as_value) { } 0 0 0
749 # select
750 0 push @res, qq|value="@{[escape($formal_value)]}"|;
751 } elsif ($as_value) {
752 # checkbox/radio, with explicit value
753 0 push @res, qq|value="@{[escape($as_value)]}"|;
754 }
755  
756 0 ▶ if (find_value_in($in, $name, $formal_value, $is_default)) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_value_in($in, $name, $formal_value, $is_default)) 0 0 0
757 0 ▶ push @res, $typeid ? "checked" : "selected";
Branch: 1 branch
BranchTrueFalseTotal
$typeid ? : 0 0 0
758 }
759  
760 0 join(" ", @res);
761 }
762  
763 0 ▶ sub find_value_in {
Subroutine find_value_in: not called CC=8 SCAR=42.8
Pod undocumented
764 0 my ($in, $name, $formal_value, $is_default) = @_;
765  
766 0 ▶ if (ref $in eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in eq "HASH") 0 0 0
767 0 return $in->{$formal_value};
768 }
769  
770 0 my $actual_value = do {
771 0 ▶ if (my $sub = UNIVERSAL::can($in, "param")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = UNIVERSAL::can($in, 'param')) { } 0 0 0
772 0 $sub->($in, $name);
773 } else {
774 0 undef;
775 }
776 };
777  
778 0 ▶ if (not defined $actual_value) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $actual_value) { } 0 0 0
elsif (not ref $actual_value) { } 0 0 0
elsif (ref $actual_value eq 'HASH') { } 0 0 0
elsif (ref $actual_value eq 'ARRAY') { } 0 0 0
779 0 ▶ $is_default ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$is_default ? : 0 0 0
780 } elsif (not ref $actual_value) {
781 0 $actual_value eq $formal_value
782 } elsif (ref $actual_value eq 'HASH') {
783 0 $actual_value->{$formal_value};
784 } elsif (ref $actual_value eq 'ARRAY') {
785 0 ▶ defined lsearch {$_ eq $formal_value} @$actual_value
Subroutine __ANON__: not called CC=1 SCAR=6.9
786 } else {
787 undef
788 0 }
789 }
790 }
791  
792 # Verbatimly stolen from CGI::Simple
793 # XXX: not used?
794 107 ▶sub url_decode {
Subroutine url_decode: not called CC=2 SCAR=15.7
Pod undocumented
795 0 my ( $self, $decode ) = @_;
796 0 ▶ return () unless defined $decode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decode 0 0 0
797 0 $decode =~ tr/+/ /;
798 0 $decode =~ s/%([a-fA-F0-9]{2})/ pack "C", hex $1 /eg;
799 # XXX: should set utf8 flag too?
800 0 return $decode;
801 }
802  
803 181 ▶sub url_encode {
Subroutine url_encode: called CC=3 SCAR=11.0
Pod undocumented
804 181 my ( $self, $encode ) = @_;
805 181 ▶ return () unless defined $encode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encode 0 181 181
806  
807 181 ▶ if (Encode::is_utf8($encode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($encode)) 25 156 181
808 25 $encode = Encode::encode_utf8($encode);
809 }
810  
811 # XXX: Forward slash (and ':') is allowed, for cleaner url. This may break...
812 $encode
813 181 =~ s{([^A-Za-z0-9\-_.!~*\'() /:])}{ uc sprintf "%%%02x",ord $1 }eg;
814 181 $encode =~ tr/ /+/;
815 181 return $encode;
816 }
817  
818 0 ▶sub url_encode_kv {
Subroutine url_encode_kv: not called CC=1 SCAR=6.9
Pod undocumented
819 0 my ($self, $k, $v) = @_;
820 0 url_encode($self, $k) . '=' . url_encode($self, $v);
821 }
822  
823 0 ▶sub encode_query {
Subroutine encode_query: not called CC=8 SCAR=42.8
Pod undocumented
824 0 my ($self, $param, $sep) = @_;
825 0 ▶ return $param unless ref $param;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $param 0 0 0
826 0 ▶ join $sep // ';', do {
Condition0%0/2
$sep // ";"
l!l
00
MC/DC0%0/1
$sep // ";"
$sep
Truth table0%0/2
A
A= $sep
Aresult
11
00
827 0 ▶ if (ref $param eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq 'HASH') { } 0 0 0
828 map {
829 0 url_encode_kv($self, $_, $param->{$_});
830 } keys %$param
831 } else {
832 0 my @param = @$param;
833 0 my @res;
834 0 ▶ while (my ($k, $v) = splice @param, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@param, 0, 2)) 0 0 0
835 0 my $ek = url_encode($self, $k);
836 push @res, $ek . '='. (url_encode($self, $_) // '')
837 0 ▶ for ref $v ? @$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
Condition0%0/2
url_encode($self, $_) // ""
l!l
00
MC/DC0%0/1
url_encode($self, $_) // ""
url_encode($self, $_)
Truth table0%0/2
A
A= url_encode($self, $_)
Aresult
11
00
838 }
839 0 @res;
840 }
841 };
842 }
843  
844 65 ▶sub callerinfo {
Subroutine callerinfo: called CC=2 SCAR=7.2
Pod undocumented
845 65 ▶ my ($pkg, $file, $line) = caller(shift // 1);
Condition50%1/2
shift() // 1
l!l
065
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
846 65 (file => $file, line => $line);
847 }
848  
849 25 ▶sub ostream {
Subroutine ostream: called CC=5 SCAR=16.4
Pod undocumented
850 25 ▶ my $fn = ref $_[0] ? $_[0] : \ ($_[0] //= "");
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 21 4 25
Condition50%1/2
$_[0] //= ""
l!l
40
MC/DC0%0/1
$_[0] //= ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
851 25 ▶ open my $fh, '>' . ($_[1] // ''), $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, '>' . ($_[1] // ""), $fn or die "Can't create output memory stream: $!" 25 0 25
Condition100%2/2
$_[1] // ""
l!l
214
MC/DC100%1/1
$_[1] // ""
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
852 or die "Can't create output memory stream: $!";
853 25 $fh;
854 }
855  
856 10 ▶sub read_file {
Subroutine read_file: called CC=4 SCAR=15.0
Pod undocumented
857 10 my ($fn, $layer) = @_;
858 10 my $fh;
859 10 ▶ if ($fn eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq '-') { } 0 10 10
860 0 $fh = \*STDIN;
861 } else {
862 10 ▶ open $fh, '<' . ($layer // ''), $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, '<' . ($layer // ""), $fn or die "Can't open '${fn}': $!" 10 0 10
Condition50%1/2
$layer // ""
l!l
010
MC/DC0%0/1
$layer // ""
$layer
Truth table50%1/2
A
A= $layer
Aresult
11
00
863 or die "Can't open '$fn': $!";
864 }
865 10 local $/;
866 10 scalar <$fh>;
867 }
868  
869 0 ▶sub dispatch_all {
Subroutine dispatch_all: not called CC=8 SCAR=42.8
Pod undocumented
870 0 my ($this, $con, $prefix, $argSpec) = splice @_, 0, 4;
871 0 ▶ my ($nargs, @preargs) = ref $argSpec ? @$argSpec : $argSpec;
Branch: 1 branch
BranchTrueFalseTotal
ref $argSpec ? : 0 0 0
872 0 my @queue;
873 0 foreach my $item (@_) {
874 0 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 0 0 0
875 0 ▶ print {$con} escape(splice @queue) if @queue;
Branch: 1 branch
BranchTrueFalseTotal
if @queue 0 0 0
876 0 my ($wname, @args) = @$item;
877 0 ▶ my $sub = $this->can('render_' . $prefix . $wname)
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $this->can('render_' . $prefix . $wname) or croak("Can't find widget '${wname}' in dispatch") 0 0 0
878 or croak "Can't find widget '$wname' in dispatch";
879 0 ▶ $sub->($this, $con, @preargs, splice(@args, 0, $nargs // 0), \@args);
Condition0%0/2
$nargs // 0
l!l
00
MC/DC0%0/1
$nargs // 0
$nargs
Truth table0%0/2
A
A= $nargs
Aresult
11
00
880 } else {
881 0 push @queue, $item;
882 }
883 }
884 0 ▶ print {$con} escape(@queue) if @queue;
Branch: 1 branch
BranchTrueFalseTotal
if @queue 0 0 0
885 }
886  
887 0 ▶sub dispatch_one {
Subroutine dispatch_one: not called CC=4 SCAR=30.0
Pod undocumented
888 0 my ($this, $con, $prefix, $nargs, $item) = @_;
889 0 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 0 0 0
890 0 my ($wname, @args) = @$item;
891 0 ▶ my $sub = $this->can('render_' . $prefix . $wname)
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $this->can('render_' . $prefix . $wname) or croak("Can't find widget '${wname}' in dispatch") 0 0 0
892 or croak "Can't find widget '$wname' in dispatch";
893 0 ▶ $sub->($this, $con, splice(@args, 0, $nargs // 0), \@args);
Condition0%0/2
$nargs // 0
l!l
00
MC/DC0%0/1
$nargs // 0
$nargs
Truth table0%0/2
A
A= $nargs
Aresult
11
00
894 } else {
895 0 print {$con} escape($item);
896 }
897 }
898  
899 2 ▶sub con_error {
Subroutine con_error: called CC=3 SCAR=11.0
Pod undocumented
900 2 my ($con, $err, @args) = @_;
901 2 ▶ if ($con->can("raise") and my $sub = $con->can("error")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($con->can('raise') and my $sub = $con->can('error')) { } 1 1 2
Condition66%2/3
$con->can("raise") and my $sub = $con->can("error")
!ll&&!rl&&r
101
MC/DC50%1/2
$con->can("raise") and my $sub = $con->can("error")
$con->can("raise") my $sub = $con->can("error")
Truth table66%2/3
A and B
A= $con->can("raise")B= my $sub = $con->can("error")
ABresult
0X0
100
111
902 1 $sub->($con, $err, @args)
903 } else {
904 1 sprintf $err, @args;
905 }
906 }
907  
908 11 ▶sub safe_render {
Subroutine safe_render: called CC=3 SCAR=11.0
Pod undocumented
909 11 my ($this, $con, $wspec, @args) = @_;
910 11 my @nsegs = lexpand($wspec);
911 11 ▶ my $wname = join _ => map {defined $_ ? $_ : ''} @nsegs;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 13 2 15
912 11 ▶ my $sub = $this->can("render_$wname")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $this->can("render_$wname") or die con_error($con, "Can't find widget '%s'", $wname) 9 2 11
913 or die con_error($con, "Can't find widget '%s'", $wname);
914 9 $sub->($this, $con, @args);
915 }
916  
917 33 ▶sub mk_http_status {
Subroutine mk_http_status: called CC=1 SCAR=0.0
Pod undocumented
918 33 my ($code) = @_;
919 33 require HTTP::Status;
920  
921 33 my $message = HTTP::Status::status_message($code);
922 33 "Status: $code $message\015\012";
923 }
924  
925 5422 ▶sub list_isa {
Subroutine list_isa: called CC=4 SCAR=13.9
Pod undocumented
926 5422 my ($pack, $all) = @_;
927 5422 my $symtab = symtab($pack);
928 5422 ▶ my $sym = $symtab->{ISA} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sym = $symtab->{"ISA"} or return 5180 242 5422
929 5180 ▶ my $isa = *{$sym}{ARRAY} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $isa = *{$sym;}{"ARRAY"} or return 5180 0 5180
930 5180 ▶ return @$isa unless $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 5107 73 5180
931 map {
932 73 [$_, list_isa($_, $all)];
933 } @$isa;
934 }
935  
936 1599 ▶sub set_inc {
Subroutine set_inc: called CC=2 SCAR=7.0
Pod undocumented
937 1599 my ($pkg, $val) = @_;
938 1599 $pkg =~ s|::|/|g;
939 1599 ▶ $INC{$pkg.'.pm'} = $val || 1;
Condition50%1/2
$val || 1
l!l
15990
MC/DC0%0/1
$val || 1
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
940 # $INC{$pkg.'.pmc'} = $val || 1;
941 1599 $_[1];
942 }
943  
944 2693 ▶sub try_invoke {
Subroutine try_invoke: called CC=4 SCAR=13.9
Pod undocumented
945 2693 my $obj = shift;
946 2693 my ($method, @args) = lexpand(shift);
947 2693 my $default = shift;
948 2693 ▶ if (defined $obj
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj and my $sub = UNIVERSAL::can($obj, $method)) { } 2489 204 2693
Condition66%2/3
defined $obj and my $sub = UNIVERSAL::can($obj, $method)
!ll&&!rl&&r
02042489
MC/DC50%1/2
defined $obj and my $sub = UNIVERSAL::can($obj, $method)
defined $obj my $sub = UNIVERSAL::can($obj, $method)
Truth table66%2/3
A and B
A= defined $objB= my $sub = UNIVERSAL::can($obj, $method)
ABresult
0X0
100
111
949 and my $sub = UNIVERSAL::can($obj, $method)) {
950 2489 $sub->($obj, @args);
951 } else {
952 204 ▶ wantarray ? () : $default;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 196 204
953 }
954 }
955  
956 0 ▶sub NIMPL {
Subroutine NIMPL: not called CC=2 SCAR=17.9
Pod undocumented
957 0 ▶ my ($pack, $file, $line, $sub, $hasargs) = caller($_[0] // 1);
Condition0%0/2
$_[0] // 1
l!l
00
MC/DC0%0/1
$_[0] // 1
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
958 0 croak "Not implemented call of '$sub'";
959 }
960  
961 4 ▶sub shallow_copy {
Subroutine shallow_copy: called CC=5 SCAR=16.8
Pod undocumented
962 4 ▶ if (ref $_[0] eq 'HASH') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 2 4
elsif (ref $_[0] eq 'ARRAY') { } 1 1 2
elsif (not ref $_[0]) { } 0 1 1
elsif ($_[1]) { } 1 0 1
963 2 +{%{$_[0]}};
964 } elsif (ref $_[0] eq 'ARRAY') {
965 1 +[@{$_[0]}];
966 } elsif (not ref $_[0]) {
967 0 my $copy = $_[0];
968 } elsif ($_[1]) {
969 # Pass thru unknown refs if 2nd arg is true.
970 1 $_[0];
971 } else {
972 0 croak "Unsupported data type for shallow_copy: " . ref $_[0];
973 }
974 }
975  
976 137 ▶sub deep_copy_array {
Subroutine deep_copy_array: called CC=2 SCAR=6.9
Pod undocumented
977 [map {
978 725 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 77 648 725
979 77 deep_copy_array($_)
980 } else {
981 648 $_;
982 }
983 137 } @{$_[0]}];
984 }
985  
986 0 ▶if (not is_debugging() or catch {require Sub::Name}) {
Subroutine __ANON__: not called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if (not is_debugging() or &catch(sub { require Sub::Name; } )) { } 107 0 107
Condition33%1/3
not is_debugging() or &catch(sub { require Sub::Name; } )
l!l&&r!l&&!r
10700
MC/DC0%0/2
not is_debugging() or &catch(sub { require Sub::Name; } )
not is_debugging() &catch(sub { require Sub::Name; } )
Truth table33%1/3
not(A) or B
A= not is_debugging()B= &catch(sub { require Sub::Name; } )
ABresult
1X1
011
000
987 2942 ▶ *subname = sub { my ($name, $sub) = @_; $sub }
Subroutine __ANON__: called CC=1 SCAR=0.0
988 107 } else {
989 0 *subname = *Sub::Name::subname;
990 }
991  
992 298 ▶sub incr_opt {
Subroutine incr_opt: called CC=4 SCAR=13.9
Pod undocumented
993 298 my ($key, $list) = @_;
994 298 my $hash = do {
995 298 ▶ if (@$list and defined $list->[0] and ref $list->[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list and defined $list->[0] and ref $list->[0] eq 'HASH') { } 229 69 298
Condition83%5/6
@$list and defined $list->[0] and ref $list->[0] eq "HASH"
!ll&&!rl&&r
366229
@$list and defined $list->[0]
!ll&&!rl&&r
30295
MC/DC66%2/3
@$list and defined $list->[0] and ref $list->[0] eq "HASH"
@$list defined $list->[0] ref $list->[0] eq "HASH"
Truth table75%3/4
A and B and C
A= @$listB= defined $list->[0]C= ref $list->[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
996 229 shift @$list;
997 } else {
998 +{}
999 69 }
1000 };
1001 298 $hash->{$key}++;
1002 298 $hash;
1003 }
1004  
1005 16 ▶sub num_is_ge {
Subroutine num_is_ge: called CC=5 SCAR=23.0
Pod undocumented
1006 16 ▶ defined $_[0] and not ref $_[0] and $_[0] ne ''
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and $_[0] ne "" and $_[0] =~ /^\d+$/ and $& >= $_[1] 0 16 16
Condition33%3/9
defined $_[0] and not ref $_[0] and $_[0] ne "" and $_[0] =~ /^\d+$/
!ll&&!rl&&r
1600
defined $_[0] and not ref $_[0] and $_[0] ne ""
!ll&&!rl&&r
1600
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
1600
MC/DC0%0/4
defined $_[0] and not ref $_[0] and $_[0] ne "" and $_[0] =~ /^\d+$/
defined $_[0] not ref $_[0] $_[0] ne "" $_[0] =~ /^\d+$/
Truth table20%1/5
A and not(B) and C and D
A= defined $_[0]B= not ref $_[0]C= $_[0] ne ""D= $_[0] =~ /^\d+$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1007 and $_[0] =~ /^\d+$/ and $& >= $_[1];
1008 }
1009  
1010 # Order preserving unique.
1011 14 ▶sub unique (@) {
Subroutine unique: called CC=2 SCAR=6.9
Pod undocumented
1012 14 my %dup;
1013 14 ▶ map {$dup{$_}++ ? () : $_} @_;
Branch: 1 branch
BranchTrueFalseTotal
$dup{$_}++ ? : 9 20 29
1014 }
1015  
1016 99 ▶sub secure_text_plain {
Subroutine secure_text_plain: called CC=1 SCAR=0.0
Pod undocumented
1017 99 shift;
1018 99 ("Content-type" => "text/plain; charset=utf-8"
1019 , "X-Content-Type-Options" => "nosniff" # To protect IE8~ from XSS.
1020 );
1021 }
1022  
1023 #========================================
1024  
1025 # Just a wrapper (and hook) for die. $self is ignored.
1026 27 ▶sub raise_response {
Subroutine raise_response: called CC=1 SCAR=0.0
Pod undocumented
1027 27 my ($self, $response) = @_;
1028 27 die $response;
1029 }
1030  
1031 #
1032 # $this->raise_download($fileName, $bytesOrBytesRef, ?[@header]?)
1033 #
1034 # $fileName is treated as data (templates pass request-derived names
1035 # here): control characters are dropped so it can never break out of the
1036 # header, and anything beyond plain ASCII is carried in an RFC 8187
1037 # filename*= parameter with an ASCII-only filename= fallback.
1038 #
1039 16 ▶sub raise_download {
Subroutine raise_download: called CC=11 SCAR=24.2
Pod undocumented
1040 16 my $this = $_[0];
1041 16 my $filename = $_[1];
1042 16 ▶ my $bytesRef = ref $_[2] eq 'SCALAR' ? $_[2] : \$_[2];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] eq 'SCALAR' ? : 0 16 16
1043 16 my @header = ("Content-type" => qq{application/octet-stream},
1044 , "Content-Length" => length($$bytesRef));
1045 16 ▶ if (my $disposition = content_disposition_attachment($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $disposition = content_disposition_attachment($filename)) 12 4 16
1046 12 push @header, "Content-Disposition" => $disposition;
1047 }
1048 16 ▶ if (defined $_[3] and ref $_[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3] and ref $_[3]) 3 13 16
Condition66%2/3
defined $_[3] and ref $_[3]
!ll&&!rl&&r
1303
MC/DC50%1/2
defined $_[3] and ref $_[3]
defined $_[3] ref $_[3]
Truth table66%2/3
A and B
A= defined $_[3]B= ref $_[3]
ABresult
0X0
100
111
1049 3 my @extra = lexpand($_[3]);
1050 3 ▶ for (my $i = 0; $i < @extra; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @extra) 3 1 4
1051 3 my ($k, $v) = @extra[$i, $i+1];
1052 3 ▶ croak "raise_download: header name is not a token: ".($k // 'undef')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and $k =~ /^[\w-]+\z/ 1 2 3
Condition60%3/5
defined $k and $k =~ /^[\w-]+\z/
!ll&&!rl&&r
012
$k // "undef"
l!l
10
MC/DC50%1/2
defined $k and $k =~ /^[\w-]+\z/
defined $k $k =~ /^[\w-]+\z/
MC/DC0%0/1
$k // "undef"
$k
Truth table66%2/3
A and B
A= defined $kB= $k =~ /^[\w-]+\z/
ABresult
0X0
100
111
Truth table50%1/2
A
A= $k
Aresult
11
00
1053 unless defined $k and $k =~ /^[\w-]+\z/;
1054 2 ▶ croak "raise_download: header '$k' contains a control character"
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v =~ /[\x00-\x1f\x7f]/ 1 1 2
Condition66%2/3
defined $v and $v =~ /[\x00-\x1f\x7f]/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $v and $v =~ /[\x00-\x1f\x7f]/
defined $v $v =~ /[\x00-\x1f\x7f]/
Truth table66%2/3
A and B
A= defined $vB= $v =~ /[\x00-\x1f\x7f]/
ABresult
0X0
100
111
1055 if defined $v and $v =~ /[\x00-\x1f\x7f]/;
1056 }
1057 1 push @header, @extra;
1058 }
1059  
1060 14 $this->raise_response([200, \@header, [$$bytesRef]]);
1061 }
1062  
1063 #
1064 # content_disposition_attachment($filename) returns the value of a
1065 # Content-Disposition header for an attachment download, or undef when
1066 # $filename is empty. Control characters (CR, LF, NUL, ...) are removed.
1067 # When the remainder is not plain ASCII without '"' or '\', an ASCII
1068 # fallback is used for filename= and the real name goes to filename*=
1069 # (RFC 8187, UTF-8 percent-encoded).
1070 #
1071 19 ▶sub content_disposition_attachment {
Subroutine content_disposition_attachment: called CC=7 SCAR=19.5
Pod undocumented
1072 19 my ($filename) = @_;
1073 19 ▶ return undef unless defined $filename and $filename ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and $filename ne "" 4 15 19
Condition100%3/3
defined $filename and $filename ne ""
!ll&&!rl&&r
2215
MC/DC100%2/2
defined $filename and $filename ne ""
defined $filename $filename ne ""
Truth table100%3/3
A and B
A= defined $filenameB= $filename ne ""
ABresult
0X0
100
111
1074  
1075 15 (my $name = $filename) =~ s/[\x00-\x1f\x7f]//g;
1076 15 ▶ return undef if $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "" 2 13 15
1077  
1078 13 ▶ if ($name =~ /^[\x20-\x7e]+\z/ and $name !~ /["\\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^[\x20-\x7e]+\z/ and not $name =~ /["\\]/) 10 3 13
Condition100%3/3
$name =~ /^[\x20-\x7e]+\z/ and not $name =~ /["\\]/
!ll&&!rl&&r
2110
MC/DC100%2/2
$name =~ /^[\x20-\x7e]+\z/ and not $name =~ /["\\]/
$name =~ /^[\x20-\x7e]+\z/ not $name =~ /["\\]/
Truth table100%3/3
A and not(B)
A= $name =~ /^[\x20-\x7e]+\z/B= not $name =~ /["\\]/
ABresult
0X0
100
111
1079 10 return qq{attachment; filename="$name"};
1080 }
1081  
1082 3 (my $fallback = $name) =~ s/[^\x20-\x7e]|["\\]/_/g;
1083  
1084 3 ▶ my $encoded = utf8::is_utf8($name)
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($name) ? : 1 2 3
1085 ? URI::Escape::uri_escape_utf8($name)
1086 : URI::Escape::uri_escape($name);
1087  
1088 3 qq{attachment; filename="$fallback"; filename*=UTF-8''$encoded};
1089 }
1090  
1091 #========================================
1092  
1093 107 foreach my $what (qw(error text dump)) {
1094 321 my $actual = "psgi_$what";
1095 321 my $sub = __PACKAGE__->can($actual);
1096 321 my $method = "raise_$actual";
1097 321 ▶ *{globref($method)} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1098 2 my $self = shift;
1099 2 $self->raise_response($sub->($self, @_));
1100 321 };
1101 }
1102  
1103 6 ▶sub psgi_error {
Subroutine psgi_error: called CC=2 SCAR=7.0
Pod undocumented
1104 6 my ($self, $status, $msg, @rest) = @_;
1105 6 my $escaped = escape($msg);
1106 6 Encode::_utf8_off($escaped);
1107 6 ▶ return [$status, [$self->secure_text_plain, @rest]
Branch: 1 branch
BranchTrueFalseTotal
$msg =~ /\n\z/ ? : 0 6 6
1108 , [$escaped
1109 , $msg =~ /\n\z/ ? () : "\n" ]];
1110 }
1111  
1112 92 ▶sub psgi_text {
Subroutine psgi_text: called CC=2 SCAR=7.1
Pod undocumented
1113 92 my ($self, $statusAndHeader, @args) = @_;
1114 92 ▶ my ($status, @header) = ref $statusAndHeader ? @$statusAndHeader : $statusAndHeader;
Branch: 1 branch
BranchTrueFalseTotal
ref $statusAndHeader ? : 0 92 92
1115 92 return [$status, [$self->secure_text_plain, @header], \@args];
1116 }
1117  
1118 1 ▶sub psgi_dump {
Subroutine psgi_dump: called CC=1 SCAR=0.0
Pod undocumented
1119 1 my $self = shift;
1120 [200
1121 , [$self->secure_text_plain]
1122 1 , [join("\n", map {terse_dump($_)} @_)."\n"]];
1123 }
1124  
1125 0 ▶sub dumpout (@) {
Subroutine dumpout: not called CC=1 SCAR=6.9
Pod undocumented
1126 0 __PACKAGE__->raise_psgi_dump(@_);
1127 }
1128  
1129 819 ▶sub ixhash {
Subroutine ixhash: called CC=1 SCAR=0.0
Pod undocumented
1130 819 tie my %hash, 'Tie::IxHash', @_;
1131 819 \%hash;
1132 }
1133  
1134 # Ported from: Rack::Utils.parse_nested_query
1135 582 ▶sub parse_nested_query {
Subroutine parse_nested_query: called CC=11 SCAR=24.5
Pod undocumented
1136 582 ▶ return {} unless defined $_[0] and $_[0] ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and $_[0] ne "" 16 566 582
Condition66%2/3
defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
016566
MC/DC50%1/2
defined $_[0] and $_[0] ne ""
defined $_[0] $_[0] ne ""
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] ne ""
ABresult
0X0
100
111
1137 566 my ($enc) = $_[1];
1138 566 ▶ my $params = $_[2] // ixhash();
Condition33%1/3
$_[2] // ixhash()
l!l&&r!l&&!r
05660
MC/DC0%0/2
$_[2] // ixhash()
$_[2] ixhash()
Truth table33%1/3
A // B
A= $_[2]B= ixhash()
ABresult
1X1
011
000
1139 566 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 517 49 566
1140 517 ▶ my @pairs = map {$enc ? map(Encode::decode($enc, $_), @$_) : @$_}
Branch: 2 branches
BranchTrueFalseTotal
$enc ? : 489 28 517
ref $_[0] eq 'ARRAY' ? : 517 0 517
1141 0 ref $_[0] eq 'ARRAY' ? $_[0] : [%{$_[0]}];
1142 517 ▶ while (my ($k, $v) = splice @pairs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 290 517 807
1143 290 normalize_params($params, $k, $v);
1144 }
1145 } else {
1146 49 foreach my $p (split /[;&]/, $_[0]) {
1147 my ($k, $v) = map {
1148 97 s/\+/ /g;
1149 186 my $raw = URI::Escape::uri_unescape($_);
1150 186 ▶ $enc ? Encode::decode($enc, $raw) : $raw;
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 0 186 186
1151 } split /=/, $p, 2;
1152 97 ▶ normalize_params($params, $k, $v) if defined $k;
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 95 2 97
1153 }
1154 }
1155 563 $params;
1156 }
1157  
1158 511 ▶sub normalize_params {
Subroutine normalize_params: called CC=20 SCAR=30.1
Pod undocumented
1159 511 my ($params, $name, $v) = @_;
1160 511 ▶ if ($name eq '[]' and defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "[]" and defined $v) 1 510 511
Condition66%2/3
$name eq "[]" and defined $v
!ll&&!rl&&r
51001
MC/DC50%1/2
$name eq "[]" and defined $v
$name eq "[]" defined $v
Truth table66%2/3
A and B
A= $name eq "[]"B= defined $v
ABresult
0X0
100
111
1161 1 return [$v];
1162 }
1163 510 ▶ my ($k) = $name =~ m(\A[\[\]]*([^\[\]]+)\]*)
Branch: 1 branch
BranchTrueFalseTotal
my($k) = $name =~ /\A[\[\]]*([^\[\]]+)\]*/ or return 508 2 510
1164 or return;
1165  
1166 508 my $after = substr($name, length $&);
1167  
1168 508 ▶ if ($after eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($after eq '') { } 321 187 508
elsif ($after eq '[]') { } 59 128 187
elsif ($after =~ /^\[\]\[([^\[\]]+)\]$/ or $after =~ /^\[\](.+)$/) { } 41 87 128
1169 321 $params->{$k} = $v;
1170 } elsif ($after eq "[]") {
1171 59 ▶ my $item = $params->{$k} //= [];
Condition100%2/2
$params->{$k} //= []
l!l
2435
MC/DC100%1/1
$params->{$k} //= []
$params->{$k}
Truth table100%2/2
A
A= $params->{$k}
Aresult
11
00
1172 59 ▶ croak "expected ARRAY (got ".(ref $item || 'String').") for param `$k'"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "ARRAY" 1 58 59
Condition50%1/2
ref $item || 'String'
l!l
10
MC/DC0%0/1
ref $item || 'String'
ref $item
Truth table50%1/2
A
A= ref $item
Aresult
11
00
1173 unless ref $item eq 'ARRAY';
1174 58 push @$item, $v;
1175 ▶ } elsif ($after =~ m(^\[\]\[([^\[\]]+)\]$) or $after =~ m(^\[\](.+)$)) {
Condition100%3/3
$after =~ /^\[\]\[([^\[\]]+)\]$/ or $after =~ /^\[\](.+)$/
l!l&&r!l&&!r
311087
MC/DC100%2/2
$after =~ /^\[\]\[([^\[\]]+)\]$/ or $after =~ /^\[\](.+)$/
$after =~ /^\[\]\[([^\[\]]+)\]$/ $after =~ /^\[\](.+)$/
Truth table100%3/3
A or B
A= $after =~ /^\[\]\[([^\[\]]+)\]$/B= $after =~ /^\[\](.+)$/
ABresult
1X1
011
000
1176 41 my $child_key = $1;
1177 41 ▶ my $item = $params->{$k} //= [];
Condition100%2/2
$params->{$k} //= []
l!l
2021
MC/DC100%1/1
$params->{$k} //= []
$params->{$k}
Truth table100%2/2
A
A= $params->{$k}
Aresult
11
00
1178 41 ▶ croak "expected ARRAY (got ".(ref $item || 'String').") for param `$k'"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "ARRAY" 1 40 41
Condition50%1/2
ref $item || 'String'
l!l
01
MC/DC0%0/1
ref $item || 'String'
ref $item
Truth table50%1/2
A
A= ref $item
Aresult
11
00
1179 unless ref $item eq 'ARRAY';
1180 40 ▶ if (@$item and ref $item->[-1] eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (@$item and ref $item->[-1] eq 'HASH' and not exists $item->[-1]{$child_key}) { } 12 28 40
Condition100%6/6
@$item and ref $item->[-1] eq "HASH" and not exists $item->[-1]{$child_key}
!ll&&!rl&&r
22612
@$item and ref $item->[-1] eq "HASH"
!ll&&!rl&&r
21118
MC/DC100%3/3
@$item and ref $item->[-1] eq "HASH" and not exists $item->[-1]{$child_key}
@$item ref $item->[-1] eq "HASH" not exists $item->[-1]{$child_key}
Truth table100%4/4
A and B and not(C)
A= @$itemB= ref $item->[-1] eq "HASH"C= not exists $item->[-1]{$child_key}
ABCresult
0XX0
10X0
1100
1111
1181 and not exists $item->[-1]->{$child_key}) {
1182 12 normalize_params($item->[-1], $child_key, $v);
1183 } else {
1184 28 push @$item, normalize_params(ixhash(), $child_key, $v);
1185 }
1186 } else {
1187 87 ▶ my $item = $params->{$k} //= ixhash();
Condition66%2/3
$params->{$k} //= ixhash()
l!l&&r!l&&!r
32550
MC/DC0%0/2
$params->{$k} //= ixhash()
$params->{$k} ixhash()
Truth table66%2/3
A //= B
A= $params->{$k}B= ixhash()
ABresult
1X1
011
000
1188 87 ▶ croak "expected HASH (got ".(ref $item || 'String').") for param `$k'"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 1 86 87
Condition50%1/2
ref $item || 'String'
l!l
01
MC/DC0%0/1
ref $item || 'String'
ref $item
Truth table50%1/2
A
A= ref $item
Aresult
11
00
1189 unless ref $item eq 'HASH';
1190 86 $params->{$k} = normalize_params($item, $after, $v);
1191 }
1192  
1193 503 $params;
1194 }
1195  
1196 # Ported (with API modification) from: Rack::Utils.build_nested_query
1197 52 ▶sub build_nested_query {
Subroutine build_nested_query: called CC=5 SCAR=17.4
Pod undocumented
1198 52 my ($self, $hash, $opts) = @_;
1199 my $ignore = ref $opts->{ignore} eq 'HASH'
1200 ? $opts->{ignore}
1201 0 ▶ : +{map {$_ => 1} @{$opts->{ignore}}};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'ignore'} eq 'HASH' ? : 0 52 52
1202 join $opts->{sep} // '&'
1203 , map {
1204 52 ▶ if ($ignore and $ignore->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore and $ignore->{$_}) { } 0 69 69
Condition60%3/5
$opts->{"sep"} // "&"
l!l
547
$ignore and $ignore->{$_}
!ll&&!rl&&r
0690
MC/DC100%1/1
$opts->{"sep"} // "&"
$opts->{"sep"}
MC/DC0%0/2
$ignore and $ignore->{$_}
$ignore $ignore->{$_}
Truth table100%2/2
A
A= $opts->{"sep"}
Aresult
11
00
Truth table33%1/3
A and B
A= $ignoreB= $ignore->{$_}
ABresult
0X0
100
111
1205 ()
1206 0 } else {
1207 69 my $v = $hash->{$_};
1208 69 my $k = url_encode($self, $_); # URI::Escape::uri_escape does too much.
1209 69 $k =~ tr/ /+/;
1210 69 build_nested_query_value($self, $v, $k);
1211 }
1212 } keys %$hash;
1213 }
1214  
1215 183 ▶sub build_nested_query_value {
Subroutine build_nested_query_value: called CC=7 SCAR=19.7
Pod undocumented
1216 183 my ($self, $value, $prefix) = @_;
1217 183 ▶ if (not defined $value) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $value) { } 4 179 183
elsif (ref $value eq 'ARRAY') { } 39 140 179
elsif (ref $value eq 'HASH' or UNIVERSAL::can($value, 'keys')) { } 50 90 140
elsif (not defined $prefix) { } 0 90 90
1218 4 $prefix;
1219 } elsif (ref $value eq 'ARRAY') {
1220 map {
1221 39 build_nested_query_value($self, $_, $prefix."[]");
1222 } @$value;
1223 ▶ } elsif (ref $value eq 'HASH' or UNIVERSAL::can($value, 'keys')) {
Condition66%2/3
ref $value eq "HASH" or UNIVERSAL::can($value, "keys")
l!l&&r!l&&!r
50090
MC/DC50%1/2
ref $value eq "HASH" or UNIVERSAL::can($value, "keys")
ref $value eq "HASH" UNIVERSAL::can($value, "keys")
Truth table66%2/3
A or B
A= ref $value eq "HASH"B= UNIVERSAL::can($value, "keys")
ABresult
1X1
011
000
1224 map {
1225 50 my $escaped = URI::Escape::uri_escape_utf8($_);
1226 56 ▶ my $key = $prefix ? $prefix."[$escaped]" : $escaped;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 56 0 56
1227 56 $key =~ tr/ /+/;
1228 56 build_nested_query_value($self, $value->{$_}, $key);
1229 } keys %$value;
1230 } elsif (not defined $prefix) {
1231 0 Carp::croak "value must be a Hash: ". terse_dump($value);
1232 } else {
1233 90 $prefix."=".URI::Escape::uri_escape_utf8($value);
1234 }
1235 }
1236  
1237 1413 ▶sub pkg2pm {
Subroutine pkg2pm: called CC=1 SCAR=0.0
Pod undocumented
1238 1413 my ($pack) = @_;
1239 1413 $pack =~ s{::|'}{/}g;
1240 1413 "$pack.pm";
1241 }
1242  
1243 0 ▶sub dputs {
Subroutine dputs: not called CC=3 SCAR=24.8
Pod undocumented
1244 0 (undef, undef, my $line) = caller(0);
1245 0 (undef, undef, undef, my $func) = caller(1);
1246 print STDERR "# $func $line: ", (map {
1247 0 ▶ if (defined $_ and not ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and not ref $_) { } 0 0 0
Condition0%0/3
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and not ref $_
defined $_ not ref $_
Truth table0%0/3
A and not(B)
A= defined $_B= not ref $_
ABresult
0X0
100
111
1248 0 $_
1249 } else {
1250 0 terse_dump($_)
1251 }
1252 } @_), "\n";
1253 }
1254  
1255 55 ▶sub is_done {
Subroutine is_done: called CC=4 SCAR=14.1
Pod undocumented
1256 6 defined $_[0] and ref $_[0] eq 'SCALAR' and not ref ${$_[0]}
1257 55 ▶ and ${$_[0]} eq 'DONE';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and ref $_[0] eq "SCALAR" and not ref ${$_[0];} and ${$_[0];} eq "DONE" 6 49 55
Condition66%4/6
defined $_[0] and ref $_[0] eq "SCALAR" and not ref ${$_[0];}
!ll&&!rl&&r
4906
defined $_[0] and ref $_[0] eq "SCALAR"
!ll&&!rl&&r
0496
MC/DC33%1/3
defined $_[0] and ref $_[0] eq "SCALAR" and not ref ${$_[0];}
defined $_[0] ref $_[0] eq "SCALAR" not ref ${$_[0];}
Truth table50%2/4
A and B and not(C)
A= defined $_[0]B= ref $_[0] eq "SCALAR"C= not ref ${$_[0];}
ABCresult
0XX0
10X0
1100
1111
1258 }
1259  
1260 6 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=7.6
Pod undocumented
1261 6 ▶ if ($_[0]->can("rewind")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->can('rewind')) { } 0 6 6
1262 0 $_[0]->rewind
1263 } else {
1264 6 seek $_[0], 0, 0;
1265 6 truncate $_[0], 0;
1266 }
1267 }
1268  
1269 0 ▶sub merge_hash_renaming (&@) {
Subroutine merge_hash_renaming: not called CC=5 SCAR=34.0
Pod undocumented
1270 0 my ($code, $base, @overlay) = @_;
1271 0 my $result = +{%$base};
1272 0 foreach my $hash (@overlay) {
1273 0 local $_;
1274 0 foreach (keys %$hash) {
1275 0 ▶ if ($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 0 0 0
1276 0 ▶ my ($renamed) = $code->($_)
Branch: 1 branch
BranchTrueFalseTotal
my($renamed) = &$code($_) or next 0 0 0
1277 or next;
1278 0 $result->{$renamed} = $hash->{$_};
1279 } else {
1280 0 $result->{$_} = $hash->{$_};
1281 }
1282 }
1283 }
1284 0 $result;
1285 }
1286  
1287 14 ▶sub trimleft_length {
Subroutine trimleft_length: called CC=2 SCAR=6.9
Pod undocumented
1288 14 shift;
1289 14 ▶ return $_[0] unless length $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 7 7 14
1290 7 substr($_[0], length($_[1]));
1291 }
1292  
1293 1 ▶sub reencode_malformed_utf8 {
Subroutine reencode_malformed_utf8: called CC=2 SCAR=7.0
Pod undocumented
1294 1 my ($str, $fallback_flag) = @_;
1295 1 Encode::_utf8_off($str);
1296 1 ▶ my $bytes = Encode::decode_utf8($str, $fallback_flag // Encode::FB_XMLCREF);
Condition50%1/2
$fallback_flag // 1032
l!l
01
MC/DC0%0/1
$fallback_flag // 1032
$fallback_flag
Truth table50%1/2
A
A= $fallback_flag
Aresult
11
00
1297 1 Encode::_utf8_on($bytes);
1298 1 $bytes;
1299 }
1300  
1301 2 ▶sub get_entity_symbol {
Subroutine get_entity_symbol: called CC=1 SCAR=0.0
Pod undocumented
1302 2 my ($pack, $entns, $entity_name) = @_;
1303 2 my $symbol_name = join("_", entity => $entity_name);
1304 2 look_for_globref($entns, $symbol_name);
1305 }
1306  
1307 7 ▶sub maybe_findbin_bin {
Subroutine maybe_findbin_bin: called CC=3 SCAR=11.2
Pod undocumented
1308 7 ▶ my $symtab = $main::{'FindBin::'} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $symtab = $main::main::{"FindBin::"} or return undef 7 0 7
1309 7 ▶ my $globref = $ {*{$symtab}{HASH}}{Bin} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $globref = do { *{$symtab;}{"HASH"} }->{"Bin"} or return undef 7 0 7
1310 7 $ {*{$globref}{SCALAR}}
1311 }
1312  
1313 #
1314 # to put all functions into @EXPORT_OK.
1315 #
1316 {
1317 107 our @EXPORT_OK = qw(define_const);
1318 107 my $symtab = symtab(__PACKAGE__);
1319 107 foreach my $name (grep {/^[a-z]/} keys %$symtab) {
1320 11770 my $glob = $symtab->{$name};
1321 11770 ▶ next unless *{$glob}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless *{$glob;}{"CODE"} 749 11021 11770
1322 11021 push @EXPORT_OK, $name;
1323 }
1324 }
1325  
1326 107 1;
« blib/lib/YATT/Lite/LanguageServer.pm ↑ Summary blib/lib/YATT/Lite/Util/FindMethods.pm »