| 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
Condition20%2/10
-t $handle and not length($ENV{"NO_COLOR"} // "") and ($ENV{"TERM"} // "") ne "dumb"
-t $handle and not length($ENV{"NO_COLOR"} // "")
$ENV{"NO_COLOR"} // ""
$ENV{"TERM"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
not defined $word or utf8::is_utf8($word) or not $word =~ /[\x80-\xFF]/u
not defined $word or utf8::is_utf8($word)
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return $text if grep { /\A(?:utf8|encoding)\b/ } PerlIO::get_layers($handle, output => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $name and $name =~ /\A[a-zA-Z][\w-]*\z/u
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 1323 | ▶ | specError("cannot derive a reader name from '%s'", $name // '') if !defined $first || $first eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
not defined $first or $first eq ""
$name // ""
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 ""
Truth table0%0/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $callback and defined $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | ▶ | return [map { processedWith($callback, $result, $_) } $value->@*] if ref $value eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | return { map { $_ => processedWith($callback, $result, $value->{$_}) } keys $value->%* } if ref $value eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 288 | ▶ | return map { scalarsIn($_) } values $value->%* if ref $value eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
(caller $frame + 1)[0] and (caller $frame)[0] =~ /\AGetopt::Pad(?:::|\z)/u
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||