Class-Usul
view release on metacpan or search on metacpan
lib/Class/Usul/TraitFor/RunningMethods.pm view on Meta::CPAN
package Class::Usul::TraitFor::RunningMethods;
use namespace::autoclean;
use Class::Usul::Constants qw( FAILED NUL OK TRUE UNDEFINED_RV );
use Class::Usul::Functions qw( dash2under elapsed emit_to exception is_hashref
is_member logname throw untaint_identifier );
use Class::Usul::Types qw( ArrayRef HashRef Int SimpleStr );
use English qw( -no_match_vars );
use File::DataClass::Types qw( OctalNum );
use Scalar::Util qw( blessed );
use Try::Tiny;
use Moo::Role;
use Class::Usul::Options;
requires qw( app_version can_call debug error exit_usage
extra_argv file next_argv output quiet );
# Public attributes
option 'method' => is => 'rwp', isa => SimpleStr, format => 's',
documentation => 'Name of the method to call',
default => NUL, order => 1, short => 'c';
option 'options' => is => 'ro', isa => HashRef, format => 's%',
documentation =>
'Zero, one or more key=value pairs available to the method call',
builder => sub { {} }, short => 'o';
option 'umask' => is => 'rw', isa => OctalNum, format => 's',
documentation => 'Set the umask to this octal number',
builder => sub { $_[ 0 ]->config->umask }, coerce => TRUE,
lazy => TRUE;
option 'verbose' => is => 'ro', isa => Int,
documentation => 'Increase the verbosity of the output',
default => 0, repeatable => TRUE, short => 'v';
has 'params' => is => 'lazy', isa => HashRef[ArrayRef],
builder => sub { {} };
# Private functions
my $_output_stacktrace = sub {
my ($e, $verbose) = @_; ($e and blessed $e) or return; $verbose //= 0;
$verbose > 0 and $e->can( 'trace' )
and return emit_to \*STDERR, $e->trace.NUL;
$e->can( 'stacktrace' ) and emit_to \*STDERR, $e->stacktrace.NUL;
return;
};
# Private methods
my $handle_result = sub {
my ($self, $method, $rv) = @_;
my $params = $self->params->{ $method };
my $args = (defined $params ) ? $params->[ 0 ] : undef;
my $expected_rv = (is_hashref $args) ? $args->{expected_rv} // OK : OK;
if (defined $rv and $rv <= $expected_rv) {
$self->quiet or $self->output
( 'Finished in [_1] seconds', { args => [ elapsed ] } );
}
elsif (defined $rv and $rv > OK) {
$self->error( 'Terminated code [_1]', {
args => [ $rv ], no_quote_bind_values => TRUE } );
}
else {
if ($rv == UNDEFINED_RV) { $self->error( 'Terminated with undefined rv' )}
else {
if (defined $rv) {
$self->error
( 'Method [_1] unknown rv [_2]', { args => [ $method, $rv ] } );
}
else {
$self->error( 'Method [_1] error uncaught or rv undefined',
{ args => [ $method ] } );
$rv = UNDEFINED_RV;
}
}
}
return $rv;
};
( run in 0.526 second using v1.01-cache-2.11-cpan-39bf76dae61 )