lib/Badger/Utils.pm

filter: Statementstmt 85.7% 186 / 217 Branchbran 70.9% 61 / 86 Conditioncond 56.1% 32 / 57 MC/DCmcdc 32.3% 11 / 34 Subroutinesub 90.7% 39 / 43 Podpod 88.4% 23 / 26 totaltotal 76.0% 352 / 463 CC 66 SCAR 24.0
CC
66
Cov
78%
wrap
90.0
plurality
9.7
inflect
6.1
CRAP 11.0
« lib/Badger/Test/Manager.pm ↑ Summary lib/Badger/Codec/TT.pm »
1 #========================================================================
2 #
3 # Badger::Utils
4 #
5 # DESCRIPTION
6 # Module implementing various useful utility functions.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 #========================================================================
12  
13 package Badger::Utils;
14  
15 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 70 ▶use base 'Badger::Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 70 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 70 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 70 ▶use Badger::Constants 'HASH ARRAY PKG DELIMITER BLANK TRUE FALSE';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 use Badger::Debug
22 70 import => ':dump',
23 70 ▶ default => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 70 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 use constant {
26 70 UTILS => 'Badger::Utils',
27 CLASS => 0,
28 FILE => 1,
29 LOADED => 2,
30 70 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 70 our $VERSION = 0.02;
33 70 our $ERROR = '';
34 0 ▶our $WARN = sub { warn @_ }; # for testing - see t/core/utils.t
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 70 our $MESSAGES = { };
36 70 our $HELPERS = { # keep this compact in case we don't need to use it
37 'Digest::MD5' => 'md5 md5_hex md5_base64',
38 'Scalar::Util' => 'blessed dualvar isweak readonly refaddr reftype
39 tainted weaken isvstring looks_like_number
40 set_prototype',
41 'List::Util' => 'first max maxstr min minstr reduce shuffle sum',
42 'List::MoreUtils' => 'any all none notall true false firstidx
43 first_index lastidx last_index insert_after
44 insert_after_string apply after after_incl before
45 before_incl indexes firstval first_value lastval
46 last_value each_array each_arrayref pairwise
47 natatime mesh zip uniq minmax',
48 'Hash::Util' => 'lock_keys unlock_keys lock_value unlock_value
49 lock_hash unlock_hash hash_seed',
50 'Badger::Date' => 'DATE Date Today',
51 'Badger::Timestamp' => 'TIMESTAMP TS Timestamp Now',
52 'Badger::Logic' => 'LOGIC Logic',
53 'Badger::Duration' => 'DURATION Duration',
54 'Badger::URL' => 'URL',
55 'Badger::Filter' => 'FILTER Filter',
56 'Badger::Filesystem' => 'FS File Dir Bin Cwd',
57 'Badger::Filesystem::Virtual'
58 => 'VFS',
59 };
60 70 our $DELEGATES; # fill this from $HELPERS on demand
61 70 our $RANDOM_NAME_LENGTH = 32;
62 70 our $TEXT_WRAP_WIDTH = 78;
63  
64  
65 70 __PACKAGE__->export_any(qw(
66 UTILS blessed is_object numlike textlike truelike falselike
67 params self_params plural
68 odd_params xprintf dotid random_name camel_case CamelCase wrap
69 permute_fragments plurality inflect split_to_list extend merge merge_hash
70 list_each hash_each join_uri resolve_uri
71 ));
72  
73 70 __PACKAGE__->export_fail(\&_export_fail);
74  
75 # looks_like_number() is such a mouthful. I prefer numlike() to go with textlike()
76 70 *numlike = \&Scalar::Util::looks_like_number;
77  
78 # it would be too confusing not to have this alias
79 70 *CamelCase = \&camel_case;
80  
81  
82 sub _export_fail {
83 101 ▶ my ($class, $target, $symbol, $more_symbols) = @_;
Subroutine _export_fail: called CC=4 SCAR=14.2
84 101 ▶ $DELEGATES ||= _expand_helpers($HELPERS);
Condition66%2/3
$Badger::Utils::DELEGATES ||= _expand_helpers($Badger::Utils::HELPERS)
l!l&&r!l&&!r
31700
MC/DC0%0/2
$Badger::Utils::DELEGATES ||= _expand_helpers($Badger::Utils::HELPERS)
$Badger::Utils::DELEGATES _expand_helpers($Badger::Utils::HELPERS)
Truth table66%2/3
A ||= B
A= $Badger::Utils::DELEGATESB= _expand_helpers($Badger::Utils::HELPERS)
ABresult
1X1
011
000
85 101 ▶ my $helper = $DELEGATES->{ $symbol } || return 0;
Condition50%1/2
$Badger::Utils::DELEGATES->{$symbol} || (return 0)
l!l
1010
MC/DC0%0/1
$Badger::Utils::DELEGATES->{$symbol} || (return 0)
$Badger::Utils::DELEGATES->{$symbol}
Truth table50%1/2
A
A= $Badger::Utils::DELEGATES->{$symbol}
Aresult
11
00
86 101 ▶ require $helper->[FILE] unless $helper->[LOADED];
Branch: 1 branch
BranchTrueFalseTotal
unless $helper->[2] 101 0 101
87 101 $class->export_symbol($target, $symbol, \&{ $helper->[CLASS].PKG.$symbol });
88 101 return 1;
89 }
90  
91 sub _expand_helpers {
92 # invert { x => 'a b c' } into { a => 'x', b => 'x', c => 'x' }
93 70 ▶ my $helpers = shift;
Subroutine _expand_helpers: called CC=1 SCAR=0.0
94 return {
95 map {
96 70 my $name = $_; # e.g. Scalar::Util
97 910 my $file = module_file($name); # e.g. Scalar/Util.pm
98 5530 map { $_ => [$name, $file, 0] } # third item is loaded flag
99 910 split(DELIMITER, $helpers->{ $name })
100 }
101 keys %$helpers
102 }
103 }
104  
105 sub is_object($$) {
106 737 ▶ blessed $_[1] && $_[1]->isa($_[0]);
Subroutine is_object: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) and $_[1]->isa($_[0]) 82 655 737
107 }
108  
109 sub textlike($) {
110 95 ▶ ! ref $_[0] # check if $[0] is a non-reference
Subroutine textlike: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[0] or builtin::blessed($_[0]) and overload::Method($_[0], "\"\"") 7 88 95
Condition100%6/6
!ref($_[0]) || builtin::blessed($_[0]) && overload::Method($_[0], '""')
l!l&&r!l&&!r
8816
builtin::blessed($_[0]) and overload::Method($_[0], "\"\"")
!ll&&!rl&&r
331
MC/DC100%3/3
!ref($_[0]) || builtin::blessed($_[0]) && overload::Method($_[0], '""')
!ref($_[0]) builtin::blessed($_[0]) overload::Method($_[0], "\"\"")
Truth table100%4/4
not(A) || B and C
A= !ref($_[0])B= builtin::blessed($_[0])C= overload::Method($_[0], "\"\"")
ABCresult
1XX1
0111
00X0
0100
111 || blessed $_[0] # or an object with an overloaded
112 && overload::Method($_[0], '""'); # '""' stringification operator
113 }
114  
115 sub truelike($) {
116 11 ▶ falselike($_[0]) ? FALSE : TRUE;
Subroutine truelike: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
&falselike($_[0]) ? : 6 5 11
117 }
118  
119 sub falselike($) {
120 22 ▶ (! $_[0] || $_[0] =~ /^(0|off|no|none|false)$/i) ? TRUE : FALSE;
Subroutine falselike: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
!$_[0] || $_[0] =~ /^(0|off|no|none|false)$/i ? : 12 10 22
Condition100%3/3
not $_[0] or $_[0] =~ /^(0|off|no|none|false)$/i
l!l&&r!l&&!r
6610
MC/DC100%2/2
not $_[0] or $_[0] =~ /^(0|off|no|none|false)$/i
not $_[0] $_[0] =~ /^(0|off|no|none|false)$/i
Truth table100%3/3
not(A) or B
A= not $_[0]B= $_[0] =~ /^(0|off|no|none|false)$/i
ABresult
1X1
011
000
121 }
122  
123 sub params {
124 # enable $DEBUG to track down calls to params() that pass an odd number
125 # of arguments, typically when the rhs argument returns an empty list,
126 # e.g. $obj->foo( x => this_returns_empty_list() )
127 17 ▶ my @args = @_;
Subroutine params: called CC=3 SCAR=11.0
Pod documented
128 local $SIG{__WARN__} = sub {
129 1 ▶ odd_params(@args);
Subroutine __ANON__: called CC=1 SCAR=0.0
130 17 } if DEBUG;
131  
132 17 ▶ @_ && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 2 15 17
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
962
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
133 }
134  
135 sub self_params {
136 3 ▶ my @args = @_;
Subroutine self_params: called CC=3 SCAR=11.3
Pod documented
137 local $SIG{__WARN__} = sub {
138 0 ▶ odd_params(@args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 3 } if DEBUG;
140  
141 3 ▶ (shift, @_ && ref $_[0] eq HASH ? shift : { @_ });
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 1 2 3
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
021
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
142 }
143  
144 sub odd_params {
145 1 ▶ my $method = (caller(2))[3];
Subroutine odd_params: called CC=3 SCAR=11.0
Pod documented
146 $WARN->(
147 "$method() called with an odd number of arguments: ",
148 1 ▶ join(', ', map { defined $_ ? $_ : '<undef>' } @_),
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 3 0 3
149 "\n"
150 );
151 1 my $i = 3;
152 1 while (1) {
153 4 my @info = caller($i);
154 4 ▶ last unless @info;
Branch: 1 branch
BranchTrueFalseTotal
unless @info 1 3 4
155 3 my ($pkg, $file, $line, $sub) = @info;
156 3 $WARN->(
157 sprintf(
158 "%4s: Called from %s in %s at line %s\n",
159 '#' . ($i++ - 2), $sub, $file, $line
160 )
161 );
162 }
163 }
164  
165  
166 sub module_file {
167 910 ▶ my $file = shift;
Subroutine module_file: called CC=1 SCAR=0.0
Pod documented
168 910 $file =~ s[::][/]g;
169 910 $file .= '.pm';
170 }
171  
172 sub xprintf {
173 1803 ▶ my $format = shift;
Subroutine xprintf: called CC=3 SCAR=11.0
Pod documented
174 1803 my @args = @_;
175 1803 $format =~
176 s{ < (\d+)
177 (?: :( [#\-\+ ]? [\w\.]+ ) )?
178 (?: \| (.*?) )?
179 >
180 }
181 48 ▶ { defined $3
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 3 45 48
Condition100%2/2
$2 || 's'
l!l
837
MC/DC100%1/1
$2 || 's'
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
182 ? _xprintf_ifdef(\@args, $1, $2, $3)
183 : '%' . $1 . '$' . ($2 || 's')
184 }egx;
185 70 ▶ no if $] > 5.021, warnings => "redundant";
Subroutine BEGIN: called CC=1 SCAR=0.0
186 1803 sprintf($format, @_);
187 }
188  
189 sub _xprintf_ifdef {
190 3 ▶ my ($args, $n, $format, $text) = @_;
Subroutine _xprintf_ifdef: called CC=3 SCAR=11.0
191 3 ▶ if (defined $args->[$n-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$args[$n - 1]) { } 2 1 3
192 2 ▶ $format = 's' unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 2 0 2
193 2 $format = '%' . $n . '$' . $format;
194 2 $text =~ s/\?/$format/g;
195 2 return $text;
196 }
197 else {
198 1 return '';
199 }
200 }
201  
202 sub dotid {
203 83 ▶ my $text = shift; # munge $text to canonical lower case and dotted form
Subroutine dotid: called CC=1 SCAR=0.0
Pod documented
204 83 $text =~ s/\W+/./g; # e.g. Foo::Bar ==> Foo.Bar
205 83 return lc $text; # e.g. Foo.Bar ==> foo.bar
206 }
207  
208 sub camel_case {
209 join(
210 BLANK,
211 map {
212 132 ▶ map { ucfirst $_ }
Subroutine camel_case: called CC=1 SCAR=0.0
Pod documented
213 split '_'
214 }
215 @_
216 );
217 }
218  
219 sub random_name {
220 5 ▶ my $length = shift || $RANDOM_NAME_LENGTH;
Subroutine random_name: called CC=3 SCAR=11.0
Pod documented
Condition66%2/3
shift() || $Badger::Utils::RANDOM_NAME_LENGTH
l!l&&r!l&&!r
410
MC/DC0%0/2
shift() || $Badger::Utils::RANDOM_NAME_LENGTH
shift() $Badger::Utils::RANDOM_NAME_LENGTH
Truth table66%2/3
A || B
A= shift()B= $Badger::Utils::RANDOM_NAME_LENGTH
ABresult
1X1
011
000
221 5 my $name = '';
222 5 require Digest::MD5;
223  
224 5 ▶ while (length $name < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name < $length) 7 5 12
225 7 $name .= Digest::MD5::md5_hex(
226 time(), rand(), $$, { }, @_
227 );
228 }
229 5 return substr($name, 0, $length);
230 }
231  
232 sub alternates {
233 19 ▶ my $text = shift;
Subroutine alternates: called CC=2 SCAR=6.9
Pod documented
234 return [
235 19 ▶ $text =~ /\|/
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\|/ ? : 2 17 19
236 ? split(qr<\|>, $text, -1) # alternates: (foo|bar) as ['foo', 'bar']
237 : ('', $text) # optional (foo) as (|foo) as ['', 'foo']
238 ];
239 }
240  
241 sub wrap {
242 0 ▶ my $text = shift;
Subroutine wrap: not called CC=9 SCAR=45.0
Pod documented
243 0 ▶ my $width = shift || $TEXT_WRAP_WIDTH;
Condition0%0/3
shift() || $Badger::Utils::TEXT_WRAP_WIDTH
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $Badger::Utils::TEXT_WRAP_WIDTH
shift() $Badger::Utils::TEXT_WRAP_WIDTH
Truth table0%0/3
A || B
A= shift()B= $Badger::Utils::TEXT_WRAP_WIDTH
ABresult
1X1
011
000
244 0 ▶ my $indent = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
245 0 my @words = split(/\s+/, $text);
246 0 my (@lines, @line, $length);
247 0 my $total = 0;
248  
249 0 ▶ while (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 0 0 0
250 0 ▶ $length = length $words[0] || (shift(@words), next);
Condition0%0/3
length $words[0] || (shift @words, next)
l!l&&r!l&&!r
000
MC/DC0%0/2
length $words[0] || (shift @words, next)
length $words[0] (shift @words, next)
Truth table0%0/3
A || B
A= length $words[0]B= (shift @words, next)
ABresult
1X1
011
000
251 0 ▶ if ($total + $length > 74 || $words[0] eq '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($total + $length > 74 or $words[0] eq '\\n') { } 0 0 0
Condition0%0/3
$total + $length > 74 or $words[0] eq "\\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
$total + $length > 74 or $words[0] eq "\\n"
$total + $length > 74 $words[0] eq "\\n"
Truth table0%0/3
A or B
A= $total + $length > 74B= $words[0] eq "\\n"
ABresult
1X1
011
000
252 0 ▶ shift @words if $words[0] eq '\n';
Branch: 1 branch
BranchTrueFalseTotal
if $words[0] eq "\\n" 0 0 0
253 0 push(@lines, join(" ", @line));
254 0 @line = ();
255 0 $total = 0;
256 }
257 else {
258 0 $total += $length + 1; # account for spaces joining words
259 0 push(@line, shift @words);
260 }
261 }
262 0 ▶ push(@lines, join(" ", @line)) if @line;
Branch: 1 branch
BranchTrueFalseTotal
if @line 0 0 0
263 0 return join(
264 "\n" . (' ' x $indent),
265 @lines
266 );
267 }
268  
269  
270 sub permute_fragments {
271 54 ▶ my $input = shift;
Subroutine permute_fragments: called CC=5 SCAR=16.1
Pod documented
272 54 my (@frags, @outputs);
273  
274 # Lookup all the (a) optional fragments and (a|b|c) alternate fragments
275 # replace them with %s. This gives us an sprintf format that we can later
276 # user to re-fill the fragment slots. Meanwhile create a list of @frags
277 # with each item corresponding to a (...) fragment which is represented
278 # by a list reference containing the alternates. e.g. the input
279 # string 'Fo(o|p) Ba(r|z)' generates @frags as ( ['o','p'], ['r','z'] ),
280 # leaving $input set to 'Fo%s Ba%s'. We treat (foo) as sugar for (|foo),
281 # so that 'Template(X)' is permuted as ('Template', 'TemplateX'), for
282 # example.
283  
284 54 $input =~
285 s/
286 \( ( .*? ) \)
287 /
288 19 push(@frags, alternates($1));
289 19 '%s';
290 /gex;
291  
292 # If any of the fragments have multiple values then $format will still contain
293 # one or more '%s' tokens and @frags will have the same number of list refs
294 # in it, one for each fragment. To iterate across all permutations of the
295 # fragment values, we calculate the product P of the sizes of all the lists in
296 # @frags and loop from 0 to P-1. Then we use a div and a mod to get the right
297 # value for each fragment, for each iteration. We divide $n by the product of
298 # all fragment lists to the right of the current fragment and mod it by the size
299 # of the current fragment list. It's effectively counting with a different base
300 # for each column. e.g. consider 3 fragments with 7, 3, and 5 values respectively
301 # [7] [3] [5] P = 7 * 3 * 5 = 105
302 # [n / 15 % 7] [n / 5 % 3] [n % 5] for 0 < n < P
303  
304 54 ▶ if (@frags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@frags) { } 18 36 54
305 18 my $product = 1; $product *= @$_ for @frags;
306 18 ▶ for (my $n = 0; $n < $product; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $product) 38 18 56
307 38 my $divisor = 1;
308 my @args = reverse map {
309 38 my $item = $_->[ $n / $divisor % @$_ ];
310 42 $divisor *= @$_;
311 42 $item;
312 } reverse @frags; # working backwards from right to left
313 38 push(@outputs, sprintf($input, @args));
314 }
315 }
316 else {
317 36 push(@outputs, $input);
318 }
319 return wantarray
320 ? @outputs
321 54 ▶ : \@outputs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 54 0 54
322 }
323  
324 #-----------------------------------------------------------------------------
325 # pluralisation and inflection
326 #-----------------------------------------------------------------------------
327  
328 sub plural {
329 73 ▶ my $name = shift;
Subroutine plural: called CC=4 SCAR=14.5
Pod documented
330  
331 73 ▶ if ($name =~ /(ss|sh|ch|x)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name =~ /(ss|sh|ch|x)$/) { } 0 73 73
elsif ($name =~ s/([^aeiou])y$//) { } 3 70 73
elsif ($name =~ /([^s\d\W])$/) { } 70 0 70
332 0 $name .= 'es';
333 }
334 elsif ($name =~ s/([^aeiou])y$//) {
335 3 $name .= $1.'ies';
336 }
337 elsif ($name =~ /([^s\d\W])$/) {
338 70 $name .= 's';
339 }
340 73 return $name;
341 }
342  
343 sub plurality {
344 19 ▶ my $n = shift || 0;
Subroutine plurality: called CC=8 SCAR=22.7
Pod documented
Condition50%1/2
shift() || 0
l!l
190
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
345 19 ▶ my @items = map { permute_fragments($_) }
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'ARRAY' ? : 0 19 19
Condition33%1/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
0190
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
346 (@_ == 1 && ref $_[0] eq ARRAY)
347 19 ? @{ $_[0] }
348 : @_;
349  
350 # if the user specifies a single word then we pluralise it for them,
351 # assuming that 0 items are plural, 1 is singular, and > 1 is plural
352 19 ▶ if (@items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items == 1) 19 0 19
353 19 my $plural = plural($items[0]);
354 19 unshift(@items, $plural); # 0 whatevers
355 19 push(@items, $plural); # n whatevers (where n > 1)
356 }
357  
358 19 ▶ die "$n is not a number\n" unless numlike($n);
Branch: 1 branch
BranchTrueFalseTotal
unless &numlike($n) 0 19 19
359 19 ▶ my $i = $n > $#items ? $#items : $n;
Branch: 1 branch
BranchTrueFalseTotal
$n > $#items ? : 16 3 19
360 19 ▶ $i = 0 if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 19 19
361  
362 19 return $items[$i];
363 }
364  
365 sub inflect {
366 19 ▶ my $n = shift || 0;
Subroutine inflect: called CC=5 SCAR=18.1
Pod documented
Condition50%1/2
shift() || 0
l!l
190
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
367 19 my $i = shift;
368 19 ▶ my $f = shift || '%s %s';
Condition50%1/2
shift() || '%s %s'
l!l
019
MC/DC0%0/1
shift() || '%s %s'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
369 19 ▶ my $z = @_ ? shift : 'no';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 19 19
370 19 ▶ return xprintf(
Condition33%1/3
$n || $z
l!l&&r!l&&!r
1900
MC/DC0%0/2
$n || $z
$n $z
Truth table33%1/3
A || B
A= $nB= $z
ABresult
1X1
011
000
371 $f, ($n or $z), plurality($n, $i)
372 );
373 }
374  
375  
376 sub _debug {
377 0 ▶ print STDERR @_;
Subroutine _debug: not called CC=1 SCAR=6.9
378 }
379  
380 #-----------------------------------------------------------------------------
381 # List utilities
382 #-----------------------------------------------------------------------------
383  
384 sub list_each {
385 1 ▶ my ($list, $fn) = @_;
Subroutine list_each: called CC=2 SCAR=6.9
Pod documented
386 1 my $n = 0;
387  
388 1 for (@$list) {
389 3 $fn->($list, $n++, $_);
390 }
391  
392 1 return $list;
393 }
394  
395 sub split_to_list {
396 3 ▶ my $list = shift;
Subroutine split_to_list: called CC=5 SCAR=17.8
Pod documented
397 3 ▶ return [ ] unless defined $list and length $list;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list and length $list 0 3 3
Condition33%1/3
defined $list and length $list
!ll&&!rl&&r
003
MC/DC0%0/2
defined $list and length $list
defined $list length $list
Truth table33%1/3
A and B
A= defined $listB= length $list
ABresult
0X0
100
111
398  
399 3 ▶ if (! ref $list) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $list) { } 2 1 3
elsif (ref $list eq 'ARRAY') { } 1 0 1
400 2 return [ split(DELIMITER, $list) ];
401 }
402 elsif (ref $list eq ARRAY) {
403 1 return $list;
404 }
405 else {
406 0 return [$list];
407 }
408 }
409  
410 #-----------------------------------------------------------------------------
411 # Hash utilities
412 #-----------------------------------------------------------------------------
413  
414 sub hash_each {
415 1 ▶ my ($hash, $fn) = @_;
Subroutine hash_each: called CC=2 SCAR=6.9
Pod documented
416  
417 1 ▶ while (my ($key, $value) = each %$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash) 2 1 3
418 2 $fn->($hash, $key, $value);
419 }
420  
421 1 return $hash;
422 }
423  
424  
425 sub extend {
426 2 ▶ my $hash = shift;
Subroutine extend: called CC=4 SCAR=15.5
Pod documented
427 2 my $more;
428  
429 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 2 6
430 4 ▶ if (! $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $_[0]) { } 0 4 4
elsif (ref $_[0] eq 'HASH') { } 4 0 4
431 # ignore undefined/false values
432 0 shift;
433 0 next;
434 }
435 elsif (ref $_[0] eq HASH) {
436 4 $more = shift;
437 }
438 else {
439 0 $more = params(@_);
440 0 @_ = ();
441 }
442 4 @$hash{ keys %$more } = values %$more;
443 }
444  
445 2 return $hash;
446 }
447  
448 sub merge {
449 2 ▶ my $hash = shift;
Subroutine merge: called CC=4 SCAR=15.2
Pod documented
450 2 my $more;
451  
452 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
453 3 ▶ if (! $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $_[0]) { } 0 3 3
elsif (ref $_[0] eq 'HASH') { } 3 0 3
454 # ignore undefined/false values
455 0 shift;
456 0 next;
457 }
458 elsif (ref $_[0] eq HASH) {
459 3 $more = shift;
460 3 $more = { %$more };
461 }
462 else {
463 0 $more = params(@_);
464 0 @_ = ();
465 }
466 3 merge_hash($hash, $more);
467 }
468  
469 2 return $hash;
470 }
471  
472 sub merge_hash {
473 6 ▶ my ($hash, $more) = @_;
Subroutine merge_hash: called CC=5 SCAR=16.1
Pod undocumented
474 6 my $into;
475  
476 6 ▶ while (my ($key, $value) = each %$more) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$more) 10 6 16
477 10 $into = $hash->{ $key };
478  
479 10 ▶ if ($into && ref $into eq HASH && ref $value eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into and ref $into eq 'HASH' and ref $value eq 'HASH') { } 3 7 10
Condition83%5/6
$into and ref $into eq "HASH" and ref $value eq "HASH"
!ll&&!rl&&r
703
$into and ref $into eq "HASH"
!ll&&!rl&&r
613
MC/DC66%2/3
$into and ref $into eq "HASH" and ref $value eq "HASH"
$into ref $into eq "HASH" ref $value eq "HASH"
Truth table75%3/4
A and B and C
A= $intoB= ref $into eq "HASH"C= ref $value eq "HASH"
ABCresult
0XX0
10X0
1100
1111
480 3 merge_hash($into, $value);
481 }
482 else {
483 7 $hash->{ $key } = $value;
484 };
485 }
486 6 return $hash;
487 }
488  
489  
490 #-----------------------------------------------------------------------------
491 # Simple URI manipulation
492 #-----------------------------------------------------------------------------
493  
494 sub join_uri {
495 10 ▶ my $uri = join('/', @_);
Subroutine join_uri: called CC=1 SCAR=0.0
Pod documented
496 10 $uri =~ s{(?<!:)/+}{/}g;
497 10 return $uri;
498 }
499  
500 sub resolve_uri {
501 3 ▶ my $base = shift;
Subroutine resolve_uri: called CC=2 SCAR=6.9
Pod documented
502 3 my $rel = join_uri(@_);
503 3 ▶ return ($rel =~ m{^/})
Branch: 1 branch
BranchTrueFalseTotal
$rel =~ m[^/] ? : 1 2 3
504 ? $rel
505 : join_uri($base, $rel);
506 }
507  
508 70 1;
509  
510 __END__
« lib/Badger/Test/Manager.pm ↑ Summary lib/Badger/Codec/TT.pm »