| 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 | BEGIN { | ||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | print STDERR "Using (file '" . __FILE__ . "')\n" | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | , (DEBUG >= 3 ? Carp::longmess("callstack:") : ()) | ||||||||||||||||||||||||||||||||||||||
| 11 | if DEBUG and DEBUG >= 2 | ||||||||||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 12 | use MOP4Import::Base::CLI -as_base | |||||||||||||||||||||||||||||||||||||
| 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 | ▶ | ]; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_precmd { | ||||||||||||||||||||||||||||||||||||||
| 51 | 23 | ▶ | (my MY $self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_precmd: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_parse_opts { | ||||||||||||||||||||||||||||||||||||||
| 64 | 23 | ▶ | my ($pack, $list, $result, $opt_alias, $converter, %opts) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_parse_opts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 23 | MOP4Import::Util::parse_json_opts($pack, $list, $result, $opt_alias); | |||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | sub cli_eval { | ||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | (my MY $self, my ($script, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_eval: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_invoke { | ||||||||||||||||||||||||||||||||||||||
| 82 | 19 | ▶ | (my MY $self, my ($method, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_invoke: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_invoke_sub { | ||||||||||||||||||||||||||||||||||||||
| 95 | 19 | ▶ | (my MY $self, my ($sub, $receiver, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_invoke_sub: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_output :method { | ||||||||||||||||||||||||||||||||||||||
| 113 | 19 | ▶ | (my MY $self, my ($list)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_output: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | 19 | ▶ | unless ($self->{scalar} ? $list->[0] : @$list) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 116 | 2 | return; | |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | my $emitter = ref $self->{output} eq 'CODE' ? $self->{output} : sub { | ||||||||||||||||||||||||||||||||||||||
| 120 | 17 | ▶ | $self->cli_write_fh(\*STDOUT, @_); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_capture_output { | ||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | (my MY $self, my ($subOrArrayOrString, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_capture_output: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my @result; | |||||||||||||||||||||||||||||||||||||
| 136 | local $self->{output} = sub { | ||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | push @result, \@_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 138 | 0 | }; | |||||||||||||||||||||||||||||||||||||
| 139 | 0 | $self->cli_apply($subOrArrayOrString, @args); | |||||||||||||||||||||||||||||||||||||
| 140 | 0 | @result; | |||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | sub cli_examine_result { | ||||||||||||||||||||||||||||||||||||||
| 144 | 5 | ▶ | (my MY $self, my $list) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_examine_result: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_exit_for_result { | ||||||||||||||||||||||||||||||||||||||
| 156 | 5 | ▶ | (my MY $self, my $list) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_exit_for_result: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | exit($self->cli_examine_result($list) ? 0 : 1); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||
| 161 | #======================================== | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | sub cli_array :method { | ||||||||||||||||||||||||||||||||||||||
| 164 | 4 | ▶ | (my MY $self, my @args) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_array: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 165 | 4 | \@args; | |||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | sub cli_list :method { | ||||||||||||||||||||||||||||||||||||||
| 169 | 4 | ▶ | (my MY $self, my @args) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_list: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 170 | 4 | @args; | |||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | sub cli_object :method { | ||||||||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | (my MY $self, my %args) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 175 | 1 | \%args; | |||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | sub cli_identity :method { | ||||||||||||||||||||||||||||||||||||||
| 179 | 2 | ▶ | (my MY $self, my ($thing)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_identity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 180 | 2 | $thing; | |||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | sub cli_map_apply :method { | ||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | (my MY $self, my ($subOrArray, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_map_apply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 185 | map { | ||||||||||||||||||||||||||||||||||||||
| 186 | 0 | $self->cli_apply($subOrArray, $_); | |||||||||||||||||||||||||||||||||||||
| 187 | } @args; | ||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||
| 190 | sub cli_grep_apply :method { | ||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | (my MY $self, my ($subOrArray, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_grep_apply: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 192 | grep { | ||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $self->cli_apply($subOrArray, $_); | |||||||||||||||||||||||||||||||||||||
| 194 | } @args; | ||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | # XXX: How about cli_reduce_apply? | ||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||
| 199 | sub cli_apply :method { | ||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | (my MY $self, my ($subOrArrayOrString, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_apply: called CC=6 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_precheck_apply { | ||||||||||||||||||||||||||||||||||||||
| 219 | 1 | ▶ | (my MY $self, my ($subOrArrayOrString)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_precheck_apply: called CC=7 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_xargs_json :method { | ||||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | (my MY $self, my (@args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_xargs_json: called CC=4 SCAR=15.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition40%2/5
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"json"} //= 1
MC/DC0%0/2
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"decode"}
($opts->{"json"} //= 1) ? "json" : ""
MC/DC0%0/1
$opts->{"json"} //= 1
$opts->{"json"}
Truth table33%1/3
A //= B
A= $opts->{"decode"}B= ($opts->{"json"} //= 1) ? "json" : ""
Truth table50%1/2
A
A= $opts->{"json"}
| |||||||||||||||||||||||||||||||||||||||
| 243 | 1 | $self->_cli_xargs($opts, @args); | |||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||
| 246 | BEGIN { | ||||||||||||||||||||||||||||||||||||||
| 247 | 12 | ▶ | my ($packSuffix) = do { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub _cli_xargs { | ||||||||||||||||||||||||||||||||||||||
| 267 | 1 | ▶ | (my MY $self, my (@args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_cli_xargs: called CC=12 SCAR=33.2
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_slurp_xargs_json { | ||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | (my MY $self, my (@args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_slurp_xargs_json: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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/5
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"json"} //= 1
MC/DC0%0/2
$opts->{"decode"} //= ($opts->{"json"} //= 1) ? "json" : ""
$opts->{"decode"}
($opts->{"json"} //= 1) ? "json" : ""
MC/DC0%0/1
$opts->{"json"} //= 1
$opts->{"json"}
Truth table0%0/3
A //= B
A= $opts->{"decode"}B= ($opts->{"json"} //= 1) ? "json" : ""
Truth table0%0/2
A
A= $opts->{"json"}
| |||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $self->cli_slurp_xargs($opts, @args); | |||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||
| 320 | sub cli_slurp_xargs { | ||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | (my MY $self, my (@args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_slurp_xargs: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_decoder_from { | ||||||||||||||||||||||||||||||||||||||
| 336 | 1 | ▶ | (my MY $self, my ($formatSpec, @rest)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_decoder_from: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_decoder_from__ { | ||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | sub {$_[0]} | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_decoder_from__: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||
| 350 | # | ||||||||||||||||||||||||||||||||||||||
| 351 | # json decoder | ||||||||||||||||||||||||||||||||||||||
| 352 | # | ||||||||||||||||||||||||||||||||||||||
| 353 | sub cli_decoder_from__json { | ||||||||||||||||||||||||||||||||||||||
| 354 | 3 | ▶ | (my MY $self, my @opts) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_decoder_from__json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 355 | 3 | my $decoder = $self->cli_json_decoder(qw/allow_nonref/, @opts); | |||||||||||||||||||||||||||||||||||||
| 356 | sub { | ||||||||||||||||||||||||||||||||||||||
| 357 | 3 | ▶ | my ($str) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 358 | 3 | Encode::_utf8_off($str); | |||||||||||||||||||||||||||||||||||||
| 359 | 3 | $decoder->decode($str); | |||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||
| 361 | 3 | } | |||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||
| 363 | #======================================== | ||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||
| 365 | sub declare_output_format { | ||||||||||||||||||||||||||||||||||||||
| 366 | 73 | ▶ | (my $myPack, my Opts $opts, my ($formatName, $sub)) = m4i_args(@_); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
declare_output_format: called CC=6 SCAR=23.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 { | |||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | shift->$writeFuncName(\*STDOUT, $_[0]); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 374 | 1 | }; | |||||||||||||||||||||||||||||||||||||
| 375 | } elsif (not defined $sub) { | ||||||||||||||||||||||||||||||||||||||
| 376 | 72 | ▶ | if ($opts->{destpkg}->can($writeFuncName)) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 377 | 72 | *{globref($opts->{destpkg}, $outputFuncName)} = sub { | |||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | shift->$writeFuncName(\*STDOUT, $_[0]); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 379 | 72 | }; | |||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||
| 381 | elsif ($opts->{destpkg}->can($encoderFuncName)) { | ||||||||||||||||||||||||||||||||||||||
| 382 | 0 | *{globref($opts->{destpkg}, $outputFuncName)} = sub { | |||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | shift->$encoderFuncName(\*STDOUT)->($_[0]); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 384 | 0 | }; | |||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||
| 386 | 0 | ▶ | unless ($opts->{destpkg}->can($writeFuncName)) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 387 | 0 | *{globref($opts->{destpkg}, $writeFuncName)} = sub { | |||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | shift->$encoderFuncName(shift)->(\@_); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_write_fh { | ||||||||||||||||||||||||||||||||||||||
| 408 | 17 | ▶ | (my MY $self, my ($outFH, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh: called CC=10 SCAR=34.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_decode_json { | ||||||||||||||||||||||||||||||||||||||
| 431 | 2 | ▶ | (my MY $self, my $string) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_decode_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 432 | 2 | $self->cli_decoder_from__json->($string); | |||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||
| 435 | sub cli_encode_json { | ||||||||||||||||||||||||||||||||||||||
| 436 | 23 | ▶ | (my MY $self, my ($obj, $json_type)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_encode_json: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_json_encoder { | ||||||||||||||||||||||||||||||||||||||
| 443 | 23 | ▶ | (my MY $self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_json_encoder: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_json_decoder { | ||||||||||||||||||||||||||||||||||||||
| 450 | 5 | ▶ | (my MY $self, my @opts) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_json_decoder: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_encode_as { | ||||||||||||||||||||||||||||||||||||||
| 463 | 2 | ▶ | (my MY $self, my ($outputSpec, @items)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_encode_as: called CC=8 SCAR=24.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_write_fh_as_ndjson { | ||||||||||||||||||||||||||||||||||||||
| 494 | 7 | ▶ | (my MY $self, my ($outFH, @tables)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_ndjson: called CC=7 SCAR=20.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_write_fh_as_json { | ||||||||||||||||||||||||||||||||||||||
| 504 | 3 | ▶ | (my MY $self, my ($outFH, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_json: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_write_fh_as_yaml { | ||||||||||||||||||||||||||||||||||||||
| 512 | 1 | ▶ | (my MY $self, my ($outFH, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_yaml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 513 | 1 | require YAML::Syck; | |||||||||||||||||||||||||||||||||||||
| 514 | 1 | print $outFH YAML::Syck::Dump(@args); | |||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||
| 517 | 9 | MY->declare_output_format(MY, 'dump'); | |||||||||||||||||||||||||||||||||||||
| 518 | sub cli_write_fh_as_dump { | ||||||||||||||||||||||||||||||||||||||
| 519 | 2 | ▶ | (my MY $self, my ($outFH, @args)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_dump: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_write_fh_as_raw { | ||||||||||||||||||||||||||||||||||||||
| 536 | 1 | ▶ | (my MY $self, my ($outFH, @tables)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_raw: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_write_fh_as_tsv { | ||||||||||||||||||||||||||||||||||||||
| 546 | 4 | ▶ | (my MY $self, my ($outFH, @tables)) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_write_fh_as_tsv: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_create_from_file :method { | ||||||||||||||||||||||||||||||||||||||
| 567 | 0 | ▶ | my ($class, $configFn, @moreOpts) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_create_from_file: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_read_file :method { | ||||||||||||||||||||||||||||||||||||||
| 587 | 7 | ▶ | my ($classOrObj, $fileNameSpec, %moreOpts) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_read_file__ { | ||||||||||||||||||||||||||||||||||||||
| 600 | 3 | ▶ | my ($classOrObj, $fileName) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_read_file__txt { | ||||||||||||||||||||||||||||||||||||||
| 610 | 2 | ▶ | my ($classOrObj, $fileName) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__txt: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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 | sub cli_read_file__yml { | ||||||||||||||||||||||||||||||||||||||
| 619 | 2 | ▶ | my ($classOrObj, $fileName) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__yml: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 620 | 2 | require YAML::Syck; | |||||||||||||||||||||||||||||||||||||
| 621 | 2 | YAML::Syck::LoadFile($fileName); | |||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||
| 624 | # .json | ||||||||||||||||||||||||||||||||||||||
| 625 | sub cli_read_file__json { | ||||||||||||||||||||||||||||||||||||||
| 626 | 2 | ▶ | my ($classOrObj, $fileName, %opts) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cli_read_file__json: called CC=7 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||