Pad/Util.pm

filter: Statementstmt 87.9% 51 / 58 Branchbran 70.0% 21 / 30 Conditioncond 43.7% 14 / 32 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 94.1% 16 / 17 Podpod 100% 10 / 10 totaltotal 71.7% 117 / 163 CC 28 SCAR 19.1
CC
28
Cov
72%
useColor
17.9
encodedFor
12.0
decodedWord
8.7
CRAP 6.7
« Pad/Completion.pm ↑ Summary Pad/Registry.pm »
1 package Getopt::Pad::Util;
2  
3 12 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 our $VERSION = '0.06';
13 12 our @EXPORT_OK = qw(camelize specError expandTilde useColor isValidName optionSpelling processedWith scalarsIn decodedWord encodedFor);
14  
15 4 ▶sub useColor($handle) {
Subroutine useColor: called CC=6 SCAR=28.9
Pod documented
16 4 ▶ return (-t $handle) && !length($ENV{NO_COLOR} // '') && (($ENV{TERM} // '') ne 'dumb') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-t $handle && !length($ENV{'NO_COLOR'} // '') && ($ENV{'TERM'} // '') ne 'dumb' ? : 0 4 4
Condition20%2/10
-t $handle and not length($ENV{"NO_COLOR"} // "") and ($ENV{"TERM"} // "") ne "dumb"
!ll&&!rl&&r
400
-t $handle and not length($ENV{"NO_COLOR"} // "")
!ll&&!rl&&r
400
$ENV{"NO_COLOR"} // ""
l!l
00
$ENV{"TERM"} // ""
l!l
00
MC/DC0%0/3
-t $handle and not length($ENV{"NO_COLOR"} // "") and ($ENV{"TERM"} // "") ne "dumb"
-t $handle $ENV{"NO_COLOR"} $ENV{"TERM"}
Truth table25%1/4
A and not(B) and C
A= -t $handleB= $ENV{"NO_COLOR"}C= $ENV{"TERM"}
ABCresult
0XX0
11X0
1000
1011
17 }
18  
19 # A command line word as characters, like the values of config files. A
20 # word that is decoded already (perl -CA, or a caller's own decode) is
21 # taken as it is; one that is not valid UTF-8, such as a Latin-1 file
22 # name, stays bytes.
23 56 ▶sub decodedWord($word) {
Subroutine decodedWord: called CC=4 SCAR=21.7
Pod documented
24 56 ▶ return $word if !defined $word || utf8::is_utf8($word) || $word !~ /[\x80-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $word or utf8::is_utf8($word) or not $word =~ /[\x80-\xFF]/u 56 0 56
Condition33%2/6
not defined $word or utf8::is_utf8($word) or not $word =~ /[\x80-\xFF]/u
l!l&&r!l&&!r
0560
not defined $word or utf8::is_utf8($word)
l!l&&r!l&&!r
0056
MC/DC0%0/3
not defined $word or utf8::is_utf8($word) or not $word =~ /[\x80-\xFF]/u
not defined $word utf8::is_utf8($word) not $word =~ /[\x80-\xFF]/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $wordB= utf8::is_utf8($word)C= not $word =~ /[\x80-\xFF]/u
ABCresult
1XX1
01X1
0011
0000
25  
26 0 try {
27 0 return Encode::decode('UTF-8', $word, Encode::FB_CROAK | Encode::LEAVE_SRC);
28 }
29 catch ($error) {
30 0 return $word;
31 }
32 }
33  
34 # $text as it is printed on $handle. Text holding characters is encoded
35 # as UTF-8, unless the handle encodes by itself (perl -CS, an :encoding
36 # layer). Byte strings, such as the help texts of a program without
37 # 'use utf8', are printed as the program wrote them.
38 0 ▶sub encodedFor($handle, $text) {
Subroutine encodedFor: not called CC=3 SCAR=24.8
Pod documented
39 0 ▶ return $text if !utf8::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($text) 0 0 0
40 0 ▶ return $text if grep { /\A(?:utf8|encoding)\b/ } PerlIO::get_layers($handle, output => 1);
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\A(?:utf8|encoding)\b/u;} &PerlIO::get_layers($handle, "output", 1) 0 0 0
41 0 return Encode::encode('UTF-8', $text);
42 }
43  
44 114 ▶sub expandTilde($path) {
Subroutine expandTilde: called CC=2 SCAR=7.2
Pod documented
45 114 ▶ return $path if !defined $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"HOME"} 0 114 114
46 114 return $path =~ s{^~(?=/|$)}{$ENV{HOME}}r;
47 }
48  
49 # The one shape an option name, an alias, an arg short name and a command
50 # name share: a letter, then word characters or dashes.
51 1463 ▶sub isValidName($name) {
Subroutine isValidName: called CC=3 SCAR=11.0
Pod documented
52 1463 ▶ return defined $name && $name =~ /\A[a-zA-Z][\w-]*\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && $name =~ /\A[a-zA-Z][\w-]*\z/u ? : 1461 2 1463
Condition100%3/3
defined $name and $name =~ /\A[a-zA-Z][\w-]*\z/u
!ll&&!rl&&r
111461
MC/DC100%2/2
defined $name and $name =~ /\A[a-zA-Z][\w-]*\z/u
defined $name $name =~ /\A[a-zA-Z][\w-]*\z/u
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /\A[a-zA-Z][\w-]*\z/u
ABresult
0X0
100
111
53 }
54  
55 # An option name as it is typed: a name of one letter with one dash, a
56 # longer one with two.
57 271 ▶sub optionSpelling($name) {
Subroutine optionSpelling: called CC=2 SCAR=6.9
Pod documented
58 271 ▶ return length $name == 1 ? '-' . $name : '--' . $name;
Branch: 1 branch
BranchTrueFalseTotal
length $name == 1 ? : 27 244 271
59 }
60  
61 1323 ▶sub camelize($name) {
Subroutine camelize: called CC=6 SCAR=21.4
Pod documented
62 1323 ▶ my ($first, @rest) = split /[-_]+/, $name // '';
Condition50%1/2
$name // ""
l!l
13230
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
63 1323 ▶ specError("cannot derive a reader name from '%s'", $name // '') if !defined $first || $first eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $first or $first eq "" 0 1323 1323
Condition20%1/5
not defined $first or $first eq ""
l!l&&r!l&&!r
001323
$name // ""
l!l
00
MC/DC0%0/2
not defined $first or $first eq ""
not defined $first $first eq ""
MC/DC0%0/1
$name // ""
$name
Truth table33%1/3
not(A) or B
A= not defined $firstB= $first eq ""
ABresult
1X1
011
000
Truth table0%0/2
A
A= $name
Aresult
11
00
64  
65 1323 my $reader = $first . join('', map { ucfirst } @rest);
66 1323 ▶ specError("derived reader name '%s' (from '%s') is not a valid identifier", $reader, $name) if $reader !~ /^[a-zA-Z_]\w*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $reader =~ /^[a-zA-Z_]\w*$/u 0 1323 1323
67  
68 1323 return $reader;
69 }
70  
71 # $value with every scalar in it replaced by what the processValue
72 # $callback of an option or arg returns for it, the shape kept. $result is
73 # the Result the callback sees. An unset scalar (undef) is left alone.
74 11 ▶sub processedWith($callback, $result, $value) {
Subroutine processedWith: called CC=5 SCAR=16.1
Pod documented
75 11 ▶ return $value if !defined $callback || !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $callback and defined $value 1 10 11
Condition66%2/3
defined $callback and defined $value
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $callback and defined $value
defined $callback defined $value
Truth table66%2/3
A and B
A= defined $callbackB= defined $value
ABresult
0X0
100
111
76 10 ▶ return [map { processedWith($callback, $result, $_) } $value->@*] if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 2 8 10
77 8 ▶ return { map { $_ => processedWith($callback, $result, $value->{$_}) } keys $value->%* } if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 1 7 8
78 7 return $callback->($result, $value);
79 }
80  
81 # Every single value in $value: the value itself, or the scalars of a
82 # list, a mapping or a list of mappings.
83 312 ▶sub scalarsIn($value) {
Subroutine scalarsIn: called CC=3 SCAR=11.0
Pod documented
84 312 ▶ return map { scalarsIn($_) } $value->@* if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 24 288 312
85 288 ▶ return map { scalarsIn($_) } values $value->%* if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 14 274 288
86 274 return ($value);
87 }
88  
89 # Spec mistakes are programmer errors: report them at the first caller
90 # outside Getopt::Pad, normally the GetOptions call, not at the library
91 # frame that noticed them.
92 58 ▶sub specError($format, @args) {
Subroutine specError: called CC=3 SCAR=11.0
Pod documented
93 58 my $frame = 0;
94 58 ▶ $frame++ while (caller($frame + 1))[0] && (caller($frame))[0] =~ /\AGetopt::Pad(?:::|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
(caller $frame + 1)[0] and (caller $frame)[0] =~ /\AGetopt::Pad(?:::|\z)/u and do { ++$frame } 217 58 275
Condition100%3/3
(caller $frame + 1)[0] and (caller $frame)[0] =~ /\AGetopt::Pad(?:::|\z)/u
!ll&&!rl&&r
157217
MC/DC100%2/2
(caller $frame + 1)[0] and (caller $frame)[0] =~ /\AGetopt::Pad(?:::|\z)/u
(caller $frame + 1)[0] (caller $frame)[0] =~ /\AGetopt::Pad(?:::|\z)/u
Truth table100%3/3
A and B
A= (caller $frame + 1)[0]B= (caller $frame)[0] =~ /\AGetopt::Pad(?:::|\z)/u
ABresult
0X0
100
111
95 58 my (undef, $file, $line) = caller($frame);
96  
97 # An uncaught die exits with $! or $? when either is set; clearing both
98 # makes every spec error end the program with status 255.
99 58 ($!, $?) = (0, 0);
100 58 die sprintf("Getopt::Pad spec: %s at %s line %d.\n", sprintf($format, @args), $file, $line);
101 }
102  
103 12 1;
104  
105 __END__
« Pad/Completion.pm ↑ Summary Pad/Registry.pm »