blib/lib/Catmandu/Util.pm

filter: Statementstmt 97.5% 363 / 372 Branchbran 82.2% 181 / 220 Conditioncond 47.9% 46 / 96 MC/DCmcdc 26.4% 14 / 53 Subroutinesub 98.6% 72 / 73 Podpod 88.4% 46 / 52 totaltotal 83.3% 722 / 866 CC 146 SCAR 27.1
CC
146
Cov
86%
data_at
79.1
io
18.8
set_data
12.1
CRAP 15.0
« blib/lib/Catmandu/Cmd/info.pm ↑ Summary blib/lib/Catmandu/Cmd/copy.pm »
1 package Catmandu::Util;
2  
3 145 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 145 our $VERSION = '1.0201_02';
6  
7 145 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 145 ▶use Sub::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 145 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 145 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 145 ▶use Ref::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 145 ▶use Data::Compare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 145 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 145 ▶use IO::Handle::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 145 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 145 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 145 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 145 ▶use Hash::Merge::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 145 our %EXPORT_TAGS = (
21 io => [
22 qw(io read_file read_io write_file read_yaml read_json join_path
23 normalize_path segmented_path)
24 ],
25 data => [qw(parse_data_path get_data set_data delete_data data_at)],
26 array => [
27 qw(array_exists array_group_by array_pluck array_to_sentence
28 array_sum array_includes array_any array_rest array_uniq array_split)
29 ],
30 hash => [qw(hash_merge)],
31 string => [qw(as_utf8 trim capitalize)],
32 is => [qw(is_same is_different)],
33 check => [qw(check_same check_different)],
34 human => [qw(human_number human_content_type human_byte_size)],
35 xml => [qw(xml_declaration xml_escape)],
36 misc => [qw(require_package use_lib pod_section)],
37 );
38  
39 145 our @EXPORT_OK = map {@$_} values %EXPORT_TAGS;
40  
41 145 $EXPORT_TAGS{all} = \@EXPORT_OK;
42  
43 145 my $HUMAN_CONTENT_TYPES = {
44  
45 # txt
46 'text/plain' => 'Text',
47 'application/txt' => 'Text',
48  
49 # pdf
50 'application/pdf' => 'PDF',
51 'application/x-pdf' => 'PDF',
52 'application/acrobat' => 'PDF',
53 'applications/vnd.pdf' => 'PDF',
54 'text/pdf' => 'PDF',
55 'text/x-pdf' => 'PDF',
56  
57 # doc
58 'application/doc' => 'Word',
59 'application/vnd.msword' => 'Word',
60 'application/vnd.ms-word' => 'Word',
61 'application/winword' => 'Word',
62 'application/word' => 'Word',
63 'application/x-msw6' => 'Word',
64 'application/x-msword' => 'Word',
65  
66 # docx
67 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
68 => 'Word',
69  
70 # xls
71 'application/vnd.ms-excel' => 'Excel',
72 'application/msexcel' => 'Excel',
73 'application/x-msexcel' => 'Excel',
74 'application/x-ms-excel' => 'Excel',
75 'application/vnd.ms-excel' => 'Excel',
76 'application/x-excel' => 'Excel',
77 'application/x-dos_ms_excel' => 'Excel',
78 'application/xls' => 'Excel',
79  
80 # xlsx
81 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' =>
82 'Excel',
83  
84 # ppt
85 'application/vnd.ms-powerpoint' => 'PowerPoint',
86 'application/mspowerpoint' => 'PowerPoint',
87 'application/ms-powerpoint' => 'PowerPoint',
88 'application/mspowerpnt' => 'PowerPoint',
89 'application/vnd-mspowerpoint' => 'PowerPoint',
90 'application/powerpoint' => 'PowerPoint',
91 'application/x-powerpoint' => 'PowerPoint',
92  
93 # pptx
94 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
95 => 'PowerPoint',
96  
97 # csv
98 'text/comma-separated-values' => 'CSV',
99 'text/csv' => 'CSV',
100 'application/csv' => 'CSV',
101  
102 # zip
103 'application/zip' => 'ZIP archive',
104 };
105  
106 145 my $XML_DECLARATION = qq(<?xml version="1.0" encoding="UTF-8"?>\n);
107  
108 ▶sub TIESCALAR { }
Subroutine TIESCALAR: not called CC=1 SCAR=0.0
109  
110 sub io {
111 79 ▶ my ($arg, %opts) = @_;
Subroutine io: called CC=18 SCAR=29.3
Pod documented
112 79 ▶ my $binmode = $opts{binmode} || $opts{encoding} || ':encoding(UTF-8)';
Condition80%4/5
$opts{'binmode'} || $opts{'encoding'} || ':encoding(UTF-8)'
l!l
6514
$opts{"binmode"} or $opts{"encoding"}
l!l&&r!l&&!r
65014
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
113 79 ▶ my $mode = $opts{mode} || 'r';
Condition100%2/2
$opts{'mode'} || 'r'
l!l
763
MC/DC100%1/1
$opts{'mode'} || 'r'
$opts{'mode'}
Truth table100%2/2
A
A= $opts{'mode'}
Aresult
11
00
114 79 my $io;
115  
116 79 ▶ if (is_scalar_ref($arg)) {
Branch: 6 branches
BranchTrueFalseTotal
if (&is_scalar_ref($arg)) { } 55 24 79
elsif (&is_glob_ref(\$arg) or &is_glob_ref($arg)) { } 10 14 24
elsif (is_string($arg)) { } 10 4 14
elsif (&is_code_ref($arg) and $mode eq 'r') { } 1 3 4
elsif (&is_code_ref($arg) and $mode eq 'w') { } 1 2 3
elsif (is_instance($arg, 'IO::Handle')) { } 1 1 2
117 55 $io = IO::Handle::Util::io_from_scalar_ref($arg);
118 55 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 55 0 55
119 }
120 ▶ 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
01014
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
121 10 ▶ $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
730
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
122 10 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 10 0 10
123 }
124 elsif (is_string($arg)) {
125 10 $io = IO::File->new($arg, $mode);
126 10 ▶ defined($io) && binmode $io, $binmode;
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 10 0 10
127 }
128 ▶ elsif (is_code_ref($arg) && $mode eq 'r') {
Condition100%3/3
&is_code_ref($arg) and $mode eq "r"
!ll&&!rl&&r
211
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
129 1 $io = IO::Handle::Util::io_from_getline($arg);
130 }
131 ▶ elsif (is_code_ref($arg) && $mode eq 'w') {
Condition66%2/3
&is_code_ref($arg) and $mode eq "w"
!ll&&!rl&&r
201
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
132 1 $io = IO::Handle::Util::io_from_write_cb($arg);
133 }
134 elsif (is_instance($arg, 'IO::Handle')) {
135 1 $io = $arg;
136 1 ▶ defined($io) && binmode $io, $binmode;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $io and binmode $io, $binmode 1 0 1
137 }
138 else {
139 1 Catmandu::BadArg->throw("can't make io from argument");
140 }
141  
142 78 $io;
143 }
144  
145 # Deprecated use tools like File::Slurp::Tiny
146 sub read_file {
147 3 ▶ my ($path) = @_;
Subroutine read_file: called CC=2 SCAR=7.0
Pod documented
148 3 local $/;
149 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]) 0 3 3
150 or Catmandu::Error->throw(qq(can't open "$path" for reading));
151 3 my $str = <$fh>;
152 3 close $fh;
153 3 $str;
154 }
155  
156 sub read_io {
157 3 ▶ my ($io) = @_;
Subroutine read_io: called CC=3 SCAR=11.0
Pod documented
158 3 ▶ $io->binmode("encoding(UTF-8)") if ($io->can('binmode'));
Branch: 1 branch
BranchTrueFalseTotal
if $io->can("binmode") 3 0 3
159 3 my @lines = ();
160 3 ▶ while (<$io>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $io)) 15 3 18
161 15 push @lines, $_;
162 }
163 3 $io->close();
164 3 join "", @lines;
165 }
166  
167 # Deprecated use tools like File::Slurp::Tiny
168 sub write_file {
169 1 ▶ my ($path, $str) = @_;
Subroutine write_file: called CC=2 SCAR=7.0
Pod documented
170 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]) 0 1 1
171 or Catmandu::Error->throw(qq(can't open "$path" for writing));
172 1 print $fh $str;
173 1 close $fh;
174 1 $path;
175 }
176  
177 sub read_yaml {
178  
179 # dies on error
180 1 ▶ YAML::XS::LoadFile($_[0]);
Subroutine read_yaml: called CC=1 SCAR=0.0
Pod documented
181 }
182  
183 sub read_json {
184 1 ▶ my $text = read_file($_[0]);
Subroutine read_json: called CC=1 SCAR=0.0
Pod documented
185  
186 # dies on error
187 1 Cpanel::JSON::XS->new->decode($text);
188 }
189  
190 sub join_path {
191 1 ▶ my $path = File::Spec->catfile(@_);
Subroutine join_path: called CC=2 SCAR=6.9
Pod documented
192 1 $path =~ s!/\./!/!g;
193 1 ▶ while ($path =~ s![^/]*/\.\./!!) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[[^/]*/\.\./][]) 6 1 7
194 1 $path;
195 }
196  
197 sub normalize_path { # taken from Dancer::FileUtils
198 1 ▶ my ($path) = @_;
Subroutine normalize_path: called CC=2 SCAR=6.9
Pod documented
199 1 $path =~ s!/\./!/!g;
200 1 ▶ while ($path =~ s![^/]*/\.\./!!) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[[^/]*/\.\./][]) 6 1 7
201 1 File::Spec->catfile($path);
202 }
203  
204 sub segmented_path {
205 1 ▶ my ($id, %opts) = @_;
Subroutine segmented_path: called CC=3 SCAR=11.2
Pod documented
206 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
207 1 my $base_path = $opts{base_path};
208 1 $id =~ s/[^0-9a-zA-Z]+//g;
209 1 my @path = unpack "(A$segment_size)*", $id;
210 1 ▶ defined $base_path
Branch: 1 branch
BranchTrueFalseTotal
defined $base_path ? : 1 0 1
211 ? File::Spec->catdir($base_path, @path)
212 : File::Spec->catdir(@path);
213 }
214  
215 sub parse_data_path {
216 1 ▶ my ($path) = @_;
Subroutine parse_data_path: called CC=1 SCAR=0.0
Pod undocumented
217 1 check_string($path);
218 1 $path = [split /[\/\.]/, $path];
219 1 my $key = pop @$path;
220 1 return $path, $key;
221 }
222  
223 sub get_data {
224 7 ▶ my ($data, $key) = @_;
Subroutine get_data: called CC=10 SCAR=23.1
Pod undocumented
225 7 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_array_ref($data)) 5 2 7
226 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
227 1 ▶ elsif ($key eq '$last') {return unless @$data; $key = @$data - 1}
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 1 1
228 1 elsif ($key eq '*') {return @$data}
229 4 ▶ if (array_exists($data, $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (array_exists($data, $key)) 3 1 4
230 3 return $data->[$key];
231 }
232 1 return;
233 }
234 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
235 1 return $data->{$key};
236 }
237 1 return;
238 }
239  
240 sub set_data {
241 15 ▶ my ($data, $key, @vals) = @_;
Subroutine set_data: called CC=12 SCAR=24.9
Pod undocumented
242 15 ▶ return unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 15 15
243 15 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_array_ref($data)) 12 3 15
244 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
245 2 ▶ elsif ($key eq '$last') {return unless @$data; $key = @$data - 1}
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 2 2
246 elsif ($key eq '$prepend') {
247 2 unshift @$data, $vals[0];
248 2 return $vals[0];
249 }
250 2 elsif ($key eq '$append') {push @$data, $vals[0]; return $vals[0]}
251 1 elsif ($key eq '*') {return splice @$data, 0, @$data, @vals}
252 7 ▶ return $data->[$key] = $vals[0] if is_natural($key);
Branch: 1 branch
BranchTrueFalseTotal
if is_natural($key) 6 1 7
253 1 return;
254 }
255 3 ▶ if (is_hash_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_hash_ref($data)) 2 1 3
256 2 return $data->{$key} = $vals[0];
257 }
258 1 return;
259 }
260  
261 sub delete_data {
262 7 ▶ my ($data, $key) = @_;
Subroutine delete_data: called CC=10 SCAR=23.1
Pod undocumented
263 7 ▶ if (is_array_ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_array_ref($data)) 5 2 7
264 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
265 1 ▶ elsif ($key eq '$last') {return unless @$data; $key = @$data - 1}
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 1 1
266 1 elsif ($key eq '*') {return splice @$data, 0, @$data}
267 4 ▶ if (array_exists($data, $key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (array_exists($data, $key)) 3 1 4
268 3 return splice @$data, $key, 1;
269 }
270 1 return;
271 }
272 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
273 1 return delete $data->{$key};
274 }
275  
276 1 return;
277 }
278  
279 sub data_at {
280 22 ▶ my ($path, $data, %opts) = @_;
Subroutine data_at: called CC=27 SCAR=43.7
Pod undocumented
281 22 ▶ if (ref $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path) { } 6 16 22
282 6 $path = [map {split /[\/\.]/} @$path];
283 }
284 else {
285 16 $path = [split /[\/\.]/, $path];
286 }
287 22 my $create = $opts{create};
288 22 ▶ my $_key = $opts{_key} // $opts{key};
Condition33%1/3
$opts{"_key"} // $opts{"key"}
l!l&&r!l&&!r
0022
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
289 22 ▶ if (defined $opts{key} && $create && @$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"key"} and $create and @$path) 0 22 22
Condition33%2/6
defined $opts{"key"} and $create and @$path
!ll&&!rl&&r
2200
defined $opts{"key"} and $create
!ll&&!rl&&r
2200
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
290 0 push @$path, $_key;
291 }
292 22 my $key;
293 22 ▶ while (defined(my $key = shift @$path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @$path)) 25 20 45
294 25 ▶ is_ref($data) || return;
Branch: 1 branch
BranchTrueFalseTotal
&is_ref($data) or return 0 25 25
295 25 ▶ if (is_array_ref($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&is_array_ref($data)) { } 7 18 25
elsif ($create and @$path) { } 0 18 18
296 7 ▶ if ($key eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '*') { } 2 5 7
297 return
298 2 map {data_at($path, $_, create => $create, _key => $_key)}
299 @$data;
300 }
301 else {
302 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
303 1 elsif ($key eq '$last') {$key = -1}
304 1 elsif ($key eq '$prepend') {unshift @$data, undef; $key = 0}
305 1 elsif ($key eq '$append') {push @$data, undef; $key = @$data}
306 5 ▶ is_integer($key) || return;
Branch: 1 branch
BranchTrueFalseTotal
is_integer($key) or return 0 5 5
307 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
308 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
309 || ord($path->[0]) == ord('$') ? [] : {};
310 }
311 else {
312 5 $data = $data->[$key];
313 }
314 }
315 }
316 ▶ elsif ($create && @$path) {
Condition33%1/3
$create and @$path
!ll&&!rl&&r
1800
MC/DC0%0/2
$create and @$path
$create @$path
Truth table33%1/3
A and B
A= $createB= @$path
ABresult
0X0
100
111
317 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
318 || ord($path->[0]) == ord('$') ? [] : {};
319 }
320 else {
321 18 $data = $data->{$key};
322 }
323 23 ▶ if ($create && @$path == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($create and @$path == 1) 0 23 23
Condition33%1/3
$create and @$path == 1
!ll&&!rl&&r
2300
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
324 0 last;
325 }
326 }
327 20 $data;
328 }
329  
330 sub array_exists {
331 10 ▶ my ($arr, $i) = @_;
Subroutine array_exists: called CC=2 SCAR=7.2
Pod documented
332 10 ▶ is_natural($i) && $i < @$arr;
Branch: 1 branch
BranchTrueFalseTotal
is_natural($i) and $i < @$arr 10 0 10
333 }
334  
335 sub array_group_by {
336 1 ▶ my ($arr, $key) = @_;
Subroutine array_group_by: called CC=1 SCAR=0.0
Pod documented
337 List::Util::reduce {
338 4 ▶ my $k = $b->{$key};
Subroutine __ANON__: called CC=3 SCAR=11.0
339 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
340 4 $a
341 }
342 1 {}, @$arr;
343 }
344  
345 sub array_pluck {
346 1 ▶ my ($arr, $key) = @_;
Subroutine array_pluck: called CC=1 SCAR=0.0
Pod documented
347 1 my @vals = map {$_->{$key}} @$arr;
348 1 \@vals;
349 }
350  
351 sub array_to_sentence {
352 7 ▶ my ($arr, $join, $join_last) = @_;
Subroutine array_to_sentence: called CC=4 SCAR=14.1
Pod documented
353 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
354 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
355 7 my $size = scalar @$arr;
356 7 ▶ $size > 2
Branch: 1 branch
BranchTrueFalseTotal
$size > 2 ? : 1 6 7
357 ? join($join_last, join($join, @$arr[0 .. $size - 2]), $arr->[-1])
358 : join($join_last, @$arr);
359 }
360  
361 sub array_sum {
362 1 ▶ List::Util::sum(0, @{$_[0]});
Subroutine array_sum: called CC=1 SCAR=0.0
Pod documented
363 }
364  
365 sub array_includes {
366 2 ▶ my ($arr, $val) = @_;
Subroutine array_includes: called CC=3 SCAR=11.0
Pod documented
367 2 ▶ is_same($val, $_) && return 1 for @$arr;
Branch: 1 branch
BranchTrueFalseTotal
is_same($val, $_) and return 1 1 1 2
368 1 0;
369 }
370  
371 sub array_any {
372 2 ▶ my ($arr, $sub) = @_;
Subroutine array_any: called CC=3 SCAR=11.0
Pod documented
373 2 ▶ $sub->($_) && return 1 for @$arr;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($_) and return 1 1 5 6
374 1 0;
375 }
376  
377 sub array_rest {
378 1 ▶ my ($arr) = @_;
Subroutine array_rest: called CC=2 SCAR=7.2
Pod documented
379 1 ▶ @$arr < 2 ? [] : [@$arr[1 .. (@$arr - 1)]];
Branch: 1 branch
BranchTrueFalseTotal
@$arr < 2 ? : 0 1 1
380 }
381  
382 sub array_uniq {
383 1 ▶ my ($arr) = @_;
Subroutine array_uniq: called CC=1 SCAR=0.0
Pod documented
384 1 my %seen = ();
385 1 my @vals = grep {not $seen{$_}++} @$arr;
386 1 \@vals;
387 }
388  
389 sub array_split {
390 20 ▶ my ($arr) = @_;
Subroutine array_split: called CC=2 SCAR=6.9
Pod documented
391 20 ▶ is_array_ref($arr) ? $arr : [split ',', $arr];
Branch: 1 branch
BranchTrueFalseTotal
&is_array_ref($arr) ? : 12 8 20
392 }
393  
394 145 *hash_merge = \&Hash::Merge::Simple::merge;
395  
396 sub as_utf8 {
397 23 ▶ my $str = $_[0];
Subroutine as_utf8: called CC=1 SCAR=0.0
Pod documented
398 23 utf8::upgrade($str);
399 23 $str;
400 }
401  
402 sub trim {
403 457 ▶ my $str = $_[0];
Subroutine trim: called CC=2 SCAR=6.9
Pod documented
404 457 ▶ if ($str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str) 456 1 457
405 456 $str =~ s/^[\h\v]+//s;
406 456 $str =~ s/[\h\v]+$//s;
407 }
408 457 $str;
409 }
410  
411 sub capitalize {
412 1 ▶ my $str = $_[0];
Subroutine capitalize: called CC=1 SCAR=0.0
Pod documented
413 1 utf8::upgrade($str);
414 1 ucfirst lc $str;
415 }
416  
417 145 *is_same = \&Data::Compare::Compare;
418  
419 sub is_different {
420 1 ▶ !is_same(@_);
Subroutine is_different: called CC=1 SCAR=0.0
Pod documented
421 }
422  
423 sub check_same {
424 2 ▶ is_same(@_) || Catmandu::BadVal->throw('should be same');
Subroutine check_same: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_same(@_) or "Catmandu::BadVal"->throw("should be same") 1 1 2
425 1 $_[0];
426 }
427  
428 sub check_different {
429 2 ▶ is_same(@_) && Catmandu::BadVal->throw('should be different');
Subroutine check_different: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_same(@_) and "Catmandu::BadVal"->throw("should be different") 1 1 2
430 1 $_[0];
431 }
432  
433 # the following code is taken from Data::Util::PurePerl 0.63
434 sub _get_stash {
435 7867 ▶ my ($inv) = @_;
Subroutine _get_stash: called CC=5 SCAR=16.3
436  
437 7867 ▶ if (Scalar::Util::blessed($inv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($inv)) { } 0 7867 7867
elsif (not is_string($inv)) { } 6 7861 7867
438 145 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
439 0 return \%{ref($inv) . '::'};
440 }
441 elsif (!is_string($inv)) {
442 6 return undef;
443 }
444  
445 7861 $inv =~ s/^:://;
446  
447 7861 my $pack = *main::;
448 7861 for my $part (split /::/, $inv) {
449 16194 ▶ return undef unless $pack = $pack->{$part . '::'};
Branch: 1 branch
BranchTrueFalseTotal
unless $pack = $$pack{$part . '::'} 123 16071 16194
450 }
451 7738 return *{$pack}{HASH};
452 }
453  
454 sub _get_code_ref {
455 7395 ▶ my ($pkg, $name, @flags) = @_;
Subroutine _get_code_ref: called CC=6 SCAR=18.8
456  
457 7395 ▶ is_string($pkg) || Catmandu::BadVal->throw('should be string');
Branch: 1 branch
BranchTrueFalseTotal
is_string($pkg) or "Catmandu::BadVal"->throw("should be string") 0 7395 7395
458 7395 ▶ is_string($name) || Catmandu::BadVal->throw('should be string');
Branch: 1 branch
BranchTrueFalseTotal
is_string($name) or "Catmandu::BadVal"->throw("should be string") 0 7395 7395
459  
460 7395 ▶ my $stash = _get_stash($pkg) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $stash = _get_stash($pkg) or return undef 0 7395 7395
461  
462 7395 ▶ if (defined(my $glob = $stash->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $glob = $stash->{$name})) 1015 6380 7395
463 1015 ▶ if (is_glob_ref(\$glob)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_glob_ref(\$glob)) { } 1015 0 1015
464 1015 return *{$glob}{CODE};
465 }
466 else { # a stub or special constant
467 145 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
468 0 return *{$pkg . '::' . $name}{CODE};
469 }
470 }
471 6380 return undef;
472 }
473  
474 sub is_invocant {
475 516 ▶ my ($inv) = @_;
Subroutine is_invocant: called CC=2 SCAR=6.9
Pod documented
476 516 ▶ if (ref $inv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inv) { } 44 472 516
477 44 return !!Scalar::Util::blessed($inv);
478 }
479 else {
480 472 return !!_get_stash($inv);
481 }
482 }
483  
484 145 *is_scalar_ref = \&Ref::Util::is_plain_scalarref;
485  
486 145 *is_array_ref = \&Ref::Util::is_plain_arrayref;
487  
488 145 *is_hash_ref = \&Ref::Util::is_plain_hashref;
489  
490 145 *is_code_ref = \&Ref::Util::is_plain_coderef;
491  
492 145 *is_regex_ref = \&Ref::Util::is_regexpref;
493  
494 145 *is_glob_ref = \&Ref::Util::is_plain_globref;
495  
496 sub is_value {
497 23508 ▶ defined($_[0]) && !is_ref($_[0]) && !is_glob_ref(\$_[0]);
Subroutine is_value: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not &is_ref($_[0]) and not &is_glob_ref(\$_[0]) 23355 153 23508
Condition100%3/3
defined $_[0] and not &is_ref($_[0])
!ll&&!rl&&r
814523355
MC/DC100%2/2
defined $_[0] and not &is_ref($_[0])
defined $_[0] not &is_ref($_[0])
Truth table100%3/3
A and not(B)
A= defined $_[0]B= not &is_ref($_[0])
ABresult
0X0
100
111
498 }
499  
500 sub is_string {
501 23010 ▶ is_value($_[0]) && length($_[0]) > 0;
Subroutine is_string: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_value($_[0]) and length $_[0] > 0 22899 111 23010
502 }
503  
504 sub is_number {
505 255 ▶ return 0 if !defined($_[0]) || is_ref($_[0]);
Subroutine is_number: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or &is_ref($_[0]) 25 230 255
Condition100%3/3
not defined $_[0] or &is_ref($_[0])
l!l&&r!l&&!r
214230
MC/DC100%2/2
not defined $_[0] or &is_ref($_[0])
not defined $_[0] &is_ref($_[0])
Truth table100%3/3
not(A) or B
A= not defined $_[0]B= &is_ref($_[0])
ABresult
1X1
011
000
506  
507 230 return $_[0] =~ m{
508 \A \s*
509 [+-]?
510 (?= \d | \.\d)
511 \d*
512 (\.\d*)?
513 (?: [Ee] (?: [+-]? \d+) )?
514 \s* \z
515 }xms;
516 }
517  
518 sub is_integer {
519 37 ▶ return 0 if !defined($_[0]) || is_ref($_[0]);
Subroutine is_integer: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or &is_ref($_[0]) 3 34 37
Condition66%2/3
not defined $_[0] or &is_ref($_[0])
l!l&&r!l&&!r
3034
MC/DC50%1/2
not defined $_[0] or &is_ref($_[0])
not defined $_[0] &is_ref($_[0])
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= &is_ref($_[0])
ABresult
1X1
011
000
520  
521 34 return $_[0] =~ m{
522 \A \s*
523 [+-]?
524 \d+
525 \s* \z
526 }xms;
527 }
528  
529 # end of code taken from Data::Util
530  
531 sub is_bool {
532 22 ▶ Scalar::Util::blessed($_[0])
Subroutine is_bool: called CC=6 SCAR=23.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("boolean") || $_[0]->isa("Types::Serialiser::Boolean") || $_[0]->isa("JSON::XS::Boolean") || $_[0]->isa("Cpanel::JSON::XS::Boolean") || $_[0]->isa("JSON::PP::Boolean") 8 14 22
Condition40%6/15
builtin::blessed($_[0]) && ($_[0]->isa('boolean') || $_[0]->isa('Types::Serialiser::Boolean') || $_[0]->isa('JSON::XS::Boolean') || $_[0]->isa('Cpanel::JSON::XS::Boolean') || $_[0]->isa('JSON::PP::Boolean'))
!ll&&!rl&&r
1408
$_[0]->isa("boolean") or $_[0]->isa("Types::Serialiser::Boolean") or $_[0]->isa("JSON::XS::Boolean") or $_[0]->isa("Cpanel::JSON::XS::Boolean") or $_[0]->isa("JSON::PP::Boolean")
l!l&&r!l&&!r
080
$_[0]->isa("boolean") or $_[0]->isa("Types::Serialiser::Boolean") or $_[0]->isa("JSON::XS::Boolean") or $_[0]->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
008
$_[0]->isa("boolean") or $_[0]->isa("Types::Serialiser::Boolean") or $_[0]->isa("JSON::XS::Boolean")
l!l&&r!l&&!r
008
$_[0]->isa("boolean") or $_[0]->isa("Types::Serialiser::Boolean")
l!l&&r!l&&!r
008
MC/DC16%1/6
builtin::blessed($_[0]) && ($_[0]->isa('boolean') || $_[0]->isa('Types::Serialiser::Boolean') || $_[0]->isa('JSON::XS::Boolean') || $_[0]->isa('Cpanel::JSON::XS::Boolean') || $_[0]->isa('JSON::PP::Boolean'))
builtin::blessed($_[0]) $_[0]->isa("boolean") $_[0]->isa("Types::Serialiser::Boolean") $_[0]->isa("JSON::XS::Boolean") $_[0]->isa("Cpanel::JSON::XS::Boolean") $_[0]->isa("JSON::PP::Boolean")
Truth table28%2/7
A && B or C or D or E or F
A= builtin::blessed($_[0])B= $_[0]->isa("boolean")C= $_[0]->isa("Types::Serialiser::Boolean")D= $_[0]->isa("JSON::XS::Boolean")E= $_[0]->isa("Cpanel::JSON::XS::Boolean")F= $_[0]->isa("JSON::PP::Boolean")
ABCDEFresult
0XXXXX0
1000000
11XXXX1
101XXX1
1001XX1
10001X1
1000011
533 && ($_[0]->isa('boolean')
534 || $_[0]->isa('Types::Serialiser::Boolean')
535 || $_[0]->isa('JSON::XS::Boolean')
536 || $_[0]->isa('Cpanel::JSON::XS::Boolean')
537 || $_[0]->isa('JSON::PP::Boolean'));
538 }
539  
540 sub is_natural {
541 21 ▶ is_integer($_[0]) && $_[0] >= 0;
Subroutine is_natural: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_integer($_[0]) and $_[0] >= 0 19 2 21
542 }
543  
544 sub is_positive {
545 10 ▶ is_integer($_[0]) && $_[0] >= 1;
Subroutine is_positive: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_integer($_[0]) and $_[0] >= 1 9 1 10
546 }
547  
548 145 *is_ref = \&Ref::Util::is_ref;
549  
550 sub is_able {
551 43 ▶ my $obj = shift;
Subroutine is_able: called CC=4 SCAR=13.9
Pod documented
552 43 ▶ is_invocant($obj) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
is_invocant($obj) or return 0 13 30 43
553 30 ▶ $obj->can($_) || return 0 for @_;
Branch: 1 branch
BranchTrueFalseTotal
$obj->can($_) or return 0 5 25 30
554 25 1;
555 }
556  
557 sub check_able {
558 13 ▶ my $obj = shift;
Subroutine check_able: called CC=2 SCAR=6.9
Pod documented
559 13 ▶ return $obj if is_able($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_able($obj, @_) 11 2 13
560 2 Catmandu::BadVal->throw('should be able to ' . array_to_sentence(\@_));
561 }
562  
563 sub check_maybe_able {
564 2 ▶ my $obj = shift;
Subroutine check_maybe_able: called CC=2 SCAR=6.9
Pod documented
565 2 ▶ return $obj if is_maybe_able($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_maybe_able($obj, @_) 1 1 2
566 1 Catmandu::BadVal->throw(
567 'should be undef or able to ' . array_to_sentence(\@_));
568 }
569  
570 sub is_instance {
571 90 ▶ my $obj = shift;
Subroutine is_instance: called CC=4 SCAR=13.9
Pod documented
572 90 ▶ Scalar::Util::blessed($obj) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) or return 0 51 39 90
573 39 ▶ $obj->isa($_) || return 0 for @_;
Branch: 1 branch
BranchTrueFalseTotal
$obj->isa($_) or return 0 16 15 31
574 23 1;
575 }
576  
577 sub check_instance {
578 2 ▶ my $obj = shift;
Subroutine check_instance: called CC=2 SCAR=6.9
Pod documented
579 2 ▶ return $obj if is_instance($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_instance($obj, @_) 1 1 2
580 1 Catmandu::BadVal->throw(
581 'should be instance of ' . array_to_sentence(\@_));
582 }
583  
584 sub check_maybe_instance {
585 2 ▶ my $obj = shift;
Subroutine check_maybe_instance: called CC=2 SCAR=6.9
Pod documented
586 2 ▶ return $obj if is_maybe_instance($obj, @_);
Branch: 1 branch
BranchTrueFalseTotal
if is_maybe_instance($obj, @_) 1 1 2
587 1 Catmandu::BadVal->throw(
588 'should be undef or instance of ' . array_to_sentence(\@_));
589 }
590  
591 145 for my $sym (
592 qw(able instance invocant ref
593 scalar_ref array_ref hash_ref code_ref regex_ref glob_ref
594 bool value string number integer natural positive)
595 )
596 {
597 2465 my $pkg = __PACKAGE__;
598 2465 my $err_name = $sym;
599 2465 $err_name =~ s/_/ /;
600 2465 push @EXPORT_OK, "is_$sym", "is_maybe_$sym", "check_$sym",
601 "check_maybe_$sym";
602 2465 push @{$EXPORT_TAGS{is}}, "is_$sym", "is_maybe_$sym";
603 2465 push @{$EXPORT_TAGS{check}}, "check_$sym", "check_maybe_$sym";
604 2465 ▶ Sub::Quote::quote_sub("${pkg}::is_maybe_$sym",
Branch: 1 branch
BranchTrueFalseTotal
unless _get_code_ref($pkg, "is_maybe_$sym") 2465 0 2465
605 "!defined(\$_[0]) || ${pkg}::is_$sym(\$_[0])")
606 unless _get_code_ref($pkg, "is_maybe_$sym");
607 2465 ▶ Sub::Quote::quote_sub("${pkg}::check_$sym",
Branch: 1 branch
BranchTrueFalseTotal
unless _get_code_ref($pkg, "check_$sym") 2175 290 2465
608 "${pkg}::is_$sym(\$_[0]) || Catmandu::BadVal->throw('should be $err_name'); \$_[0]"
609 ) unless _get_code_ref($pkg, "check_$sym");
610 2465 ▶ Sub::Quote::quote_sub("${pkg}::check_maybe_$sym",
Branch: 1 branch
BranchTrueFalseTotal
unless _get_code_ref($pkg, "check_maybe_$sym") 2175 290 2465
611 "${pkg}::is_maybe_$sym(\$_[0]) || Catmandu::BadVal->throw('should be undef or $err_name'); \$_[0]"
612 ) unless _get_code_ref($pkg, "check_maybe_$sym");
613 }
614  
615 sub human_number { # taken from Number::Format
616 1 ▶ my $num = $_[0];
Subroutine human_number: called CC=2 SCAR=7.0
Pod documented
617  
618 # add leading 0's so length($num) is divisible by 3
619 1 $num = '0' x (3 - (length($num) % 3)) . $num;
620  
621 # split $num into groups of 3 characters and insert commas
622 1 $num = join ',', grep {$_ ne ''} split /(...)/, $num;
623  
624 # strip off leading zeroes and/or comma
625 1 $num =~ s/^0+,?//;
626 1 ▶ length $num ? $num : '0';
Branch: 1 branch
BranchTrueFalseTotal
length $num ? : 1 0 1
627 }
628  
629 sub human_byte_size {
630 4 ▶ my ($size) = @_;
Subroutine human_byte_size: called CC=4 SCAR=13.9
Pod documented
631 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
632 1 return sprintf("%.2f GB", $size / 1000000000);
633 }
634 elsif ($size > 1000000) {
635 1 return sprintf("%.2f MB", $size / 1000000);
636 }
637 elsif ($size > 1000) {
638 1 return sprintf("%.2f KB", $size / 1000);
639 }
640 1 "$size bytes";
641 }
642  
643 sub human_content_type {
644 1 ▶ my ($content_type, $default) = @_;
Subroutine human_content_type: called CC=3 SCAR=13.3
Pod documented
645 1 my ($key) = $content_type =~ /^([^;]+)/;
646 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
647 }
648  
649 sub xml_declaration {
650 1 ▶ $XML_DECLARATION;
Subroutine xml_declaration: called CC=1 SCAR=0.0
Pod documented
651 }
652  
653 sub xml_escape {
654 1 ▶ my ($str) = @_;
Subroutine xml_escape: called CC=1 SCAR=0.0
Pod documented
655 1 utf8::upgrade($str);
656  
657 1 $str =~ s/&/&amp;/go;
658 1 $str =~ s/</&lt;/go;
659 1 $str =~ s/>/&gt;/go;
660 1 $str =~ s/"/&quot;/go;
661 1 $str =~ s/'/&apos;/go;
662  
663 # remove control chars
664 1 $str
665 =~ s/[^\x09\x0A\x0D\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]//go;
666  
667 1 $str;
668 }
669  
670 sub use_lib {
671 2 ▶ my (@dirs) = @_;
Subroutine use_lib: called CC=2 SCAR=7.0
Pod documented
672  
673 145 ▶ use lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
674 2 local $@;
675 2 lib->import(@dirs);
676 2 ▶ Catmandu::Error->throw($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
677  
678 2 1;
679 }
680  
681 sub pod_section {
682 1453 ▶ my $class = is_ref($_[0]) ? ref(shift) : shift;
Subroutine pod_section: called CC=5 SCAR=16.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
&is_ref($_[0]) ? : 0 1453 1453
683 1453 my $section = uc(shift);
684  
685 1453 ▶ unless (-r $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $class) 9 1444 1453
686 9 $class =~ s!::!/!g;
687 9 $class .= '.pm';
688 9 ▶ $class = $INC{$class} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$class = $INC{$class} or return "" 0 9 9
689 }
690  
691 1453 my $text = "";
692 1453 ▶ open my $input, "<", $class or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<", $class or return "" 0 1453 1453
693 1453 open my $output, ">", \$text;
694  
695 1453 require Pod::Usage; # lazy load only if needed
696 1453 Pod::Usage::pod2usage(
697 -input => $input,
698 -output => $output,
699 -sections => $section,
700 -exit => "NOEXIT",
701 -verbose => 99,
702 -indent => 0,
703 -utf8 => 1,
704 @_
705 );
706 1453 $section = ucfirst(lc($section));
707 1453 $text =~ s/$section:\n//m;
708 1453 chomp $text;
709  
710 1453 $text;
711 }
712  
713 sub require_package {
714 418 ▶ my ($pkg, $ns) = @_;
Subroutine require_package: called CC=6 SCAR=18.2
Pod documented
715  
716 418 ▶ if ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns) 383 35 418
717 383 ▶ unless ($pkg =~ s/^\+// || $pkg =~ /^$ns/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg =~ s/^\+// or $pkg =~ /^$ns/) 383 0 383
Condition33%1/3
$pkg =~ s/^\+// or $pkg =~ /^$ns/
l!l&&r!l&&!r
00383
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
718 383 $pkg = "${ns}::$pkg";
719 }
720 }
721  
722 418 ▶ return $pkg if is_invocant($pkg);
Branch: 1 branch
BranchTrueFalseTotal
if is_invocant($pkg) 320 98 418
723  
724 98 ▶ eval "require $pkg;1;"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $pkg;1;" or "Catmandu::NoSuchPackage"->throw("message", "No such package: $pkg", "package_name", $pkg) 5 93 98
725 or Catmandu::NoSuchPackage->throw(
726 message => "No such package: $pkg",
727 package_name => $pkg
728 );
729  
730 93 $pkg;
731 }
732  
733 145 1;
734  
735 __END__
736  
737 =pod
738  
739 =head1 NAME
740  
741 Catmandu::Util - A collection of utility functions
742  
743 =head1 SYNOPSIS
744  
745 use Catmandu::Util qw(:string);
746  
747 $str = trim($str);
748  
749 =head1 FUNCTIONS
750  
751 =head2 IO functions
752  
753 use Catmandu::Util qw(:io);
754  
755 =over 4
756  
757 =item io($io, %opts)
758  
759 Takes a file path, glob, glob reference, scalar reference or L<IO::Handle>
760 object and returns an opened L<IO::Handle> object.
761  
762 my $fh = io '/path/to/file';
763  
764 my $fh = io *STDIN;
765  
766 my $fh = io \*STDOUT, mode => 'w', binmode => ':crlf';
767  
768 my $write_cb = sub { my $str = $_[0]; ... };
769  
770 my $fh = io $write_cb, mode => 'w';
771  
772 my $scalar = "";
773 my $fh = io \$scalar, mode => 'w';
774 $fh->print("some text");
775  
776 Options are:
777  
778 =over 12
779  
780 =item mode
781  
782 Default is C<"r">.
783  
784 =item binmode
785  
786 Default is C<":encoding(UTF-8)">.
787  
788 =item encoding
789  
790 Alias for C<binmode>.
791  
792 =back
793  
794 =item read_file($path);
795  
796 [deprecated]: use tools like use tools like File::Slurp::Tiny instead.
797  
798 Reads the file at C<$path> into a string.
799  
800 my $str = read_file('/path/to/file.txt');
801  
802 Throws a Catmandu::Error on failure.
803  
804 =item read_io($io)
805  
806 Reads an IO::Handle into a string.
807  
808 my $str = read_file($fh);
809  
810 =item write_file($path, $str);
811  
812 [deprecated]: use tools like use tools like File::Slurp::Tiny instead.
813  
814 Writes the string C<$str> to a file at C<$path>.
815  
816 write_file('/path/to/file.txt', "contents");
817  
818 Throws a Catmandu::Error on failure.
819  
820 =item read_yaml($path);
821  
822 Reads the YAML file at C<$path> into a Perl hash.
823  
824 my $cfg = read_yaml($path);
825  
826 Dies on failure reading the file or parsing the YAML.
827  
828 =item read_json($path);
829  
830 Reads the JSON file at C<$path> into a Perl hash.
831  
832 my $cfg = read_json($path);
833  
834 Dies on failure reading the file or parsing the JSON.
835  
836 =item join_path(@path);
837  
838 Joins relative paths into an absolute path.
839  
840 join_path('/path/..', './to', 'file.txt');
841 # => "/to/file.txt"
842  
843 =item normalize_path($path);
844  
845 Normalizes a relative path to an absolute path.
846  
847 normalize_path('/path/../to/./file.txt');
848 # => "/to/file.txt"
849  
850 =item segmented_path($path);
851  
852 my $id = "FB41144C-F0ED-11E1-A9DE-61C894A0A6B4";
853 segmented_path($id, segment_size => 4);
854 # => "FB41/144C/F0ED/11E1/A9DE/61C8/94A0/A6B4"
855 segmented_path($id, segment_size => 2, base_path => "/files");
856 # => "/files/FB/41/14/4C/F0/ED/11/E1/A9/DE/61/C8/94/A0/A6/B4"
857  
858 =back
859  
860 =head2 Hash functions
861  
862 use Catmandu::Util qw(:hash);
863  
864 A collection of functions that operate on hash references.
865  
866 =over 4
867  
868 =item hash_merge($hash1, $hash2, ... , $hashN)
869  
870 Merge <hash1> through <hashN>, with the nth-most (rightmost) hash taking precedence.
871 Returns a new hash reference representing the merge.
872  
873 hash_merge({a => 1}, {b => 2}, {a => 3});
874 # => { a => 3 , b => 2}
875  
876 =back
877  
878 =head2 Array functions
879  
880 use Catmandu::Util qw(:array);
881  
882 A collection of functions that operate on array references.
883  
884 =over 4
885  
886 =item array_exists($array, $index)
887  
888 Returns C<1> if C<$index> is in the bounds of C<$array>
889  
890 array_exists(["a", "b"], 2);
891 # => 0
892 array_exists(["a", "b"], 1);
893 # => 1
894  
895 =item array_group_by($array, $key)
896  
897 my $list = [{color => 'black', id => 1},
898 {color => 'white', id => 2},
899 {id => 3},
900 {color => 'black', id => 4}];
901 array_group_by($list, 'color');
902 # => {black => [{color => 'black', id => 1}, {color => 'black', id => 4}],
903 # white => [{color => 'white', id => 2}]}
904  
905 =item array_pluck($array, $key)
906  
907 my $list = [{id => 1}, {}, {id => 3}];
908 array_pluck($list, 'id');
909 # => [1, undef, 3]
910  
911 =item array_to_sentence($array)
912  
913 =item array_to_sentence($array, $join)
914  
915 =item array_to_sentence($array, $join, $join_last)
916  
917 array_to_sentence([1,2,3]);
918 # => "1, 2 and 3"
919 array_to_sentence([1,2,3], ",");
920 # => "1,2 and 3"
921 array_to_sentence([1,2,3], ",", " & ");
922 # => "1,2 & 3"
923  
924 =item array_sum($array)
925  
926 array_sum([1,2,3]);
927 # => 6
928  
929 =item array_includes($array, $val)
930  
931 Returns 1 if C<$array> includes a value that is deeply equal to C<$val>, 0
932 otherwise. Comparison is done with C<is_same()>.
933  
934 array_includes([{color => 'black'}], {color => 'white'});
935 # => 0
936 array_includes([{color => 'black'}], {color => 'black'});
937 # => 1
938  
939 =item array_any($array, \&sub)
940  
941 array_any(["green", "blue"], sub { my $color = $_[0]; $color eq "blue" });
942 # => 1
943  
944 =item array_rest($array)
945  
946 Returns a copy of C<$array> without the head.
947  
948 array_rest([1,2,3,4]);
949 # => [2,3,4]
950 array_rest([1]);
951 # => []
952  
953 =item array_uniq($array)
954  
955 Returns a copy of C<$array> with all duplicates removed. Comparison is done
956 with C<is_same()>.
957  
958 =item array_split($array | $string)
959  
960 Returns C<$array> or a new array by splitting C<$string> at commas.
961  
962 =back
963  
964 =head2 String functions
965  
966 use Catmandu::Util qw(:string);
967  
968 =over 4
969  
970 =item as_utf8($str)
971  
972 Returns a copy of C<$str> flagged as UTF-8.
973  
974 =item trim($str)
975  
976 Returns a copy of C<$str> with leading and trailing whitespace removed.
977  
978 =item capitalize($str)
979  
980 Equivalent to C<< ucfirst lc as_utf8 $str >>.
981  
982 =back
983  
984 =head2 Is functions
985  
986 use Catmandu::Util qw(:is);
987  
988 is_number(42) ? "it's numeric" : "it's not numeric";
989  
990 is_maybe_hash_ref({});
991 # => 1
992 is_maybe_hash_ref(undef);
993 # => 1
994 is_maybe_hash_ref([]);
995 # => 0
996  
997 A collection of predicate functions that test the type or value of argument
998 C<$val>. Each function (except C<is_same()> and C<is_different>) also has a
999 I<maybe> variant that also tests true if C<$val> is undefined.
1000 Returns C<1> or C<0>.
1001  
1002 =over 4
1003  
1004 =item is_invocant($val)
1005  
1006 =item is_maybe_invocant($val)
1007  
1008 Tests if C<$val> is callable (is an existing package or blessed object).
1009  
1010 =item is_able($val, @method_names)
1011  
1012 =item is_maybe_able($val, @method_names)
1013  
1014 Tests if C<$val> is callable and has all methods in C<@method_names>.
1015  
1016 =item is_instance($val, @class_names)
1017  
1018 =item is_maybe_instance($val, @class_names)
1019  
1020 Tests if C<$val> is a blessed object and an instance of all the classes
1021 in C<@class_names>.
1022  
1023 =item is_ref($val)
1024  
1025 =item is_maybe_ref($val)
1026  
1027 Tests if C<$val> is a reference. Equivalent to C<< ref $val ? 1 : 0 >>.
1028  
1029 =item is_scalar_ref($val)
1030  
1031 =item is_maybe_scalar_ref($val)
1032  
1033 Tests if C<$val> is a scalar reference.
1034  
1035 =item is_array_ref($val)
1036  
1037 =item is_maybe_array_ref($val)
1038  
1039 Tests if C<$val> is an array reference.
1040  
1041 =item is_hash_ref($val)
1042  
1043 =item is_maybe_hash_ref($val)
1044  
1045 Tests if C<$val> is a hash reference.
1046  
1047 =item is_code_ref($val)
1048  
1049 =item is_maybe_code_ref($val)
1050  
1051 Tests if C<$val> is a subroutine reference.
1052  
1053 =item is_regex_ref($val)
1054  
1055 =item is_maybe_regex_ref($val)
1056  
1057 Tests if C<$val> is a regular expression reference generated by the C<qr//>
1058 operator.
1059  
1060 =item is_glob_ref($val)
1061  
1062 =item is_maybe_glob_ref($val)
1063  
1064 Tests if C<$val> is a glob reference.
1065  
1066 =item is_value($val)
1067  
1068 =item is_maybe_value($val)
1069  
1070 Tests if C<$val> is a real value (defined, not a reference and not a
1071 glob.
1072  
1073 =item is_string($val)
1074  
1075 =item is_maybe_string($val)
1076  
1077 Tests if C<$val> is a non-empty string.
1078 Equivalent to C<< is_value($val) && length($val) > 0 >>.
1079  
1080 =item is_number($val)
1081  
1082 =item is_maybe_number($val)
1083  
1084 Tests if C<$val> is a number.
1085  
1086 =item is_integer($val)
1087  
1088 =item is_maybe_integer($val)
1089  
1090 Tests if C<$val> is an integer.
1091  
1092 =item is_natural($val)
1093  
1094 =item is_maybe_natural($val)
1095  
1096 Tests if C<$val> is a non-negative integer.
1097 Equivalent to C<< is_integer($val) && $val >= 0 >>.
1098  
1099 =item is_positive($val)
1100  
1101 =item is_maybe_positive($val)
1102  
1103 Tests if C<$val> is a positive integer.
1104 Equivalent to C<< is_integer($val) && $val >= 1 >>.
1105  
1106 =item is_same($val, $other_val)
1107  
1108 Tests if C<$val> is deeply equal to C<$other_val>.
1109  
1110 =item is_different($val, $other_val)
1111  
1112 The opposite of C<is_same()>.
1113  
1114 =back
1115  
1116 =head2 Check functions
1117  
1118 use Catmandu::Util qw(:check);
1119  
1120 check_hash_ref({color => 'red'});
1121 # => {color => 'red'}
1122 check_hash_ref([]);
1123 # dies
1124  
1125 A group of assert functions similar to the C<:is> group, but instead of
1126 returning true or false they return their argument or die.
1127  
1128 =over 4
1129  
1130 =item check_invocant($val)
1131  
1132 =item check_maybe_invocant($val)
1133  
1134 =item check_able($val, @method_names)
1135  
1136 =item check_maybe_able($val, @method_names)
1137  
1138 =item check_instance($val, @class_names)
1139  
1140 =item check_maybe_instance($val, @class_names)
1141  
1142 =item check_ref($val)
1143  
1144 =item check_maybe_ref($val)
1145  
1146 =item check_scalar_ref($val)
1147  
1148 =item check_maybe_scalar_ref($val)
1149  
1150 =item check_array_ref($val)
1151  
1152 =item check_maybe_array_ref($val)
1153  
1154 =item check_hash_ref($val)
1155  
1156 =item check_maybe_hash_ref($val)
1157  
1158 =item check_code_ref($val)
1159  
1160 =item check_maybe_code_ref($val)
1161  
1162 =item check_regex_ref($val)
1163  
1164 =item check_maybe_regex_ref($val)
1165  
1166 =item check_glob_ref($val)
1167  
1168 =item check_maybe_glob_ref($val)
1169  
1170 =item check_value($val)
1171  
1172 =item check_maybe_value($val)
1173  
1174 =item check_string($val)
1175  
1176 =item check_maybe_string($val)
1177  
1178 =item check_number($val)
1179  
1180 =item check_maybe_number($val)
1181  
1182 =item check_integer($val)
1183  
1184 =item check_maybe_integer($val)
1185  
1186 =item check_natural($val)
1187  
1188 =item check_maybe_natural($val)
1189  
1190 =item check_positive($val)
1191  
1192 =item check_maybe_positive($val)
1193  
1194 =item check_same($val, $other_val)
1195  
1196 =item check_different($val, $other_val)
1197  
1198 =back
1199  
1200 =head2 Human output functions
1201  
1202 use Catmandu::Util qw(:human);
1203  
1204 =over 4
1205  
1206 =item human_number($num)
1207  
1208 Insert a comma a 3-digit intervals to make C<$num> more readable. Only works
1209 with I<integers> for now.
1210  
1211 human_number(64354);
1212 # => "64,354"
1213  
1214 =item human_byte_size($size)
1215  
1216 human_byte_size(64);
1217 # => "64 bytes"
1218 human_byte_size(10005000);
1219 # => "10.01 MB"
1220  
1221 =item human_content_type($content_type)
1222  
1223 =item human_content_type($content_type, $default)
1224  
1225 human_content_type('application/x-dos_ms_excel');
1226 # => "Excel"
1227 human_content_type('application/zip');
1228 # => "ZIP archive"
1229 human_content_type('foo/x-unknown');
1230 # => "foo/x-unknown"
1231 human_content_type('foo/x-unknown', 'Unknown');
1232 # => "Unknown"
1233  
1234 =back
1235  
1236 =head2 XML functions
1237  
1238 use Catmandu::Util qw(:xml);
1239  
1240 =over 4
1241  
1242 =item xml_declaration()
1243  
1244 Returns C<< qq(<?xml version="1.0" encoding="UTF-8"?>\n) >>.
1245  
1246 =item xml_escape($str)
1247  
1248 Returns an XML escaped copy of C<$str>.
1249  
1250 =back
1251  
1252 =head2 Miscellaneous functions
1253  
1254 =over 4
1255  
1256 =item require_package($pkg)
1257  
1258 =item require_package($pkg, $namespace)
1259  
1260 Load package C<$pkg> at runtime with C<require> and return it's full name.
1261  
1262 my $pkg = require_package('File::Spec');
1263 my $dir = $pkg->tmpdir();
1264  
1265 require_package('Util', 'Catmandu');
1266 # => "Catmandu::Util"
1267 require_package('Catmandu::Util', 'Catmandu');
1268 # => "Catmandu::Util"
1269  
1270 Throws a Catmandu::Error on failure.
1271  
1272 =item use_lib(@dirs)
1273  
1274 Add directories to C<@INC> at runtime.
1275  
1276 Throws a Catmandu::Error on failure.
1277  
1278 =item pod_section($package_or_file, $section [, @options] )
1279  
1280 Get documentation of a package for a selected section. Additional options are
1281 passed to L<Pod::Usage>.
1282  
1283 =back
1284  
1285 =cut
« blib/lib/Catmandu/Cmd/info.pm ↑ Summary blib/lib/Catmandu/Cmd/copy.pm »