| 1 | package Class::Usul::TraitFor::RunningMethods; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use Class::Usul::Constants qw( FAILED NUL OK TRUE UNDEFINED_RV ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use Class::Usul::Functions qw( dash2under elapsed emit_to exception is_hashref | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 17 | is_member logname throw untaint_identifier ); | |||||||||||||||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use Class::Usul::Types qw( ArrayRef HashRef Int SimpleStr ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use English qw( -no_match_vars ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use File::DataClass::Types qw( OctalNum ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 17 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 17 | ▶ | use Class::Usul::Options; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 17 | requires qw( app_version can_call debug error exit_usage | |||||||||||||||||||||||||||||||||||||||||
| 17 | extra_argv file next_argv output quiet ); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Public attributes | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 17 | option 'method' => is => 'rwp', isa => SimpleStr, format => 's', | |||||||||||||||||||||||||||||||||||||||||
| 21 | documentation => 'Name of the method to call', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | default => NUL, order => 1, short => 'c'; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | option 'options' => is => 'ro', isa => HashRef, format => 's%', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | documentation => | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'Zero, one or more key=value pairs available to the method call', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 11 | ▶ | builder => sub { {} }, short => 'o'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_options: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | option 'umask' => is => 'rw', isa => OctalNum, format => 's', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | documentation => 'Set the umask to this octal number', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | builder => sub { $_[ 0 ]->config->umask }, coerce => TRUE, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_umask: called CC=1 SCAR=0.4
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | lazy => TRUE; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 17 | option 'verbose' => is => 'ro', isa => Int, | |||||||||||||||||||||||||||||||||||||||||
| 35 | documentation => 'Increase the verbosity of the output', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | default => 0, repeatable => TRUE, short => 'v'; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | has 'params' => is => 'lazy', isa => HashRef[ArrayRef], | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | builder => sub { {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
Subroutine
_build_params: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # Private functions | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $_output_stacktrace = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my ($e, $verbose) = @_; ($e and blessed $e) or return; $verbose //= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$e and builtin::blessed($e)
$verbose //= 0
MC/DC0%0/2
$e and builtin::blessed($e)
$e
builtin::blessed($e)
MC/DC0%0/1
$verbose //= 0
$verbose
Truth table0%0/3
A and B
A= $eB= builtin::blessed($e)
Truth table0%0/2
A
A= $verbose
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $verbose > 0 and $e->can( 'trace' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$verbose > 0 and $e->can("trace")
MC/DC0%0/2
$verbose > 0 and $e->can("trace")
$verbose > 0
$e->can("trace")
Truth table0%0/3
A and B
A= $verbose > 0B= $e->can("trace")
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | and return emit_to \*STDERR, $e->trace.NUL; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $e->can( 'stacktrace' ) and emit_to \*STDERR, $e->stacktrace.NUL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # Private methods | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | my $handle_result = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=30.7
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | my ($self, $method, $rv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | my $params = $self->params->{ $method }; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | my $args = (defined $params ) ? $params->[ 0 ] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | ▶ | my $expected_rv = (is_hashref $args) ? $args->{expected_rv} // OK : OK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$args->{"expected_rv"} // 0
MC/DC0%0/1
$args->{"expected_rv"} // 0
$args->{"expected_rv"}
Truth table50%1/2
A
A= $args->{"expected_rv"}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if (defined $rv and $rv <= $expected_rv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $rv and $rv <= $expected_rv
MC/DC50%1/2
defined $rv and $rv <= $expected_rv
defined $rv
$rv <= $expected_rv
Truth table66%2/3
A and B
A= defined $rvB= $rv <= $expected_rv
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | $self->quiet or $self->output | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | ( 'Finished in [_1] seconds', { args => [ elapsed ] } ); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ▶ | elsif (defined $rv and $rv > OK) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $rv and $rv > 0
MC/DC0%0/2
defined $rv and $rv > 0
defined $rv
$rv > 0
Truth table33%1/3
A and B
A= defined $rvB= $rv > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $self->error( 'Terminated code [_1]', { | |||||||||||||||||||||||||||||||||||||||||
| 66 | args => [ $rv ], no_quote_bind_values => TRUE } ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($rv == UNDEFINED_RV) { $self->error( 'Terminated with undefined rv' )} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (defined $rv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $self->error | |||||||||||||||||||||||||||||||||||||||||
| 73 | ( 'Method [_1] unknown rv [_2]', { args => [ $method, $rv ] } ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self->error( 'Method [_1] error uncaught or rv undefined', | |||||||||||||||||||||||||||||||||||||||||
| 77 | { args => [ $method ] } ); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $rv = UNDEFINED_RV; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | return $rv; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $_handle_run_exception = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my ($self, $method, $error) = @_; my $e; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless ($e = exception $error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->error | |||||||||||||||||||||||||||||||||||||||||
| 91 | ( 'Method [_1] exception without error', { args => [ $method ] } ); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return UNDEFINED_RV; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $e->can( 'out' ) and $e->out and $self->output( $e->out ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$e->can("out") and $e->out
MC/DC0%0/2
$e->can("out") and $e->out
$e->can("out")
$e->out
Truth table0%0/3
A and B
A= $e->can("out")B= $e->out
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $self->error( $e->error, { args => $e->args } ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $self->debug and $_output_stacktrace->( $error, $self->verbose ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return $e->can( 'rv' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$e->rv or defined $e->rv ? 1 : -1
MC/DC0%0/2
$e->rv or defined $e->rv ? 1 : -1
$e->rv
defined $e->rv ? 1 : -1
Truth table0%0/3
A or B
A= $e->rvB= defined $e->rv ? 1 : -1
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | ? ($e->rv || (defined $e->rv ? FAILED : UNDEFINED_RV)) : UNDEFINED_RV; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Public methods | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | my $method = $self->select_method; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 4 | my $text = 'Started by [_1] Version [_2] Pid [_3]'; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | my $args = { args => [ logname, $self->app_version, abs $PID ] }; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | (is_member $method, 'help', 'run_chain') and $self->quiet( TRUE ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | $self->quiet or $self->output( $text, $args ); umask $self->umask; my $rv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | if ($method eq 'run_chain' or $self->can_call( $method )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$method eq "run_chain" or $self->can_call($method)
MC/DC0%0/2
$method eq "run_chain" or $self->can_call($method)
$method eq "run_chain"
$self->can_call($method)
Truth table33%1/3
A or B
A= $method eq "run_chain"B= $self->can_call($method)
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | my $params = exists $self->params->{ $method } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | ▶ | ? $self->params->{ $method } : []; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | defined ($rv = $self->$method( @{ $params } )) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | or throw 'Method [_1] return value undefined', | ||||||||||||||||||||||||||||||||||||||||||
| 121 | args => [ $method ], rv => UNDEFINED_RV; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | catch { $rv = $self->$_handle_run_exception( $method, $_ ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self->error( 'Class [_1] method [_2] not found', | |||||||||||||||||||||||||||||||||||||||||
| 127 | { args => [ blessed $self, $method ] } ); | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $rv = UNDEFINED_RV; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | $rv = $self->$handle_result( $method, $rv ); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | $self->file->delete_tmp_files; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 4 | return $rv; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub run_chain { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_chain: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; my $args = { args => [ $self->method ] }; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $self->method ? $self->error( 'Method [_1] unknown', $args ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | : $self->error( 'Method not specified' ); | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $self->exit_usage( 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return; # Not reached | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub select_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_method: called CC=7 SCAR=32.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | my $self = shift; my $method = untaint_identifier dash2under $self->method; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | unless ($self->can_call( $method )) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | $method = untaint_identifier dash2under $self->extra_argv( 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $method and $self->_set_method( $method ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | ($method and $self->can_call( $method ) and $self->next_argv) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$method and $self->can_call($method) and $self->next_argv
$method and $self->can_call($method)
MC/DC0%0/3
$method and $self->can_call($method) and $self->next_argv
$method
$self->can_call($method)
$self->next_argv
Truth table0%0/4
A and B and C
A= $methodB= $self->can_call($method)C= $self->next_argv
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | or $method = undef; | ||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | ▶ | return $method ? $method : 'run_chain'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||||||||||||||||