| 1 | package CLI::Simple::Utils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 12 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 16 | choose | ||||||||||||||||||||||||||||
| 17 | dmp | ||||||||||||||||||||||||||||
| 18 | dump_json | ||||||||||||||||||||||||||||
| 19 | file_or_stdout | ||||||||||||||||||||||||||||
| 20 | normalize_options | ||||||||||||||||||||||||||||
| 21 | slurp | ||||||||||||||||||||||||||||
| 22 | slurp_json | ||||||||||||||||||||||||||||
| 23 | toPascalCase | ||||||||||||||||||||||||||||
| 24 | toCamelCase | ||||||||||||||||||||||||||||
| 25 | ToCamelCase | ||||||||||||||||||||||||||||
| 26 | to_snake_case | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 12 | our $VERSION = '2.4.0'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub toPascalCase { goto &_toCamelCase; } | ||||||||||||||||||||||||||
|
Subroutine
toPascalCase: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub ToCamelCase { goto &_toCamelCase; } | ||||||||||||||||||||||||||
|
Subroutine
ToCamelCase: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub toCamelCase { return _toCamelCase( $_[0], $_[1], 1 ); } | ||||||||||||||||||||||||||
|
Subroutine
toCamelCase: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | ######################################################################## | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _toCamelCase { | ||||||||||||||||||||||||||
|
Subroutine
_toCamelCase: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 36 | ######################################################################## | ||||||||||||||||||||||||||||
| 37 | 0 | my ( $snake_case, $want_hash, $lc_first ) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $snake_case = ref $snake_case ? $snake_case : [$snake_case]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $want_hash //= wantarray ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$want_hash //= wantarray ? 0 : 1
MC/DC0%0/1
$want_hash //= wantarray ? 0 : 1
$want_hash
Truth table0%0/2
A
A= $want_hash
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my @CamelCase = map { | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | ( $want_hash ? $_ : (), join q{}, map {ucfirst} split /[_-]/xsm ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | } @{$snake_case}; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return $want_hash ? {@CamelCase} : @CamelCase | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 48 | if !$lc_first; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return map {lcfirst} @CamelCase | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | if !$want_hash; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my %camelCase = @CamelCase; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | %camelCase = map { $_ => lcfirst $camelCase{$_} } keys %camelCase; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | return \%camelCase; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | ######################################################################## | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub to_snake_case { | ||||||||||||||||||||||||||
|
Subroutine
to_snake_case: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | ######################################################################## | ||||||||||||||||||||||||||||
| 63 | 0 | my ($str) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return q{} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | if !defined $str; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # 1. Handle acronym boundaries (e.g., HTMLParser -> HTML_Parser) | ||||||||||||||||||||||||||||
| 69 | # We look for a sequence of UpperCase followed by (UpperCase + LowerCase) | ||||||||||||||||||||||||||||
| 70 | 0 | $str =~ s/([[:upper:]]+)([[:upper:]][[:lower:]])/$1_$2/xsmg; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # 2. Handle normal Camel/Pascal boundaries (e.g., UserID -> User_ID) | ||||||||||||||||||||||||||||
| 73 | # We look for (LowerCase/Digit) followed by (UpperCase) | ||||||||||||||||||||||||||||
| 74 | 0 | $str =~ s/([[:lower:]\d])([[:upper:]])/$1_$2/xsmg; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # 3. Lowercase everything | ||||||||||||||||||||||||||||
| 77 | 0 | return lc $str; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ######################################################################## | ||||||||||||||||||||||||||||
| 81 | 17 | ▶ | sub choose(&) { ## no critic | ||||||||||||||||||||||||||
|
Subroutine
choose: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | ######################################################################## | ||||||||||||||||||||||||||||
| 83 | 17 | my $code = shift; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Pass the correct context down to the block | ||||||||||||||||||||||||||||
| 86 | 17 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 87 | 15 | return $code->(); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | elsif ( defined wantarray ) { | ||||||||||||||||||||||||||||
| 90 | 2 | return scalar $code->(); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 0 | $code->(); | |||||||||||||||||||||||||||
| 94 | 0 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | ######################################################################## | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub file_or_stdout { | ||||||||||||||||||||||||||
|
Subroutine
file_or_stdout: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | ######################################################################## | ||||||||||||||||||||||||||||
| 101 | 0 | my ( $self, $outfile, $mode ) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $mode //= 'r'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$mode //= "r"
MC/DC0%0/1
$mode //= "r"
$mode
Truth table0%0/2
A
A= $mode
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $outfile //= $self->can('get_outfile') ? $self->get_outfile : qw{-}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$outfile //= $self->can("get_outfile") ? $self->get_outfile : "-"
MC/DC0%0/2
$outfile //= $self->can("get_outfile") ? $self->get_outfile : "-"
$outfile
$self->can("get_outfile") ? $self->get_outfile : "-"
Truth table0%0/3
A //= B
A= $outfileB= $self->can("get_outfile") ? $self->get_outfile : "-"
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $fh = choose { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( !$outfile || $outfile eq q{-} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $outfile or $outfile eq "-"
MC/DC0%0/2
not $outfile or $outfile eq "-"
not $outfile
$outfile eq "-"
Truth table0%0/3
not(A) or B
A= not $outfileB= $outfile eq "-"
| |||||||||||||||||||||||||||||
| 109 | 0 | return \*STDOUT; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | require IO::File; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | return IO::File->new( $outfile, $mode ); | |||||||||||||||||||||||||||
| 115 | 0 | }; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return $fh; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | ######################################################################## | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub dmp { | ||||||||||||||||||||||||||
|
Subroutine
dmp: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | ######################################################################## | ||||||||||||||||||||||||||||
| 123 | 0 | my (@args) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | return Dumper( \@args ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | ######################################################################## | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub slurp_json { | ||||||||||||||||||||||||||
|
Subroutine
slurp_json: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | ######################################################################## | ||||||||||||||||||||||||||||
| 131 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $json = eval { return decode_json( slurp($file) ) }; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | croak "ERROR: could not decode JSON string:\n$EVAL_ERROR\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $json or $CLI::Simple::Utils::EVAL_ERROR
MC/DC0%0/2
not $json or $CLI::Simple::Utils::EVAL_ERROR
not $json
$CLI::Simple::Utils::EVAL_ERROR
Truth table0%0/3
not(A) or B
A= not $jsonB= $CLI::Simple::Utils::EVAL_ERROR
| |||||||||||||||||||||||||||||
| 136 | if !$json || $EVAL_ERROR; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return $json; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | ######################################################################## | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub slurp { | ||||||||||||||||||||||||||
|
Subroutine
slurp: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | ######################################################################## | ||||||||||||||||||||||||||||
| 144 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | local $RS = undef; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | return <$file> | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if openhandle $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | open my $fh, '<:raw', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | or croak "ERROR: could not open $file\n"; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | my $content = <$fh>; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | close $fh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | or carp "ERROR: could not close $file\n"; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | return $content; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ######################################################################## | ||||||||||||||||||||||||||||
| 163 | 29 | ▶ | sub normalize_options { | ||||||||||||||||||||||||||
|
Subroutine
normalize_options: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | ######################################################################## | ||||||||||||||||||||||||||||
| 165 | 29 | my ($options) = @_; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 29 | foreach my $k ( keys %{$options} ) { | |||||||||||||||||||||||||||
| 168 | 18 | ▶ | next if $k !~ /\-/xsm; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | my $val = delete $options->{$k}; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | $k =~ s/\-/_/gxsm; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | $options->{$k} = $val; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 29 | return %{$options}; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | ######################################################################## | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | sub dump_json { | ||||||||||||||||||||||||||
|
Subroutine
dump_json: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | ######################################################################## | ||||||||||||||||||||||||||||
| 182 | 0 | my ($obj) = @_; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | return JSON->new->pretty->encode($obj); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | ######################################################################## | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub args { | ||||||||||||||||||||||||||
|
Subroutine
args: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | ######################################################################## | ||||||||||||||||||||||||||||
| 190 | 0 | my ( $args, @valid_keys ) = @_; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | croak 'args(): first argument must be a hashref' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | if ref($args) ne 'HASH'; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | my %ok = map { $_ => 1 } @valid_keys; | |||||||||||||||||||||||||||
| 196 | 0 | my @bad = grep { !$ok{$_} } keys %{$args}; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | croak sprintf 'bad argument(s): %s', join ', ', @bad | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | if @bad; | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # return in the caller-specified order | ||||||||||||||||||||||||||||
| 202 | 0 | return @{$args}{@valid_keys}; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 12 | 1; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||||||||||