Catmandu/Util.pm

filter: Statementstmt 94.3% 348 / 369 Branchbran 78.3% 163 / 208 Conditioncond 42.1% 40 / 95 MC/DCmcdc 18.8% 10 / 53 Subroutinesub 95.8% 69 / 72 Podpod 86.7% 46 / 53 totaltotal 79.5% 676 / 850 CC 140 SCAR 29.3
CC
140
Cov
82%
data_at
101.8
content_type
20.0
io
19.8
CRAP 18.8
« Catmandu/Cmd/help.pm ↑ Summary Catmandu/Searchable.pm »
1 package Catmandu::Util;
2  
3 177 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 177 our $VERSION = '1.2025';
6  
7 177 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 177 ▶use Sub::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 177 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 177 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 177 ▶use Data::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 177 ▶use Data::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 177 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 177 ▶use IO::Handle::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 177 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 177 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 177 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 177 ▶use Hash::Merge::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 177 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 177 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 177 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 177 our %EXPORT_TAGS = (
24 io => [
25 qw(io read_file read_io write_file read_yaml read_json join_path
26 normalize_path segmented_path content_type)
27 ],
28 data => [qw(parse_data_path get_data set_data delete_data data_at)],
29 array => [
30 qw(array_exists array_group_by array_pluck array_to_sentence
31 array_sum array_includes array_any array_rest array_uniq array_split)
32 ],
33 hash => [qw(hash_merge)],
34 string => [qw(as_utf8 trim capitalize)],
35 is => [qw(is_same is_different)],
36 check => [qw(check_same check_different)],
37 human => [qw(human_number human_content_type human_byte_size)],
38 xml => [qw(xml_declaration xml_escape)],
39 misc => [qw(require_package use_lib pod_section)],
40 date => [qw(now)],
41 );
42  
43 177 our @EXPORT_OK = map {@$_} values %EXPORT_TAGS;
44  
45 177 $EXPORT_TAGS{all} = \@EXPORT_OK;
46  
47 177 my $HUMAN_CONTENT_TYPES = {
48  
49 # txt
50 'text/plain' => 'Text',
51 'application/txt' => 'Text',
52  
53 # pdf
54 'application/pdf' => 'PDF',
55 'application/x-pdf' => 'PDF',
56 'application/acrobat' => 'PDF',
57 'applications/vnd.pdf' => 'PDF',
58 'text/pdf' => 'PDF',
59 'text/x-pdf' => 'PDF',
60  
61 # doc
62 'application/doc' => 'Word',
63 'application/vnd.msword' => 'Word',
64 'application/vnd.ms-word' => 'Word',
65 'application/winword' => 'Word',
66 'application/word' => 'Word',
67 'application/x-msw6' => 'Word',
68 'application/x-msword' => 'Word',
69  
70 # docx
71 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
72 => 'Word',
73  
74 # xls
75 'application/vnd.ms-excel' => 'Excel',
76 'application/msexcel' => 'Excel',
77 'application/x-msexcel' => 'Excel',
78 'application/x-ms-excel' => 'Excel',
79 'application/vnd.ms-excel' => 'Excel',
80 'application/x-excel' => 'Excel',
81 'application/x-dos_ms_excel' => 'Excel',
82 'application/xls' => 'Excel',
83  
84 # xlsx
85 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' =>
86 'Excel',
87  
88 # ppt
89 'application/vnd.ms-powerpoint' => 'PowerPoint',
90 'application/mspowerpoint' => 'PowerPoint',
91 'application/ms-powerpoint' => 'PowerPoint',
92 'application/mspowerpnt' => 'PowerPoint',
93 'application/vnd-mspowerpoint' => 'PowerPoint',
94 'application/powerpoint' => 'PowerPoint',
95 'application/x-powerpoint' => 'PowerPoint',
96  
97 # pptx
98 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
99 => 'PowerPoint',
100  
101 # csv
102 'text/comma-separated-values' => 'CSV',
103 'text/csv' => 'CSV',
104 'application/csv' => 'CSV',
105  
106 # zip
107 'application/zip' => 'ZIP archive',
108 };
109  
110 177 my $XML_DECLARATION = qq(<?xml version="1.0" encoding="UTF-8"?>\n);
111  
112 0 ▶sub TIESCALAR { }
Subroutine TIESCALAR: not called CC=1 SCAR=0.0
113  
114 115 ▶sub io {
Subroutine io: called CC=18 SCAR=29.9
Pod documented
115 115 my ($arg, %opts) = @_;
116 115 ▶ my $binmode = $opts{binmode} || $opts{encoding} || ':encoding(UTF-8)';
Condition80%4/5
$opts{'binmode'} || $opts{'encoding'} || ':encoding(UTF-8)'
l!l
10213
$opts{"binmode"} or $opts{"encoding"}
l!l&&r!l&&!r
102013
MC/DC50%1/2
$opts{'binmode'} || $opts{'encoding'} || ':encoding(UTF-8)'
$opts{"binmode"} $opts{"encoding"}
Truth table66%2/3
A or B
A= $opts{"binmode"}B= $opts{"encoding"}
ABresult
1X1
011
000
117 115 ▶ my $mode = $opts{mode} || 'r';
Condition100%2/2
$opts{'mode'} || 'r'
l!l
1132
MC/DC100%1/1
$opts{'mode'} || 'r'
$opts{'mode'}
Truth table100%2/2
A
A= $opts{'mode'}
Aresult
11
00
118 115 my $io;
119  
120 115 ▶ if (is_scalar_ref($arg)) {
Branch: 6 branches
BranchTrueFalseTotal
if (is_scalar_ref($arg)) { } 73 42 115
elsif (is_glob_ref(\$arg) or is_glob_ref($arg)) { } 17 25 42
elsif (is_string($arg)) { } 22 3 25
elsif (is_code_ref($arg) and $mode eq 'r') { } 1 2 3
elsif (is_code_ref($arg) and $mode eq 'w') { } 1 1 2
elsif (is_instance($arg, 'IO::Handle')) { } 1 0 1
121 73 $io = IO::Handle::Util::io_from_scalar_ref($arg);
122 73 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 73 0 73
123 }
124 ▶ elsif (is_glob_ref(\$arg) || is_glob_ref($arg)) {
Condition66%2/3
is_glob_ref(\$arg) or is_glob_ref($arg)
l!l&&r!l&&!r
01725
MC/DC50%1/2
is_glob_ref(\$arg) or is_glob_ref($arg)
is_glob_ref(\$arg) is_glob_ref($arg)
Truth table66%2/3
A or B
A= is_glob_ref(\$arg)B= is_glob_ref($arg)
ABresult
1X1
011
000
125 17 ▶ $io = IO::Handle->new_from_fd($arg, $mode) // $arg;
Condition66%2/3
"IO::Handle"->new_from_fd($arg, $mode) // $arg
l!l&&r!l&&!r
1340
MC/DC0%0/2
"IO::Handle"->new_from_fd($arg, $mode) // $arg
"IO::Handle"->new_from_fd($arg, $mode) $arg
Truth table66%2/3
A // B
A= "IO::Handle"->new_from_fd($arg, $mode)B= $arg
ABresult
1X1
011
000
126 17 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 17 0 17
127 }
128 elsif (is_string($arg)) {
129 22 $io = IO::File->new($arg, $mode);
130 22 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 22 0 22
131 }
132 ▶ elsif (is_code_ref($arg) && $mode eq 'r') {
Condition100%3/3
is_code_ref($arg) and $mode eq "r"
!ll&&!rl&&r
111
MC/DC100%2/2
is_code_ref($arg) and $mode eq "r"
is_code_ref($arg) $mode eq "r"
Truth table100%3/3
A and B
A= is_code_ref($arg)B= $mode eq "r"
ABresult
0X0
100
111
133 1 $io = IO::Handle::Util::io_from_getline($arg);
134 }
135 ▶ elsif (is_code_ref($arg) && $mode eq 'w') {
Condition66%2/3
is_code_ref($arg) and $mode eq "w"
!ll&&!rl&&r
101
MC/DC50%1/2
is_code_ref($arg) and $mode eq "w"
is_code_ref($arg) $mode eq "w"
Truth table66%2/3
A and B
A= is_code_ref($arg)B= $mode eq "w"
ABresult
0X0
100
111
136 1 $io = IO::Handle::Util::io_from_write_cb($arg);
137 }
138 elsif (is_instance($arg, 'IO::Handle')) {
139 1 $io = $arg;
140 1 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 1 0 1
141 }
142 else {
143 0 Catmandu::BadArg->throw("can't make io from argument");
144 }
145  
146 115 $io;
147 }
148  
149 # Deprecated use tools like File::Slurp::Tiny
150 3 ▶sub read_file {
Subroutine read_file: called CC=2 SCAR=7.0
Pod documented
151 3 my ($path) = @_;
152 3 local $/;
153 3 ▶ open my $fh, "<:encoding(UTF-8)", $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $path or "Catmandu::Error"->throw(qq[can't open "$path" for reading]) 3 0 3
154 or Catmandu::Error->throw(qq(can't open "$path" for reading));
155 3 my $str = <$fh>;
156 3 close $fh;
157 3 $str;
158 }
159  
160 3 ▶sub read_io {
Subroutine read_io: called CC=3 SCAR=11.0
Pod documented
161 3 my ($io) = @_;
162 3 ▶ $io->binmode("encoding(UTF-8)") if ($io->can('binmode'));
Branch: 1 branch
BranchTrueFalseTotal
if $io->can("binmode") 3 0 3
163 3 my @lines = ();
164 3 ▶ while (<$io>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $io)) 15 3 18
165 15 push @lines, $_;
166 }
167 3 $io->close();
168 3 join "", @lines;
169 }
170  
171 # Deprecated use tools like File::Slurp::Tiny
172 1 ▶sub write_file {
Subroutine write_file: called CC=2 SCAR=7.0
Pod documented
173 1 my ($path, $str) = @_;
174 1 ▶ open my $fh, ">:encoding(UTF-8)", $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $path or "Catmandu::Error"->throw(qq[can't open "$path" for writing]) 1 0 1
175 or Catmandu::Error->throw(qq(can't open "$path" for writing));
176 1 print $fh $str;
177 1 close $fh;
178 1 $path;
179 }
180  
181 1 ▶sub read_yaml {
Subroutine read_yaml: called CC=1 SCAR=0.0
Pod documented
182  
183 # dies on error
184 1 YAML::XS::LoadFile($_[0]);
185 }
186  
187 1 ▶sub read_json {
Subroutine read_json: called CC=1 SCAR=0.0
Pod documented
188 1 my $text = read_file($_[0]);
189  
190 # dies on error
191 1 Cpanel::JSON::XS->new->decode($text);
192 }
193  
194 ##
195 # Split a path on . or /, but not on \/ or \.
196 22 ▶sub split_path {
Subroutine split_path: called CC=1 SCAR=0.0
Pod undocumented
197 22 my ($path) = @_;
198 22 $path = trim($path);
199 22 $path =~ s/^\$[\.\/]//;
200 22 return [map {s/\\(?=[\.\/])//g; $_} split /(?<!\\)[\.\/]/, $path];
201 }
202  
203 1 ▶sub join_path {
Subroutine join_path: called CC=2 SCAR=6.9
Pod documented
204 1 my $path = File::Spec->catfile(@_);
205 1 $path =~ s!/\./!/!g;
206 1 ▶ while ($path =~ s![^/]*/\.\./!!) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[[^/]*/\.\./][]) 6 1 7
207 1 $path;
208 }
209  
210 1 ▶sub normalize_path { # taken from Dancer::FileUtils
Subroutine normalize_path: called CC=2 SCAR=6.9
Pod documented
211 1 my ($path) = @_;
212 1 $path =~ s!/\./!/!g;
213 1 ▶ while ($path =~ s![^/]*/\.\./!!) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[[^/]*/\.\./][]) 6 1 7
214 1 File::Spec->catfile($path);
215 }
216  
217 1 ▶sub segmented_path {
Subroutine segmented_path: called CC=3 SCAR=11.2
Pod documented
218 1 my ($id, %opts) = @_;
219 1 ▶ my $segment_size = $opts{segment_size} || 3;
Condition50%1/2
$opts{'segment_size'} || 3
l!l
10
MC/DC0%0/1
$opts{'segment_size'} || 3
$opts{'segment_size'}
Truth table50%1/2
A
A= $opts{'segment_size'}
Aresult
11
00
220 1 my $base_path = $opts{base_path};
221 1 $id =~ s/[^0-9a-zA-Z]+//g;
222 1 my @path = unpack "(A$segment_size)*", $id;
223 1 ▶ defined $base_path
Branch: 1 branch
BranchTrueFalseTotal
defined $base_path ? : 1 0 1
224 ? File::Spec->catdir($base_path, @path)
225 : File::Spec->catdir(@path);
226 }
227  
228 177 my $MIME_TYPES;
229  
230 0 ▶sub content_type {
Subroutine content_type: not called CC=4 SCAR=30.0
Pod documented
231 0 my ($filename) = @_;
232  
233 0 ▶ $MIME_TYPES ||= MIME::Types->new(only_complete => 1);
Condition0%0/3
$MIME_TYPES ||= "MIME::Types"->new("only_complete", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$MIME_TYPES ||= "MIME::Types"->new("only_complete", 1)
$MIME_TYPES "MIME::Types"->new("only_complete", 1)
Truth table0%0/3
A ||= B
A= $MIME_TYPESB= "MIME::Types"->new("only_complete", 1)
ABresult
1X1
011
000
234  
235 0 ▶ return undef unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
236  
237 0 my ($ext) = $filename =~ /\.(.+?)$/;
238  
239 0 my $type = 'application/octet-stream';
240  
241 0 my $mime = $MIME_TYPES->mimeTypeOf($ext);
242  
243 # Require explicit stringification!
244 0 ▶ $type = sprintf "%s", $mime->type if $mime;
Branch: 1 branch
BranchTrueFalseTotal
if $mime 0 0 0
245  
246 0 $type;
247 }
248  
249 1 ▶sub parse_data_path {
Subroutine parse_data_path: called CC=1 SCAR=0.0
Pod undocumented
250 1 my ($path) = @_;
251 1 check_string($path);
252 1 $path = split_path($path);
253 1 my $key = pop @$path;
254 1 return $path, $key;
255 }
256  
257 7 ▶sub get_data {
Subroutine get_data: called CC=10 SCAR=23.1
Pod undocumented
258 7 my ($data, $key) = @_;
259 7 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($data)) 5 2 7
260 5 ▶ if ($key eq '$first') {return unless @$data; $key = 0}
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq '$first') { } 1 4 5
elsif ($key eq '$last') { } 1 3 4
elsif ($key eq '*') { } 1 2 3
unless @$data 0 1 1
261 1 ▶ elsif ($key eq '$last') {return unless @$data; $key = @$data - 1}
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 1 1
262 1 elsif ($key eq '*') {return @$data}
263 4 ▶ if (array_exists($data, $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (array_exists($data, $key)) 3 1 4
264 3 return $data->[$key];
265 }
266 1 return;
267 }
268 2 ▶ if (is_hash_ref($data) && exists $data->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($data) and exists $data->{$key}) 1 1 2
Condition66%2/3
is_hash_ref($data) and exists $data->{$key}
!ll&&!rl&&r
011
MC/DC50%1/2
is_hash_ref($data) and exists $data->{$key}
is_hash_ref($data) exists $data->{$key}
Truth table66%2/3
A and B
A= is_hash_ref($data)B= exists $data->{$key}
ABresult
0X0
100
111
269 1 return $data->{$key};
270 }
271 1 return;
272 }
273  
274 15 ▶sub set_data {
Subroutine set_data: called CC=12 SCAR=24.9
Pod undocumented
275 15 my ($data, $key, @vals) = @_;
276 15 ▶ return unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 15 15
277 15 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($data)) 12 3 15
278 12 ▶ if ($key eq '$first') {return unless @$data; $key = 0}
Branch: 6 branches
BranchTrueFalseTotal
if ($key eq '$first') { } 2 10 12
elsif ($key eq '$last') { } 2 8 10
elsif ($key eq '$prepend') { } 2 6 8
elsif ($key eq '$append') { } 2 4 6
elsif ($key eq '*') { } 1 3 4
unless @$data 0 2 2
279 2 ▶ elsif ($key eq '$last') {return unless @$data; $key = @$data - 1}
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 2 2
280 elsif ($key eq '$prepend') {
281 2 unshift @$data, $vals[0];
282 2 return $vals[0];
283 }
284 2 elsif ($key eq '$append') {push @$data, $vals[0]; return $vals[0]}
285 1 elsif ($key eq '*') {return splice @$data, 0, @$data, @vals}
286 7 ▶ return $data->[$key] = $vals[0] if is_natural($key);
Branch: 1 branch
BranchTrueFalseTotal
if is_natural($key) 6 1 7
287 1 return;
288 }
289 3 ▶ if (is_hash_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($data)) 2 1 3
290 2 return $data->{$key} = $vals[0];
291 }
292 1 return;
293 }
294  
295 7 ▶sub delete_data {
Subroutine delete_data: called CC=10 SCAR=23.1
Pod undocumented
296 7 my ($data, $key) = @_;
297 7 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($data)) 5 2 7
298 5 ▶ if ($key eq '$first') {return unless @$data; $key = 0}
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq '$first') { } 1 4 5
elsif ($key eq '$last') { } 1 3 4
elsif ($key eq '*') { } 1 2 3
unless @$data 0 1 1
299 1 ▶ elsif ($key eq '$last') {return unless @$data; $key = @$data - 1}
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 1 1
300 1 elsif ($key eq '*') {return splice @$data, 0, @$data}
301 4 ▶ if (array_exists($data, $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (array_exists($data, $key)) 3 1 4
302 3 return splice @$data, $key, 1;
303 }
304 1 return;
305 }
306 2 ▶ if (is_hash_ref($data) && exists $data->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($data) and exists $data->{$key}) 1 1 2
Condition66%2/3
is_hash_ref($data) and exists $data->{$key}
!ll&&!rl&&r
101
MC/DC50%1/2
is_hash_ref($data) and exists $data->{$key}
is_hash_ref($data) exists $data->{$key}
Truth table66%2/3
A and B
A= is_hash_ref($data)B= exists $data->{$key}
ABresult
0X0
100
111
307 1 return delete $data->{$key};
308 }
309  
310 1 return;
311 }
312  
313 13 ▶sub data_at {
Subroutine data_at: called CC=27 SCAR=46.2
Pod undocumented
314 13 my ($path, $data, %opts) = @_;
315 13 ▶ if (ref $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path) { } 0 13 13
316 0 $path = [map {split_path($_)} @$path];
317 }
318 else {
319 13 $path = split_path($path);
320 }
321 13 my $create = $opts{create};
322 13 ▶ my $_key = $opts{_key} // $opts{key};
Condition33%1/3
$opts{"_key"} // $opts{"key"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
$opts{"_key"} // $opts{"key"}
$opts{"_key"} $opts{"key"}
Truth table33%1/3
A // B
A= $opts{"_key"}B= $opts{"key"}
ABresult
1X1
011
000
323 13 ▶ if (defined $opts{key} && $create && @$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"key"} and $create and @$path) 0 13 13
Condition33%2/6
defined $opts{"key"} and $create and @$path
!ll&&!rl&&r
1300
defined $opts{"key"} and $create
!ll&&!rl&&r
1300
MC/DC0%0/3
defined $opts{"key"} and $create and @$path
defined $opts{"key"} $create @$path
Truth table25%1/4
A and B and C
A= defined $opts{"key"}B= $createC= @$path
ABCresult
0XX0
10X0
1100
1111
324 0 push @$path, $_key;
325 }
326 13 my $key;
327 13 ▶ while (defined(my $key = shift @$path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @$path)) 18 13 31
328 18 ▶ is_ref($data) || return;
Branch: 1 branch
BranchTrueFalseTotal
is_ref($data) or return 18 0 18
329 18 ▶ if (is_array_ref($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_array_ref($data)) { } 5 13 18
elsif ($create and @$path) { } 0 13 13
330 5 ▶ if ($key eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '*') { } 0 5 5
331 return
332 0 map {data_at($path, $_, create => $create, _key => $_key)}
333 @$data;
334 }
335 else {
336 5 ▶ if ($key eq '$first') {$key = 0}
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq '$first') { } 1 4 5
elsif ($key eq '$last') { } 1 3 4
elsif ($key eq '$prepend') { } 1 2 3
elsif ($key eq '$append') { } 1 1 2
337 1 elsif ($key eq '$last') {$key = -1}
338 1 elsif ($key eq '$prepend') {unshift @$data, undef; $key = 0}
339 1 elsif ($key eq '$append') {push @$data, undef; $key = @$data}
340 5 ▶ is_integer($key) || return;
Branch: 1 branch
BranchTrueFalseTotal
is_integer($key) or return 5 0 5
341 5 ▶ if ($create && @$path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create and @$path) { } 0 5 5
Condition33%1/3
$create and @$path
!ll&&!rl&&r
500
MC/DC0%0/2
$create and @$path
$create @$path
Truth table33%1/3
A and B
A= $createB= @$path
ABresult
0X0
100
111
342 0 ▶ $data = $data->[$key] ||= is_integer($path->[0])
Branch: 1 branch
BranchTrueFalseTotal
is_integer($path->[0]) || ord $path->[0] == 36 ? : 0 0 0
Condition0%0/6
$data->[$key] ||= is_integer($path->[0]) || ord $path->[0] == 36 ? [] : {}
l!l&&r!l&&!r
000
is_integer($path->[0]) or ord $path->[0] == 36
l!l&&r!l&&!r
000
MC/DC0%0/3
$data->[$key] ||= is_integer($path->[0]) || ord $path->[0] == 36 ? [] : {}
$data->[$key] is_integer($path->[0]) ord $path->[0] == 36
Truth table0%0/4
A ||= B or C
A= $data->[$key]B= is_integer($path->[0])C= ord $path->[0] == 36
ABCresult
1XX1
01X1
0011
0000
343 || ord($path->[0]) == ord('$') ? [] : {};
344 }
345 else {
346 5 $data = $data->[$key];
347 }
348 }
349 }
350 ▶ elsif ($create && @$path) {
Condition33%1/3
$create and @$path
!ll&&!rl&&r
1300
MC/DC0%0/2
$create and @$path
$create @$path
Truth table33%1/3
A and B
A= $createB= @$path
ABresult
0X0
100
111
351 0 ▶ $data = $data->{$key} ||= is_integer($path->[0])
Branch: 1 branch
BranchTrueFalseTotal
is_integer($path->[0]) || ord $path->[0] == 36 ? : 0 0 0
Condition0%0/6
$data->{$key} ||= is_integer($path->[0]) || ord $path->[0] == 36 ? [] : {}
l!l&&r!l&&!r
000
is_integer($path->[0]) or ord $path->[0] == 36
l!l&&r!l&&!r
000
MC/DC0%0/3
$data->{$key} ||= is_integer($path->[0]) || ord $path->[0] == 36 ? [] : {}
$data->{$key} is_integer($path->[0]) ord $path->[0] == 36
Truth table0%0/4
A ||= B or C
A= $data->{$key}B= is_integer($path->[0])C= ord $path->[0] == 36
ABCresult
1XX1
01X1
0011
0000
352 || ord($path->[0]) == ord('$') ? [] : {};
353 }
354 else {
355 13 $data = $data->{$key};
356 }
357 18 ▶ if ($create && @$path == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create and @$path == 1) 0 18 18
Condition33%1/3
$create and @$path == 1
!ll&&!rl&&r
1800
MC/DC0%0/2
$create and @$path == 1
$create @$path == 1
Truth table33%1/3
A and B
A= $createB= @$path == 1
ABresult
0X0
100
111
358 0 last;
359 }
360 }
361 13 $data;
362 }
363  
364 10 ▶sub array_exists {
Subroutine array_exists: called CC=2 SCAR=7.2
Pod documented
365 10 my ($arr, $i) = @_;
366 10 ▶ is_natural($i) && $i < @$arr;
Branch: 1 branch
BranchTrueFalseTotal
is_natural($i) and $i < @$arr 10 0 10
367 }
368  
369 1 ▶sub array_group_by {
Subroutine array_group_by: called CC=1 SCAR=0.0
Pod documented
370 1 my ($arr, $key) = @_;
371 4 ▶ List::Util::reduce {
Subroutine __ANON__: called CC=3 SCAR=11.0
372 4 my $k = $b->{$key};
373 4 ▶ push @{$a->{$k} ||= []}, $b if defined $k;
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 3 1 4
Condition50%1/2
$a->{$k} ||= []
l!l
03
MC/DC0%0/1
$a->{$k} ||= []
$a->{$k}
Truth table50%1/2
A
A= $a->{$k}
Aresult
11
00
374 4 $a
375 }
376 1 {}, @$arr;
377 }
378  
379 1 ▶sub array_pluck {
Subroutine array_pluck: called CC=1 SCAR=0.0
Pod documented
380 1 my ($arr, $key) = @_;
381 1 my @vals = map {$_->{$key}} @$arr;
382 1 \@vals;
383 }
384  
385 7 ▶sub array_to_sentence {
Subroutine array_to_sentence: called CC=4 SCAR=14.1
Pod documented
386 7 my ($arr, $join, $join_last) = @_;
387 7 ▶ $join //= ', ';
Condition50%1/2
$join //= ", "
l!l
07
MC/DC0%0/1
$join //= ", "
$join
Truth table50%1/2
A
A= $join
Aresult
11
00
388 7 ▶ $join_last //= ' and ';
Condition50%1/2
$join_last //= " and "
l!l
07
MC/DC0%0/1
$join_last //= " and "
$join_last
Truth table50%1/2
A
A= $join_last
Aresult
11
00
389 7 my $size = scalar @$arr;
390 7 ▶ $size > 2
Branch: 1 branch
BranchTrueFalseTotal
$size > 2 ? : 1 6 7
391 ? join($join_last, join($join, @$arr[0 .. $size - 2]), $arr->[-1])
392 : join($join_last, @$arr);
393 }
394  
395 1 ▶sub array_sum {
Subroutine array_sum: called CC=1 SCAR=0.0
Pod documented
396 1 List::Util::sum(0, @{$_[0]});
397 }
398  
399 2 ▶sub array_includes {
Subroutine array_includes: called CC=3 SCAR=11.0
Pod documented
400 2 my ($arr, $val) = @_;
401 2 ▶ is_same($val, $_) && return 1 for @$arr;
Branch: 1 branch
BranchTrueFalseTotal
is_same($val, $_) and return 1 1 1 2
402 1 0;
403 }
404  
405 2 ▶sub array_any {
Subroutine array_any: called CC=3 SCAR=11.0
Pod documented
406 2 my ($arr, $sub) = @_;
407 2 ▶ $sub->($_) && return 1 for @$arr;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($_) and return 1 1 5 6
408 1 0;
409 }
410  
411 1 ▶sub array_rest {
Subroutine array_rest: called CC=2 SCAR=7.2
Pod documented
412 1 my ($arr) = @_;
413 1 ▶ @$arr < 2 ? [] : [@$arr[1 .. (@$arr - 1)]];
Branch: 1 branch
BranchTrueFalseTotal
@$arr < 2 ? : 0 1 1
414 }
415  
416 1 ▶sub array_uniq {
Subroutine array_uniq: called CC=1 SCAR=0.0
Pod documented
417 1 my ($arr) = @_;
418 1 my %seen = ();
419 1 my @vals = grep {not $seen{$_}++} @$arr;
420 1 \@vals;
421 }
422  
423 20 ▶sub array_split {
Subroutine array_split: called CC=2 SCAR=6.9
Pod documented
424 20 my ($arr) = @_;
425 20 ▶ is_array_ref($arr) ? $arr : [split ',', $arr];
Branch: 1 branch
BranchTrueFalseTotal
is_array_ref($arr) ? : 12 8 20
426 }
427  
428 31 ▶sub as_utf8 {
Subroutine as_utf8: called CC=1 SCAR=0.0
Pod documented
429 31 my $str = $_[0];
430 31 utf8::upgrade($str);
431 31 $str;
432 }
433  
434 684 ▶sub trim {
Subroutine trim: called CC=2 SCAR=6.9
Pod documented
435 684 my $str = $_[0];
436 684 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 677 7 684
437 677 $str =~ s/^[\h\v]+//s;
438 677 $str =~ s/[\h\v]+$//s;
439 }
440 684 $str;
441 }
442  
443 1 ▶sub capitalize {
Subroutine capitalize: called CC=1 SCAR=0.0
Pod documented
444 1 my $str = $_[0];
445 1 utf8::upgrade($str);
446 1 ucfirst lc $str;
447 }
448  
449 1 ▶sub is_different {
Subroutine is_different: called CC=1 SCAR=0.0
Pod documented
450 1 !is_same(@_);
451 }
452  
453 2 ▶sub check_same {
Subroutine check_same: called CC=2 SCAR=6.9
Pod documented
454 2 ▶ is_same(@_) || Catmandu::BadVal->throw('should be same');
Branch: 1 branch
BranchTrueFalseTotal
is_same(@_) or "Catmandu::BadVal"->throw("should be same") 1 1 2
455 1 $_[0];
456 }
457  
458 2 ▶sub check_different {
Subroutine check_different: called CC=2 SCAR=6.9
Pod documented
459 2 ▶ is_same(@_) && Catmandu::BadVal->throw('should be different');
Branch: 1 branch
BranchTrueFalseTotal
is_same(@_) and "Catmandu::BadVal"->throw("should be different") 1 1 2
460 1 $_[0];
461 }
462  
463 22 ▶sub is_bool {
Subroutine is_bool: called CC=7 SCAR=24.4
Pod undocumented
464 22 my $obj = $_[0];
465 22 ▶ Scalar::Util::blessed($obj) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) or return 0 8 14 22
466 8 ▶ eval {
Condition50%1/2
eval { do { $obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean") or $obj->isa("JSON::XS::Boolean") or $obj->isa("Cpanel::JSON::XS::Boolean") or $obj->isa("JSON::PP::Boolean") } } // 0
l!l
80
MC/DC0%0/1
eval { do { $obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean") or $obj->isa("JSON::XS::Boolean") or $obj->isa("Cpanel::JSON::XS::Boolean") or $obj->isa("JSON::PP::Boolean") } } // 0
eval { do { $obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean") or $obj->isa("JSON::XS::Boolean") or $obj->isa("Cpanel::JSON::XS::Boolean") or $obj->isa("JSON::PP::Boolean") } }
Truth table50%1/2
A
A= eval { do { $obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean") or $obj->isa("JSON::XS::Boolean") or $obj->isa("Cpanel::JSON::XS::Boolean") or $obj->isa("JSON::PP::Boolean") } }
Aresult
11
00
467 8 ▶ $obj->isa('boolean')
Condition33%4/12
$obj->isa('boolean') || $obj->isa('Types::Serialiser::Boolean') || $obj->isa('JSON::XS::Boolean') || $obj->isa('Cpanel::JSON::XS::Boolean') || $obj->isa('JSON::PP::Boolean')
l!l&&r!l&&!r
080
$obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean") or $obj->isa("JSON::XS::Boolean") or $obj->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
008
$obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean") or $obj->isa("JSON::XS::Boolean")
l!l&&r!l&&!r
008
$obj->isa("boolean") or $obj->isa("Types::Serialiser::Boolean")
l!l&&r!l&&!r
008
MC/DC0%0/5
$obj->isa('boolean') || $obj->isa('Types::Serialiser::Boolean') || $obj->isa('JSON::XS::Boolean') || $obj->isa('Cpanel::JSON::XS::Boolean') || $obj->isa('JSON::PP::Boolean')
$obj->isa("boolean") $obj->isa("Types::Serialiser::Boolean") $obj->isa("JSON::XS::Boolean") $obj->isa("Cpanel::JSON::XS::Boolean") $obj->isa('JSON::PP::Boolean')
Truth table16%1/6
A or B or C or D || E
A= $obj->isa("boolean")B= $obj->isa("Types::Serialiser::Boolean")C= $obj->isa("JSON::XS::Boolean")D= $obj->isa("Cpanel::JSON::XS::Boolean")E= $obj->isa('JSON::PP::Boolean')
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
468 || $obj->isa('Types::Serialiser::Boolean')
469 || $obj->isa('JSON::XS::Boolean')
470 || $obj->isa('Cpanel::JSON::XS::Boolean')
471 || $obj->isa('JSON::PP::Boolean');
472 } // 0;
473 }
474  
475 911 ▶sub is_integer {
Subroutine is_integer: called CC=2 SCAR=6.9
Pod documented
476 911 ▶ Data::Util::is_integer($_[0]) && $_[0] !~ /^0[0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
Data::Util::is_integer($_[0]) and not $_[0] =~ /^0[0-9]/ 84 827 911
477 }
478  
479 884 ▶sub is_natural {
Subroutine is_natural: called CC=2 SCAR=6.9
Pod documented
480 884 ▶ is_integer($_[0]) && $_[0] >= 0;
Branch: 1 branch
BranchTrueFalseTotal
is_integer($_[0]) and $_[0] >= 0 63 821 884
481 }
482  
483 21 ▶sub is_positive {
Subroutine is_positive: called CC=2 SCAR=6.9
Pod documented
484 21 ▶ is_integer($_[0]) && $_[0] >= 1;
Branch: 1 branch
BranchTrueFalseTotal
is_integer($_[0]) and $_[0] >= 1 15 6 21
485 }
486  
487 0 ▶sub is_float {
Subroutine is_float: not called CC=3 SCAR=24.8
Pod documented
488 0 ▶ is_value($_[0])
Branch: 1 branch
BranchTrueFalseTotal
is_value($_[0]) and $_[0] =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ and not $_[0] =~ /^0[0-9]/ 0 0 0
Condition0%0/3
is_value($_[0]) and $_[0] =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
!ll&&!rl&&r
000
MC/DC0%0/2
is_value($_[0]) and $_[0] =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
is_value($_[0]) $_[0] =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
Truth table0%0/3
A and B
A= is_value($_[0])B= $_[0] =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
ABresult
0X0
100
111
489 && $_[0] =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
490 && $_[0] !~ /^0[0-9]/;
491 }
492  
493 981 ▶sub is_ref {
Subroutine is_ref: called CC=2 SCAR=6.9
Pod documented
494 981 ▶ ref $_[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 18 963 981
495 }
496  
497 57 ▶sub is_able {
Subroutine is_able: called CC=4 SCAR=13.9
Pod documented
498 57 my $obj = shift;
499 57 ▶ is_invocant($obj) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
is_invocant($obj) or return 0 43 14 57
500 43 ▶ $obj->can($_) || return 0 for @_;
Branch: 1 branch
BranchTrueFalseTotal
$obj->can($_) or return 0 40 3 43
501 40 1;
502 }
503  
504 24 ▶sub check_able {
Subroutine check_able: called CC=2 SCAR=6.9
Pod documented
505 24 my $obj = shift;
506 24 ▶ return $obj if is_able($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_able($obj, @_) 22 2 24
507 2 Catmandu::BadVal->throw('should be able to ' . array_to_sentence(\@_));
508 }
509  
510 2 ▶sub check_maybe_able {
Subroutine check_maybe_able: called CC=2 SCAR=6.9
Pod documented
511 2 my $obj = shift;
512 2 ▶ return $obj if is_maybe_able($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_maybe_able($obj, @_) 1 1 2
513 1 Catmandu::BadVal->throw(
514 'should be undef or able to ' . array_to_sentence(\@_));
515 }
516  
517 186 ▶sub is_instance {
Subroutine is_instance: called CC=4 SCAR=13.9
Pod documented
518 186 my $obj = shift;
519 186 ▶ Scalar::Util::blessed($obj) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) or return 0 41 145 186
520 41 for my $pkg (@_) {
521 30 ▶ eval {$obj->isa($pkg)} || return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $obj->isa($pkg) } } or return 0 25 5 30
522 }
523 36 1;
524 }
525  
526 2 ▶sub check_instance {
Subroutine check_instance: called CC=2 SCAR=6.9
Pod documented
527 2 my $obj = shift;
528 2 ▶ return $obj if is_instance($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_instance($obj, @_) 1 1 2
529 1 Catmandu::BadVal->throw(
530 'should be instance of ' . array_to_sentence(\@_));
531 }
532  
533 2 ▶sub check_maybe_instance {
Subroutine check_maybe_instance: called CC=2 SCAR=6.9
Pod documented
534 2 my $obj = shift;
535 2 ▶ return $obj if is_maybe_instance($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_maybe_instance($obj, @_) 1 1 2
536 1 Catmandu::BadVal->throw(
537 'should be undef or instance of ' . array_to_sentence(\@_));
538 }
539  
540 177 Data::Util::install_subroutine(
541 __PACKAGE__,
542 hash_merge => \&Hash::Merge::Simple::merge,
543 is_same => \&Data::Compare::Compare,
544 is_invocant => \&Data::Util::is_invocant,
545 is_scalar_ref => \&Data::Util::is_scalar_ref,
546 is_array_ref => \&Data::Util::is_array_ref,
547 is_hash_ref => \&Data::Util::is_hash_ref,
548 is_code_ref => \&Data::Util::is_code_ref,
549 is_regex_ref => \&Data::Util::is_rx,
550 is_glob_ref => \&Data::Util::is_glob_ref,
551 is_value => \&Data::Util::is_value,
552 is_string => \&Data::Util::is_string,
553 is_number => \&Data::Util::is_number,
554 );
555  
556 177 for my $sym (
557 qw(able instance invocant ref
558 scalar_ref array_ref hash_ref code_ref regex_ref glob_ref
559 bool value string number integer natural positive float)
560 )
561 {
562 3186 my $err_name = $sym;
563 3186 $err_name =~ s/_/ /;
564  
565 3186 push @EXPORT_OK, "is_$sym", "is_maybe_$sym", "check_$sym",
566 "check_maybe_$sym";
567 3186 push @{$EXPORT_TAGS{is}}, "is_$sym", "is_maybe_$sym";
568 3186 push @{$EXPORT_TAGS{check}}, "check_$sym", "check_maybe_$sym";
569  
570 3186 ▶ unless (Data::Util::get_code_ref(__PACKAGE__, "is_maybe_$sym")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Util::get_code_ref(__PACKAGE__, "is_maybe_$sym")) 3186 0 3186
571 3186 my $sub
572 = Sub::Quote::quote_sub("!defined(\$_[0]) || is_$sym(\$_[0])");
573 3186 Data::Util::install_subroutine(__PACKAGE__, "is_maybe_$sym" => $sub);
574 }
575  
576 3186 ▶ unless (Data::Util::get_code_ref(__PACKAGE__, "check_$sym")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Util::get_code_ref(__PACKAGE__, "check_$sym")) 2832 354 3186
577 2832 my $sub
578 = Sub::Quote::quote_sub(
579 "is_$sym(\$_[0]) || Catmandu::BadVal->throw('should be $err_name'); \$_[0]"
580 );
581 2832 Data::Util::install_subroutine(__PACKAGE__, "check_$sym" => $sub);
582 }
583  
584 3186 ▶ unless (Data::Util::get_code_ref(__PACKAGE__, "check_maybe_$sym")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Util::get_code_ref(__PACKAGE__, "check_maybe_$sym")) 2832 354 3186
585 2832 my $sub
586 = Sub::Quote::quote_sub(
587 "is_maybe_$sym(\$_[0]) || Catmandu::BadVal->throw('should be undef or $err_name'); \$_[0]"
588 );
589 2832 Data::Util::install_subroutine(__PACKAGE__,
590 "check_maybe_$sym" => $sub);
591 }
592 }
593  
594 1 ▶sub human_number { # taken from Number::Format
Subroutine human_number: called CC=2 SCAR=7.0
Pod documented
595 1 my $num = $_[0];
596  
597 # add leading 0's so length($num) is divisible by 3
598 1 $num = '0' x (3 - (length($num) % 3)) . $num;
599  
600 # split $num into groups of 3 characters and insert commas
601 1 $num = join ',', grep {$_ ne ''} split /(...)/, $num;
602  
603 # strip off leading zeroes and/or comma
604 1 $num =~ s/^0+,?//;
605 1 ▶ length $num ? $num : '0';
Branch: 1 branch
BranchTrueFalseTotal
length $num ? : 1 0 1
606 }
607  
608 4 ▶sub human_byte_size {
Subroutine human_byte_size: called CC=4 SCAR=13.9
Pod documented
609 4 my ($size) = @_;
610 4 ▶ if ($size > 1000000000) {
Branch: 3 branches
BranchTrueFalseTotal
if ($size > 1000000000) { } 1 3 4
elsif ($size > 1000000) { } 1 2 3
elsif ($size > 1000) { } 1 1 2
611 1 return sprintf("%.2f GB", $size / 1000000000);
612 }
613 elsif ($size > 1000000) {
614 1 return sprintf("%.2f MB", $size / 1000000);
615 }
616 elsif ($size > 1000) {
617 1 return sprintf("%.2f KB", $size / 1000);
618 }
619 1 "$size bytes";
620 }
621  
622 1 ▶sub human_content_type {
Subroutine human_content_type: called CC=3 SCAR=13.3
Pod documented
623 1 my ($content_type, $default) = @_;
624 1 my ($key) = $content_type =~ /^([^;]+)/;
625 1 ▶ $HUMAN_CONTENT_TYPES->{$key} // $default // $content_type;
Condition33%2/6
($HUMAN_CONTENT_TYPES->{$key} // $default) // $content_type
l!l&&r!l&&!r
100
$HUMAN_CONTENT_TYPES->{$key} // $default
l!l&&r!l&&!r
100
MC/DC0%0/3
($HUMAN_CONTENT_TYPES->{$key} // $default) // $content_type
$HUMAN_CONTENT_TYPES->{$key} $default $content_type
Truth table25%1/4
A // B // C
A= $HUMAN_CONTENT_TYPES->{$key}B= $defaultC= $content_type
ABCresult
1XX1
01X1
0011
0000
626 }
627  
628 1 ▶sub xml_declaration {
Subroutine xml_declaration: called CC=1 SCAR=0.0
Pod documented
629 1 $XML_DECLARATION;
630 }
631  
632 1 ▶sub xml_escape {
Subroutine xml_escape: called CC=1 SCAR=0.0
Pod documented
633 1 my ($str) = @_;
634 1 utf8::upgrade($str);
635  
636 1 $str =~ s/&/&amp;/go;
637 1 $str =~ s/</&lt;/go;
638 1 $str =~ s/>/&gt;/go;
639 1 $str =~ s/"/&quot;/go;
640 1 $str =~ s/'/&apos;/go;
641  
642 # remove control chars
643 1 $str
644 =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
645  
646 1 $str;
647 }
648  
649 2 ▶sub use_lib {
Subroutine use_lib: called CC=2 SCAR=7.0
Pod documented
650 2 my (@dirs) = @_;
651  
652 177 ▶ use lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
653 2 local $@;
654 2 lib->import(@dirs);
655 2 ▶ Catmandu::Error->throw($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
656  
657 2 1;
658 }
659  
660 962 ▶sub pod_section {
Subroutine pod_section: called CC=5 SCAR=18.0
Pod documented
661 962 ▶ my $class = is_ref($_[0]) ? ref(shift) : shift;
Branch: 1 branch
BranchTrueFalseTotal
is_ref($_[0]) ? : 0 962 962
662 962 my $section = uc(shift);
663  
664 962 ▶ unless (-r $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $class) 0 962 962
665 0 $class =~ s!::!/!g;
666 0 $class .= '.pm';
667 0 ▶ $class = $INC{$class} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$class = $INC{$class} or return "" 0 0 0
668 }
669  
670 962 my $text = "";
671 962 ▶ open my $input, "<", $class or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<", $class or return "" 962 0 962
672 962 open my $output, ">", \$text;
673  
674 962 require Pod::Usage; # lazy load only if needed
675 962 Pod::Usage::pod2usage(
676 -input => $input,
677 -output => $output,
678 -sections => $section,
679 -exit => "NOEXIT",
680 -verbose => 99,
681 -indent => 0,
682 -utf8 => 1,
683 @_
684 );
685 962 $section = ucfirst(lc($section));
686 962 $text =~ s/$section:\n//m;
687 962 chomp $text;
688  
689 962 $text;
690 }
691  
692 884 ▶sub require_package {
Subroutine require_package: called CC=6 SCAR=18.2
Pod documented
693 884 my ($pkg, $ns) = @_;
694  
695 884 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) 812 72 884
696 812 ▶ unless ($pkg =~ s/^\+// || $pkg =~ /^$ns/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg =~ s/^\+// or $pkg =~ /^$ns/) 812 0 812
Condition33%1/3
$pkg =~ s/^\+// or $pkg =~ /^$ns/
l!l&&r!l&&!r
00812
MC/DC0%0/2
$pkg =~ s/^\+// or $pkg =~ /^$ns/
$pkg =~ s/^\+// $pkg =~ /^$ns/
Truth table33%1/3
A or B
A= $pkg =~ s/^\+//B= $pkg =~ /^$ns/
ABresult
1X1
011
000
697 812 $pkg = "${ns}::$pkg";
698 }
699 }
700  
701 884 ▶ return $pkg if is_invocant($pkg);
Branch: 1 branch
BranchTrueFalseTotal
if is_invocant($pkg) 614 270 884
702  
703 270 ▶ eval "require $pkg;1;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $pkg;1;" or "Catmandu::NoSuchPackage"->throw("message", "No such package: $pkg", "package_name", $pkg) 264 6 270
704 or Catmandu::NoSuchPackage->throw(
705 message => "No such package: $pkg",
706 package_name => $pkg
707 );
708  
709 264 $pkg;
710 }
711  
712 7 ▶sub now {
Subroutine now: called CC=4 SCAR=13.9
Pod documented
713 7 my $format = $_[0];
714 7 my $now;
715  
716 7 ▶ if (!defined $format || $format eq 'iso_date_time') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $format or $format eq 'iso_date_time') { } 5 2 7
elsif ($format eq 'iso_date_time_millis') { } 1 1 2
Condition100%3/3
not defined $format or $format eq "iso_date_time"
l!l&&r!l&&!r
142
MC/DC100%2/2
not defined $format or $format eq "iso_date_time"
not defined $format $format eq "iso_date_time"
Truth table100%3/3
not(A) or B
A= not defined $formatB= $format eq "iso_date_time"
ABresult
1X1
011
000
717 5 $now = POSIX::strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(time));
718 }
719 elsif ($format eq 'iso_date_time_millis') {
720 1 my $t = Time::HiRes::time;
721 1 $now = POSIX::strftime('%Y-%m-%dT%H:%M:%S', gmtime($t));
722 1 $now .= sprintf('.%03d', ($t - int($t)) * 1000);
723 1 $now .= 'Z';
724 }
725 else {
726 1 $now = POSIX::strftime($format, gmtime(time));
727 }
728 }
729  
730 177 1;
731  
732 __END__
« Catmandu/Cmd/help.pm ↑ Summary Catmandu/Searchable.pm »