| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||
| 2 | package MOP4Import::Base::CLI_JSON; | ||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 12 | print STDERR "Using (file '" . __FILE__ . "')\n" | |||||||||||||||||||||||||||||||
| 10 | , (DEBUG >= 3 ? Carp::longmess("callstack:") : ()) | ||||||||||||||||||||||||||||||||
| 11 | if DEBUG and DEBUG >= 2 | ||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use MOP4Import::Base::CLI -as_base | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | , [constant => parse_opts__preserve_hyphen => 1] | ||||||||||||||||||||||||||||||||
| 16 | , [fields => | ||||||||||||||||||||||||||||||||
| 17 | , ['help' => doc => "show this help message", json_type => 'string'] | ||||||||||||||||||||||||||||||||
| 18 | , ['quiet' => doc => 'to be (somewhat) quiet', json_type => 'int'] | ||||||||||||||||||||||||||||||||
| 19 | , ['scalar' => doc => "evaluate methods in scalar context", json_type => 'bool'] | ||||||||||||||||||||||||||||||||
| 20 | , ['output' => default => 'ndjson' | ||||||||||||||||||||||||||||||||
| 21 | , doc => "choose output serializer (ndjson/json/tsv/dump)" | ||||||||||||||||||||||||||||||||
| 22 | , json_type => 'string' | ||||||||||||||||||||||||||||||||
| 23 | ] | ||||||||||||||||||||||||||||||||
| 24 | , ['flatten' | ||||||||||||||||||||||||||||||||
| 25 | , json_type => 'bool' | ||||||||||||||||||||||||||||||||
| 26 | ] | ||||||||||||||||||||||||||||||||
| 27 | , ['undef-as' => default => 'null' | ||||||||||||||||||||||||||||||||
| 28 | , doc => "serialize undef as this value. used in tsv output" | ||||||||||||||||||||||||||||||||
| 29 | , json_type => 'string' | ||||||||||||||||||||||||||||||||
| 30 | ] | ||||||||||||||||||||||||||||||||
| 31 | , ['no-exit-code' | ||||||||||||||||||||||||||||||||
| 32 | , doc => "exit with 0(EXIT_SUCCESS) even when result was falsy/empty" | ||||||||||||||||||||||||||||||||
| 33 | , json_type => 'bool' | ||||||||||||||||||||||||||||||||
| 34 | ] | ||||||||||||||||||||||||||||||||
| 35 | , ['binary' => default => 0, doc => "keep STDIN/OUT/ERR binary friendly" | ||||||||||||||||||||||||||||||||
| 36 | , json_type => 'bool' | ||||||||||||||||||||||||||||||||
| 37 | ] | ||||||||||||||||||||||||||||||||
| 38 | 12 | ]; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 12 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | 12 | ▶ | use MOP4Import::Base::JSON -as_base; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 12 | ▶ | use MOP4Import::Opts; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 12 | ▶ | use MOP4Import::Util qw/lexpand globref take_locked_opts_of lock_keys_as/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 12 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 12 | ▶ | use open (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 23 | ▶ | sub cli_precmd { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_precmd: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 51 | 23 | (my MY $self) = @_; | |||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||
| 53 | # cli_precmd() may be called from $class->cmd_help. | ||||||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||||||
| 55 | 23 | ▶ | unless (ref $self and $self->{binary}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $self and $self->{"binary"}
MC/DC0%0/2
ref $self and $self->{"binary"}
ref $self
$self->{"binary"}
Truth table33%1/3
A and B
A= ref $selfB= $self->{"binary"}
| |||||||||||||||||||||||||||||||||
| 56 | 23 | 'open'->import(qw/:locale :std/); | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||||||
| 61 | # Replace parse_opts to use parse_json_opts | ||||||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||||||
| 63 | 23 | ▶ | sub cli_parse_opts { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_parse_opts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 64 | 23 | my ($pack, $list, $result, $opt_alias, $converter, %opts) = @_; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 23 | MOP4Import::Util::parse_json_opts($pack, $list, $result, $opt_alias); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub cli_eval { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_eval: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 70 | 1 | (my MY $self, my ($script, @args)) = @_; | |||||||||||||||||||||||||||||||
| 71 | 1 | my $pack = ref $self; | |||||||||||||||||||||||||||||||
| 72 | 1 | my $sub = do { | |||||||||||||||||||||||||||||||
| 73 | 1 | local $@; | |||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | my $code = eval qq{package $pack; use strict; sub {my \$self = shift; $script\n}}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 1 | $code; | |||||||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||||||
| 78 | 1 | $sub->($self, @args); | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 19 | ▶ | sub cli_invoke { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_invoke: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 82 | 19 | (my MY $self, my ($method, @args)) = @_; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 19 | $self->cli_precmd($method); | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 19 | ▶ | my $sub = $self->can($method) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | or Carp::croak "No such method: $method"; | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 19 | my $list = $self->cli_invoke_sub($sub, $self, @args); | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 19 | ▶ | $self->cli_exit_for_result($list) unless $self->{'no-exit-code'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 19 | ▶ | sub cli_invoke_sub { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_invoke_sub: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 95 | 19 | (my MY $self, my ($sub, $receiver, @args)) = @_; | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 19 | my @res; | |||||||||||||||||||||||||||||||
| 98 | 19 | ▶ | if ($self->{scalar}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 5 | $res[0] = $sub->($receiver, @args); | |||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||
| 101 | 14 | @res = $sub->($receiver, @args); | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 19 | ▶ | $self->cli_output(\@res) unless $self->{quiet}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 19 | \@res; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # | ||||||||||||||||||||||||||||||||
| 110 | # cli_output($list) -- Output abstraction (yield). | ||||||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||||||
| 112 | 19 | ▶ | sub cli_output :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_output: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 113 | 19 | (my MY $self, my ($list)) = @_; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 19 | ▶ | unless ($self->{scalar} ? $list->[0] : @$list) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 116 | 2 | return; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 17 | ▶ | my $emitter = ref $self->{output} eq 'CODE' ? $self->{output} : sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 120 | 17 | $self->cli_write_fh(\*STDOUT, @_); | |||||||||||||||||||||||||||||||
| 121 | 17 | ▶ | }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 17 | ▶ | if ($self->{scalar}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 4 | $emitter->($_) for @$list; | |||||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||||||
| 126 | 13 | $emitter->($list); | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||||||
| 131 | # Gather output from cli_output | ||||||||||||||||||||||||||||||||
| 132 | # | ||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub cli_capture_output { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_capture_output: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 134 | 0 | (my MY $self, my ($subOrArrayOrString, @args)) = @_; | |||||||||||||||||||||||||||||||
| 135 | 0 | my @result; | |||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | local $self->{output} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 137 | 0 | push @result, \@_; | |||||||||||||||||||||||||||||||
| 138 | 0 | }; | |||||||||||||||||||||||||||||||
| 139 | 0 | $self->cli_apply($subOrArrayOrString, @args); | |||||||||||||||||||||||||||||||
| 140 | 0 | @result; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 5 | ▶ | sub cli_examine_result { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_examine_result: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 144 | 5 | (my MY $self, my $list) = @_; | |||||||||||||||||||||||||||||||
| 145 | 5 | ▶ | if ($self->{scalar}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 2 | $list->[0]; | |||||||||||||||||||||||||||||||
| 147 | } else { | ||||||||||||||||||||||||||||||||
| 148 | 3 | @$list; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||||||
| 153 | # exit code handling | ||||||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||||||
| 155 | 5 | ▶ | sub cli_exit_for_result { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_exit_for_result: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 156 | 5 | (my MY $self, my $list) = @_; | |||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | exit($self->cli_examine_result($list) ? 0 : 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | #======================================== | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 4 | ▶ | sub cli_array :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_array: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 164 | 4 | (my MY $self, my @args) = @_; | |||||||||||||||||||||||||||||||
| 165 | 4 | \@args; | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 4 | ▶ | sub cli_list :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 169 | 4 | (my MY $self, my @args) = @_; | |||||||||||||||||||||||||||||||
| 170 | 4 | @args; | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 1 | ▶ | sub cli_object :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 174 | 1 | (my MY $self, my %args) = @_; | |||||||||||||||||||||||||||||||
| 175 | 1 | \%args; | |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub cli_identity :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_identity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 179 | 2 | (my MY $self, my ($thing)) = @_; | |||||||||||||||||||||||||||||||
| 180 | 2 | $thing; | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub cli_map_apply :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_map_apply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 184 | 0 | (my MY $self, my ($subOrArray, @args)) = @_; | |||||||||||||||||||||||||||||||
| 185 | map { | ||||||||||||||||||||||||||||||||
| 186 | 0 | $self->cli_apply($subOrArray, $_); | |||||||||||||||||||||||||||||||
| 187 | } @args; | ||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub cli_grep_apply :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_grep_apply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 191 | 0 | (my MY $self, my ($subOrArray, @args)) = @_; | |||||||||||||||||||||||||||||||
| 192 | grep { | ||||||||||||||||||||||||||||||||
| 193 | 0 | $self->cli_apply($subOrArray, $_); | |||||||||||||||||||||||||||||||
| 194 | } @args; | ||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | # XXX: How about cli_reduce_apply? | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | sub cli_apply :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_apply: called CC=6 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 200 | 1 | (my MY $self, my ($subOrArrayOrString, @args)) = @_; | |||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | if (not defined $subOrArrayOrString) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 202 | 0 | Carp::croak "undefined sub for cli_apply"; | |||||||||||||||||||||||||||||||
| 203 | } elsif (ref $subOrArrayOrString eq 'CODE') { | ||||||||||||||||||||||||||||||||
| 204 | 0 | $subOrArrayOrString->(@args); | |||||||||||||||||||||||||||||||
| 205 | ▶ | } elsif (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
MC/DC0%0/2
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
not ref $subOrArrayOrString
ref $subOrArrayOrString eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $subOrArrayOrStringB= ref $subOrArrayOrString eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 206 | 1 | my ($meth, @opts) = lexpand($subOrArrayOrString); | |||||||||||||||||||||||||||||||
| 207 | 1 | ▶ | if (my $sub = $self->can("cmd_$meth")) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 208 | 0 | $sub->($self, @opts, @args); | |||||||||||||||||||||||||||||||
| 209 | } else { | ||||||||||||||||||||||||||||||||
| 210 | 1 | $self->$meth(@opts, @args); | |||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | } else { | ||||||||||||||||||||||||||||||||
| 213 | 0 | Carp::croak "Invalid argument for cli_apply: " | |||||||||||||||||||||||||||||||
| 214 | . MOP4Import::Util::terse_dump($subOrArrayOrString); | ||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | 1 | ▶ | sub cli_precheck_apply { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_precheck_apply: called CC=7 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 219 | 1 | (my MY $self, my ($subOrArrayOrString)) = @_; | |||||||||||||||||||||||||||||||
| 220 | 1 | ▶ | if (not defined $subOrArrayOrString) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 221 | 0 | Carp::croak "undefined sub for cli_apply"; | |||||||||||||||||||||||||||||||
| 222 | } elsif (ref $subOrArrayOrString eq 'CODE') { | ||||||||||||||||||||||||||||||||
| 223 | 0 | 1; | |||||||||||||||||||||||||||||||
| 224 | } else { | ||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | if (not ref $subOrArrayOrString or ref $subOrArrayOrString eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
MC/DC0%0/2
not ref $subOrArrayOrString or ref $subOrArrayOrString eq "ARRAY"
not ref $subOrArrayOrString
ref $subOrArrayOrString eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $subOrArrayOrStringB= ref $subOrArrayOrString eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 226 | 1 | my ($meth, @opts) = lexpand($subOrArrayOrString); | |||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | return if $self->can("cmd_$meth") || $self->can($meth); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->can("cmd_$meth") or $self->can($meth)
MC/DC0%0/2
$self->can("cmd_$meth") or $self->can($meth)
$self->can("cmd_$meth")
$self->can($meth)
Truth table33%1/3
A or B
A= $self->can("cmd_$meth")B= $self->can($meth)
| |||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | 0 | Carp::croak "Invalid argument for cli_apply: " | |||||||||||||||||||||||||||||||
| 230 | . MOP4Import::Util::terse_dump($subOrArrayOrString); | ||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | use MOP4Import::Types | ||||||||||||||||||||||||||||||||
| 235 | 12 | ▶ | cliopts__xargs => [[fields => qw/null slurp single json decode/]]; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | sub cli_xargs_json :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_xargs_json: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 238 | 1 | (my MY $self, my (@args)) = @_; | |||||||||||||||||||||||||||||||
| 239 | 1 | my cliopts__xargs $opts = $self->take_locked_opts_of( | |||||||||||||||||||||||||||||||
| 240 | cliopts__xargs, \@args, {0 => 'null', input => 'decode'}, | ||||||||||||||||||||||||||||||||
| 241 | ); | ||||||||||||||||||||||||||||||||
| 242 | 1 | ▶ | $opts->{decode} //= (($opts->{json} //=1) ? 'json' : ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"json"} //= 1
MC/DC0%0/1
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"decode"}
MC/DC0%0/1
$opts->{"json"} //= 1
$opts->{"json"}
Truth table50%1/2
A
A= $opts->{"decode"}
Truth table50%1/2
A
A= $opts->{"json"}
| |||||||||||||||||||||||||||||||||
| 243 | 1 | $self->_cli_xargs($opts, @args); | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 12 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||||||
| 247 | 12 | my ($packSuffix) = do { | |||||||||||||||||||||||||||||||
| 248 | 12 | ▶ | if ($] >= 5.022) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | 12 | 'compat_double_diamond'; | |||||||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||||||
| 251 | 0 | 'compat_double_diamond_5_20'; | |||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | }; | ||||||||||||||||||||||||||||||||
| 254 | 12 | (my $dir = __FILE__) =~ s,/?[^/]+\z,,; | |||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 12 | my $fn = File::Spec->rel2abs($dir) . "/../Util/$packSuffix.pm"; | |||||||||||||||||||||||||||||||
| 257 | 12 | ▶ | if (-r __FILE__ and not -r $fn) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-r __FILE__ and not -r $fn
MC/DC0%0/2
-r __FILE__ and not -r $fn
-r __FILE__
not -r $fn
Truth table33%1/3
A and not(B)
A= -r __FILE__B= not -r $fn
| |||||||||||||||||||||||||||||||||
| 258 | 0 | die "Can't load $fn"; | |||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||
| 260 | 12 | do $fn; | |||||||||||||||||||||||||||||||
| 261 | 12 | my $pack = 'MOP4Import::Util::'.$packSuffix; | |||||||||||||||||||||||||||||||
| 262 | 12 | $pack->import; | |||||||||||||||||||||||||||||||
| 263 | 12 | print STDERR "compat_diamond is loaded from $fn\n" if DEBUG and DEBUG >= 2; | |||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | 1 | ▶ | sub _cli_xargs { | ||||||||||||||||||||||||||||||
|
Subroutine
_cli_xargs: called CC=12 SCAR=33.2
| |||||||||||||||||||||||||||||||||
| 267 | 1 | (my MY $self, my (@args)) = @_; | |||||||||||||||||||||||||||||||
| 268 | 1 | my cliopts__xargs $opts = $self->take_locked_opts_of( | |||||||||||||||||||||||||||||||
| 269 | cliopts__xargs, \@args, {0 => 'null'}, | ||||||||||||||||||||||||||||||||
| 270 | ); | ||||||||||||||||||||||||||||||||
| 271 | 1 | my ($subOrArray, @restPrefix) = @args; | |||||||||||||||||||||||||||||||
| 272 | 1 | $self->cli_precheck_apply($subOrArray); | |||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 1 | ▶ | local $/ = $opts->{null} ? "\0" : "\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | 1 | local *ARGV; | |||||||||||||||||||||||||||||||
| 276 | 1 | ▶ | if ($opts->{slurp} || $opts->{single}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts->{"slurp"} or $opts->{"single"}
MC/DC0%0/2
$opts->{"slurp"} or $opts->{"single"}
$opts->{"slurp"}
$opts->{"single"}
Truth table33%1/3
A or B
A= $opts->{"slurp"}B= $opts->{"single"}
| |||||||||||||||||||||||||||||||||
| 277 | 0 | my @all = $self->cli_slurp_xargs($opts); | |||||||||||||||||||||||||||||||
| 278 | $self->cli_apply( | ||||||||||||||||||||||||||||||||
| 279 | $subOrArray, @restPrefix, | ||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | ($opts->{single} ? \@all : @all) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||||||
| 282 | } else { | ||||||||||||||||||||||||||||||||
| 283 | my $decoder = defined $opts->{decode} | ||||||||||||||||||||||||||||||||
| 284 | 1 | ▶ | ? $self->cli_decoder_from($opts->{decode}) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 285 | 1 | local $_; | |||||||||||||||||||||||||||||||
| 286 | 1 | ▶ | if (not ref $subOrArray and $self->can("cmd_$subOrArray")) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $subOrArray and $self->can("cmd_$subOrArray")
MC/DC0%0/2
not ref $subOrArray and $self->can("cmd_$subOrArray")
not ref $subOrArray
$self->can("cmd_$subOrArray")
Truth table33%1/3
not(A) and B
A= not ref $subOrArrayB= $self->can("cmd_$subOrArray")
| |||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | while (defined($_ = $self->cli_compat_diamond)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | 0 | chomp; | |||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | $self->cli_apply( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 290 | $subOrArray, @restPrefix, | ||||||||||||||||||||||||||||||||
| 291 | ($decoder ? $decoder->($_) : $_) | ||||||||||||||||||||||||||||||||
| 292 | ) | ||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | 0 | $self->{'no-exit-code'} = 1; | |||||||||||||||||||||||||||||||
| 295 | 0 | (); | |||||||||||||||||||||||||||||||
| 296 | } else { | ||||||||||||||||||||||||||||||||
| 297 | 1 | my @result; | |||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | while (defined($_ = $self->cli_compat_diamond)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 299 | 1 | chomp; | |||||||||||||||||||||||||||||||
| 300 | # XXX: yield... | ||||||||||||||||||||||||||||||||
| 301 | 1 | ▶ | push @result, $self->cli_apply( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 302 | $subOrArray, @restPrefix, | ||||||||||||||||||||||||||||||||
| 303 | ($decoder ? $decoder->($_) : $_) | ||||||||||||||||||||||||||||||||
| 304 | ) | ||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||
| 306 | 1 | @result; | |||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub cli_slurp_xargs_json { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_slurp_xargs_json: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 312 | 0 | (my MY $self, my (@args)) = @_; | |||||||||||||||||||||||||||||||
| 313 | 0 | my cliopts__xargs $opts = $self->take_locked_opts_of( | |||||||||||||||||||||||||||||||
| 314 | cliopts__xargs, \@args, {0 => 'null'}, | ||||||||||||||||||||||||||||||||
| 315 | ); | ||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $opts->{decode} //= (($opts->{json} //=1) ? 'json' : ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"json"} //= 1
MC/DC0%0/1
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"decode"}
MC/DC0%0/1
$opts->{"json"} //= 1
$opts->{"json"}
Truth table0%0/2
A
A= $opts->{"decode"}
Truth table0%0/2
A
A= $opts->{"json"}
| |||||||||||||||||||||||||||||||||
| 317 | 0 | $self->cli_slurp_xargs($opts, @args); | |||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | sub cli_slurp_xargs { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_slurp_xargs: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 321 | 0 | (my MY $self, my (@args)) = @_; | |||||||||||||||||||||||||||||||
| 322 | 0 | my cliopts__xargs $opts = $self->take_locked_opts_of( | |||||||||||||||||||||||||||||||
| 323 | cliopts__xargs, \@args, {0 => 'null'}, | ||||||||||||||||||||||||||||||||
| 324 | ); | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 0 | local @ARGV = @args; | |||||||||||||||||||||||||||||||
| 327 | my $decoder = defined $opts->{decode} | ||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | ? $self->cli_decoder_from($opts->{decode}) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | map { | ||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | $decoder ? $decoder->($_) : $_ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | } $self->cli_compat_diamond | ||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 1 | ▶ | sub cli_decoder_from { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_decoder_from: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 336 | 1 | (my MY $self, my ($formatSpec, @rest)) = @_; | |||||||||||||||||||||||||||||||
| 337 | 1 | my ($format, @opts) = lexpand($formatSpec); | |||||||||||||||||||||||||||||||
| 338 | 1 | ▶ | my $sub = $self->can("cli_decoder_from__$format") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 339 | or Carp::croak "Unknown decorder is requested: $format"; | ||||||||||||||||||||||||||||||||
| 340 | 1 | $sub->($self, @opts, @rest); | |||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | # | ||||||||||||||||||||||||||||||||
| 344 | # pass-through decoder. | ||||||||||||||||||||||||||||||||
| 345 | # | ||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | sub cli_decoder_from__ { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_decoder_from__: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | sub {$_[0]} | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 348 | 0 | } | |||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | # | ||||||||||||||||||||||||||||||||
| 351 | # json decoder | ||||||||||||||||||||||||||||||||
| 352 | # | ||||||||||||||||||||||||||||||||
| 353 | 3 | ▶ | sub cli_decoder_from__json { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_decoder_from__json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 354 | 3 | (my MY $self, my @opts) = @_; | |||||||||||||||||||||||||||||||
| 355 | 3 | my $decoder = $self->cli_json_decoder(qw/allow_nonref/, @opts); | |||||||||||||||||||||||||||||||
| 356 | 3 | ▶ | sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 357 | 3 | my ($str) = @_; | |||||||||||||||||||||||||||||||
| 358 | 3 | Encode::_utf8_off($str); | |||||||||||||||||||||||||||||||
| 359 | 3 | $decoder->decode($str); | |||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||
| 361 | 3 | } | |||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | #======================================== | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 73 | ▶ | sub declare_output_format { | ||||||||||||||||||||||||||||||
|
Subroutine
declare_output_format: called CC=6 SCAR=23.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 366 | 73 | (my $myPack, my Opts $opts, my ($formatName, $sub)) = m4i_args(@_); | |||||||||||||||||||||||||||||||
| 367 | 73 | my $encoderFuncName = "cli_encoder_to__$formatName"; | |||||||||||||||||||||||||||||||
| 368 | 73 | my $writeFuncName = "cli_write_fh_as_$formatName"; | |||||||||||||||||||||||||||||||
| 369 | 73 | my $outputFuncName = "cli_output_as_$formatName"; | |||||||||||||||||||||||||||||||
| 370 | 73 | ▶ | if (ref $sub eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 371 | 1 | *{globref($opts->{destpkg}, $writeFuncName)} = $sub; | |||||||||||||||||||||||||||||||
| 372 | 1 | ▶ | *{globref($opts->{destpkg}, $outputFuncName)} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 373 | 0 | shift->$writeFuncName(\*STDOUT, $_[0]); | |||||||||||||||||||||||||||||||
| 374 | 1 | }; | |||||||||||||||||||||||||||||||
| 375 | } elsif (not defined $sub) { | ||||||||||||||||||||||||||||||||
| 376 | 72 | ▶ | if ($opts->{destpkg}->can($writeFuncName)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 377 | 72 | ▶ | *{globref($opts->{destpkg}, $outputFuncName)} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 378 | 0 | shift->$writeFuncName(\*STDOUT, $_[0]); | |||||||||||||||||||||||||||||||
| 379 | 72 | }; | |||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | elsif ($opts->{destpkg}->can($encoderFuncName)) { | ||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | *{globref($opts->{destpkg}, $outputFuncName)} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 383 | 0 | shift->$encoderFuncName(\*STDOUT)->($_[0]); | |||||||||||||||||||||||||||||||
| 384 | 0 | }; | |||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | 0 | ▶ | unless ($opts->{destpkg}->can($writeFuncName)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | *{globref($opts->{destpkg}, $writeFuncName)} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 388 | 0 | shift->$encoderFuncName(shift)->(\@_); | |||||||||||||||||||||||||||||||
| 389 | 0 | }; | |||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||
| 392 | else { | ||||||||||||||||||||||||||||||||
| 393 | 0 | Carp::croak "output_format $formatName doesn't have method '$writeFuncName'"; | |||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||
| 395 | } else { | ||||||||||||||||||||||||||||||||
| 396 | 0 | Carp::croak "Invalid argument for output_format: " | |||||||||||||||||||||||||||||||
| 397 | . MOP4Import::Util::terse_dump($sub); | ||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | # cli_write_fh($outFH, @output) -- Output format abstraction | ||||||||||||||||||||||||||||||||
| 402 | # In cli_run context, cli_write_fh is called from cli_output($list) and | ||||||||||||||||||||||||||||||||
| 403 | # used as cli_write_fh(\*STDOUT, $list). | ||||||||||||||||||||||||||||||||
| 404 | # But in general, cli_write_fh can process multiple arguments at once. | ||||||||||||||||||||||||||||||||
| 405 | # | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | 17 | ▶ | sub cli_write_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh: called CC=10 SCAR=34.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 408 | 17 | (my MY $self, my ($outFH, @args)) = @_; | |||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | 17 | my ($outputFmt, @opts) = lexpand($self->{'output'}); | |||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||
| 412 | 17 | ▶ | if (my $sub = $self->can("cli_encoder_to__$outputFmt")) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 413 | 0 | my $encoder = $sub->($self, $outFH, @opts); | |||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | $encoder->($_) for ($self->{flatten} ? (map { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 415 | 0 | ▶ | (defined $_ && ref $_ eq 'ARRAY') ? @$_ : $_ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_ and ref $_ eq "ARRAY"
MC/DC0%0/2
defined $_ and ref $_ eq "ARRAY"
defined $_
ref $_ eq "ARRAY"
Truth table0%0/3
A and B
A= defined $_B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 416 | } @args) : @args); | ||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||
| 418 | elsif ($sub = $self->can("cli_write_fh_as_".$outputFmt)) { | ||||||||||||||||||||||||||||||||
| 419 | $sub->($self, $outFH, $self->{flatten} ? (map { | ||||||||||||||||||||||||||||||||
| 420 | 17 | ▶ | (defined $_ && ref $_ eq 'ARRAY') ? @$_ : $_ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $_ and ref $_ eq "ARRAY"
MC/DC0%0/2
defined $_ and ref $_ eq "ARRAY"
defined $_
ref $_ eq "ARRAY"
Truth table33%1/3
A and B
A= defined $_B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 421 | } @args) : @args); | ||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||
| 423 | else { | ||||||||||||||||||||||||||||||||
| 424 | 0 | Carp::croak("Unknown output format: $self->{'output'}"); | |||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | sub cli_json { JSON() } | ||||||||||||||||||||||||||||||
|
Subroutine
cli_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | 2 | ▶ | sub cli_decode_json { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_decode_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 431 | 2 | (my MY $self, my $string) = @_; | |||||||||||||||||||||||||||||||
| 432 | 2 | $self->cli_decoder_from__json->($string); | |||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | 23 | ▶ | sub cli_encode_json { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_encode_json: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 436 | 23 | (my MY $self, my ($obj, $json_type)) = @_; | |||||||||||||||||||||||||||||||
| 437 | 23 | my $json = $self->SUPER::cli_encode_json($obj, $json_type); | |||||||||||||||||||||||||||||||
| 438 | 23 | ▶ | Encode::_utf8_on($json) unless $self->{binary}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 439 | 23 | $json; | |||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||
| 442 | 23 | ▶ | sub cli_json_encoder { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_json_encoder: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 443 | 23 | (my MY $self) = @_; | |||||||||||||||||||||||||||||||
| 444 | 23 | my $js = $self->SUPER::cli_json_encoder; | |||||||||||||||||||||||||||||||
| 445 | 23 | ▶ | $js->utf8 unless $self->{binary}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 446 | 23 | $js; | |||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||
| 449 | 5 | ▶ | sub cli_json_decoder { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_json_decoder: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 450 | 5 | (my MY $self, my @opts) = @_; | |||||||||||||||||||||||||||||||
| 451 | 5 | my $js = JSON()->new->relaxed; | |||||||||||||||||||||||||||||||
| 452 | 5 | ▶ | $js->utf8 unless $self->{binary}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 453 | 5 | foreach my $opt (@opts) { | |||||||||||||||||||||||||||||||
| 454 | 3 | my ($method, @args) = lexpand($opt); | |||||||||||||||||||||||||||||||
| 455 | 3 | $js->$method(@args); | |||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||
| 457 | 5 | $js; | |||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | #---------------------------------------- | ||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | 2 | ▶ | sub cli_encode_as { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_encode_as: called CC=8 SCAR=24.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 463 | 2 | (my MY $self, my ($outputSpec, @items)) = @_; | |||||||||||||||||||||||||||||||
| 464 | 2 | my ($outputFmt, $layer, @opts) = lexpand($outputSpec); | |||||||||||||||||||||||||||||||
| 465 | 2 | ▶ | $outputFmt //= ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$outputFmt //= ""
MC/DC0%0/1
$outputFmt //= ""
$outputFmt
Truth table50%1/2
A
A= $outputFmt
| |||||||||||||||||||||||||||||||||
| 466 | # Allow $layer to be a HASH | ||||||||||||||||||||||||||||||||
| 467 | 2 | ▶ | if (defined $layer and ref $layer eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $layer and ref $layer eq "HASH"
MC/DC0%0/2
defined $layer and ref $layer eq "HASH"
defined $layer
ref $layer eq "HASH"
Truth table66%2/3
A and B
A= defined $layerB= ref $layer eq "HASH"
| |||||||||||||||||||||||||||||||||
| 468 | 0 | my %opts = %$layer; | |||||||||||||||||||||||||||||||
| 469 | 0 | $layer = delete $opts{layer}; | |||||||||||||||||||||||||||||||
| 470 | 0 | unshift @opts, %opts; | |||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||
| 472 | 2 | ▶ | $layer //= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$layer //= ""
MC/DC100%1/1
$layer //= ""
$layer
Truth table100%2/2
A
A= $layer
| |||||||||||||||||||||||||||||||||
| 473 | 2 | my $buffer = ""; | |||||||||||||||||||||||||||||||
| 474 | { | ||||||||||||||||||||||||||||||||
| 475 | 2 | open my $outFH, ">$layer", \$buffer; | |||||||||||||||||||||||||||||||
| 476 | 2 | ▶ | if (my $sub = $self->can("cli_encoder_to__$outputFmt")) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 477 | 0 | my $encoder = $sub->($self, $outFH, @opts); | |||||||||||||||||||||||||||||||
| 478 | 0 | foreach my $item (@items) { | |||||||||||||||||||||||||||||||
| 479 | 0 | $encoder->($item); | |||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||
| 482 | elsif ($sub = $self->can("cli_write_fh_as_$outputFmt")) { | ||||||||||||||||||||||||||||||||
| 483 | 2 | $sub->($self, $outFH, \@items); | |||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||
| 485 | else { | ||||||||||||||||||||||||||||||||
| 486 | 0 | Carp::croak "Unknown output format: '$outputFmt'"; | |||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||
| 489 | 2 | $buffer; | |||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||
| 492 | 9 | MY->declare_output_format(MY, 'ndjson'); | |||||||||||||||||||||||||||||||
| 493 | 7 | ▶ | sub cli_write_fh_as_ndjson { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_ndjson: called CC=7 SCAR=20.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 494 | 7 | (my MY $self, my ($outFH, @tables)) = @_; | |||||||||||||||||||||||||||||||
| 495 | 7 | foreach my $table (@tables) { | |||||||||||||||||||||||||||||||
| 496 | 7 | ▶ | foreach my $item (ref $table eq 'ARRAY' ? @$table : $table) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 497 | 12 | ▶ | print $outFH ((ref $item ? $self->cli_encode_json($item) : $item // $self->{'undef-as'} // ''), "\n"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
($item // $self->{"undef-as"}) // ""
$item // $self->{"undef-as"}
MC/DC0%0/2
($item // $self->{"undef-as"}) // ""
$item
$self->{"undef-as"}
Truth table33%1/3
A // B
A= $itemB= $self->{"undef-as"}
| |||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||
| 502 | 9 | MY->declare_output_format(MY, 'json'); | |||||||||||||||||||||||||||||||
| 503 | 3 | ▶ | sub cli_write_fh_as_json { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 504 | 3 | (my MY $self, my ($outFH, @args)) = @_; | |||||||||||||||||||||||||||||||
| 505 | 3 | foreach my $item (@args) { | |||||||||||||||||||||||||||||||
| 506 | 3 | print $outFH $self->cli_encode_json($item), "\n"; | |||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||
| 510 | 9 | MY->declare_output_format(MY, 'yaml'); | |||||||||||||||||||||||||||||||
| 511 | 1 | ▶ | sub cli_write_fh_as_yaml { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_yaml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 512 | 1 | (my MY $self, my ($outFH, @args)) = @_; | |||||||||||||||||||||||||||||||
| 513 | 1 | require YAML::Syck; | |||||||||||||||||||||||||||||||
| 514 | 1 | print $outFH YAML::Syck::Dump(@args); | |||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||
| 517 | 9 | MY->declare_output_format(MY, 'dump'); | |||||||||||||||||||||||||||||||
| 518 | 2 | ▶ | sub cli_write_fh_as_dump { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_dump: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 519 | 2 | (my MY $self, my ($outFH, @args)) = @_; | |||||||||||||||||||||||||||||||
| 520 | 2 | foreach my $item (@args) { | |||||||||||||||||||||||||||||||
| 521 | 2 | my $dumper = Data::Dumper->new($item) | |||||||||||||||||||||||||||||||
| 522 | ->Terse(1) | ||||||||||||||||||||||||||||||||
| 523 | ->Sortkeys(1) | ||||||||||||||||||||||||||||||||
| 524 | ->Indent(1) | ||||||||||||||||||||||||||||||||
| 525 | ->Deparse(1); | ||||||||||||||||||||||||||||||||
| 526 | 2 | ▶ | if (my $sub = $dumper->can("Trailingcomma")) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 527 | 2 | $sub->($dumper, 1); | |||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||
| 530 | 2 | print $outFH $dumper->Dump; | |||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||
| 534 | 9 | MY->declare_output_format(MY, 'raw'); | |||||||||||||||||||||||||||||||
| 535 | 1 | ▶ | sub cli_write_fh_as_raw { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_raw: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 536 | 1 | (my MY $self, my ($outFH, @tables)) = @_; | |||||||||||||||||||||||||||||||
| 537 | 1 | foreach my $table (@tables) { | |||||||||||||||||||||||||||||||
| 538 | 1 | ▶ | foreach my $item (ref $table eq 'ARRAY' ? @$table : $table) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 539 | 1 | print $outFH $item; | |||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | 9 | MY->declare_output_format(MY, 'tsv'); | |||||||||||||||||||||||||||||||
| 545 | 4 | ▶ | sub cli_write_fh_as_tsv { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_tsv: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 546 | 4 | (my MY $self, my ($outFH, @tables)) = @_; | |||||||||||||||||||||||||||||||
| 547 | 4 | foreach my $table (@tables) { | |||||||||||||||||||||||||||||||
| 548 | 4 | foreach my $rec (@$table) { | |||||||||||||||||||||||||||||||
| 549 | print $outFH join("\t", map { | ||||||||||||||||||||||||||||||||
| 550 | 12 | ▶ | if (not defined $_) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 551 | 1 | $self->{'undef-as'}; | |||||||||||||||||||||||||||||||
| 552 | } elsif (ref $_) { | ||||||||||||||||||||||||||||||||
| 553 | 5 | $self->cli_encode_json($_); | |||||||||||||||||||||||||||||||
| 554 | } else { | ||||||||||||||||||||||||||||||||
| 555 | 21 | my $cp = $_; | |||||||||||||||||||||||||||||||
| 556 | 21 | $cp =~ s/[\t\n]+/ /g; | |||||||||||||||||||||||||||||||
| 557 | 21 | ▶ | $cp | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||
| 559 | } ref $rec eq 'ARRAY' ? @$rec : $rec), "\n"; | ||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||
| 564 | #======================================== | ||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||
| 566 | 0 | ▶ | sub cli_create_from_file :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_create_from_file: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 567 | 0 | my ($class, $configFn, @moreOpts) = @_; | |||||||||||||||||||||||||||||||
| 568 | 0 | my $realConfigFn = File::Spec->rel2abs($configFn); | |||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | my $oldcwd = $ENV{PWD} || do {require Cwd; Cwd::getcwd()}; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'PWD'} || do {
require Cwd;
Cwd::getcwd()
}
MC/DC0%0/2
$ENV{'PWD'} || do {
require Cwd;
Cwd::getcwd()
}
$ENV{'PWD'}
do {
require Cwd;
Cwd::getcwd()
}
Truth table0%0/3
A || B
A= $ENV{'PWD'}B= do {
require Cwd;
Cwd::getcwd()
}
| |||||||||||||||||||||||||||||||||
| 570 | 0 | my $realDir = File::Basename::dirname($realConfigFn); | |||||||||||||||||||||||||||||||
| 571 | 0 | ▶ | chdir($realDir) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 572 | or Carp::croak "Can't chdir to $realDir: $!"; | ||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | # Read $configFn with scalar context. | ||||||||||||||||||||||||||||||||
| 575 | 0 | my $opts = $class->cli_read_file($realConfigFn); | |||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | 0 | ▶ | my $object = (ref $class || $class)->new( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 578 | ref $opts eq 'HASH' ? %$opts : @$opts, | ||||||||||||||||||||||||||||||||
| 579 | @moreOpts | ||||||||||||||||||||||||||||||||
| 580 | ); | ||||||||||||||||||||||||||||||||
| 581 | 0 | ▶ | chdir($oldcwd) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 582 | or Carp::croak "Can't chdir back to $oldcwd: $!"; | ||||||||||||||||||||||||||||||||
| 583 | 0 | $object; | |||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||
| 586 | 7 | ▶ | sub cli_read_file :method { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 587 | 7 | my ($classOrObj, $fileNameSpec, %moreOpts) = @_; | |||||||||||||||||||||||||||||||
| 588 | 7 | my ($fileName, %opts) = lexpand($fileNameSpec); | |||||||||||||||||||||||||||||||
| 589 | 7 | my ($ftype) = $fileName =~ m{\.(\w+)$}; | |||||||||||||||||||||||||||||||
| 590 | 7 | ▶ | $ftype //= ""; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$ftype //= ""
MC/DC100%1/1
$ftype //= ""
$ftype
Truth table100%2/2
A
A= $ftype
| |||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||
| 592 | 7 | ▶ | my $sub = $classOrObj->can("cli_read_file__$ftype") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 593 | or Carp::croak "Unsupported file type '$ftype': $fileName"; | ||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||
| 595 | 7 | $sub->($classOrObj, $fileName, %opts, %moreOpts); | |||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | # No filename extension => read entire content except last \n. | ||||||||||||||||||||||||||||||||
| 599 | 3 | ▶ | sub cli_read_file__ { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 600 | 3 | my ($classOrObj, $fileName) = @_; | |||||||||||||||||||||||||||||||
| 601 | 3 | ▶ | open my $fh, '<:utf8', $fileName | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 602 | or Carp::croak "Can't open $fileName: $!"; | ||||||||||||||||||||||||||||||||
| 603 | 3 | my $all = do {local $/; <$fh>}; | |||||||||||||||||||||||||||||||
| 604 | 3 | chomp($all); | |||||||||||||||||||||||||||||||
| 605 | 3 | $all; | |||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | # .txt => array of lines | ||||||||||||||||||||||||||||||||
| 609 | 2 | ▶ | sub cli_read_file__txt { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__txt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 610 | 2 | my ($classOrObj, $fileName) = @_; | |||||||||||||||||||||||||||||||
| 611 | 2 | my $all = $classOrObj->cli_read_file__($fileName); | |||||||||||||||||||||||||||||||
| 612 | 2 | my @list = split "\n", $all; | |||||||||||||||||||||||||||||||
| 613 | 2 | ▶ | wantarray ? @list : \@list; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||
| 616 | # .yml | ||||||||||||||||||||||||||||||||
| 617 | 9 | *cli_read_file__yaml = *cli_read_file__yml;*cli_read_file__yaml = *cli_read_file__yml; | |||||||||||||||||||||||||||||||
| 618 | 2 | ▶ | sub cli_read_file__yml { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__yml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 619 | 2 | my ($classOrObj, $fileName) = @_; | |||||||||||||||||||||||||||||||
| 620 | 2 | require YAML::Syck; | |||||||||||||||||||||||||||||||
| 621 | 2 | YAML::Syck::LoadFile($fileName); | |||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||
| 624 | # .json | ||||||||||||||||||||||||||||||||
| 625 | 2 | ▶ | sub cli_read_file__json { | ||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__json: called CC=7 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 626 | 2 | my ($classOrObj, $fileName, %opts) = @_; | |||||||||||||||||||||||||||||||
| 627 | 2 | my $allow_comments = delete $opts{allow_comments}; | |||||||||||||||||||||||||||||||
| 628 | 2 | ▶ | open my $fh, '<', $fileName | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 629 | or Carp::croak "Can't open $fileName: $!"; | ||||||||||||||||||||||||||||||||
| 630 | 2 | my $all = do {local $/; <$fh>}; | |||||||||||||||||||||||||||||||
| 631 | 2 | ▶ | unless (defined $all) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 632 | 0 | Carp::croak "Can't read $fileName: $!"; | |||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||
| 634 | 2 | ▶ | if ($allow_comments) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 635 | 2 | require MOP4Import::Util::CommentedJson; | |||||||||||||||||||||||||||||||
| 636 | 2 | local $@; | |||||||||||||||||||||||||||||||
| 637 | 2 | eval { | |||||||||||||||||||||||||||||||
| 638 | 2 | $all = MOP4Import::Util::CommentedJson->strip_json_comments($all); | |||||||||||||||||||||||||||||||
| 639 | }; | ||||||||||||||||||||||||||||||||
| 640 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 641 | 0 | Carp::carp "Can't strip comment in $fileName: $@"; | |||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||||||
| 644 | 2 | local $@; | |||||||||||||||||||||||||||||||
| 645 | 2 | my @result; | |||||||||||||||||||||||||||||||
| 646 | 2 | eval { | |||||||||||||||||||||||||||||||
| 647 | 2 | @result = $classOrObj->cli_json_decoder->incr_parse($all); | |||||||||||||||||||||||||||||||
| 648 | }; | ||||||||||||||||||||||||||||||||
| 649 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 650 | 0 | Carp::croak "decode_json failed in $fileName: $@"; | |||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||
| 652 | 2 | ▶ | @result >= 2 ? \@result : $result[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||
| 655 | 9 | ▶ | MY->cli_run(\@ARGV) unless caller; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||
| 657 | 9 | 1; | |||||||||||||||||||||||||||||||