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
lib/Class/Usul/TraitFor/RunningMethods.pm view on Meta::CPAN
=item C<umask>
An octal number which is used to set the umask by the L</run> method
=item C<v verbose>
Repeatable boolean that increases the verbosity of the output
=back
Defines the following attributes;
=over 3
=item C<params>
A hash reference keyed by method name. The values are array references which
are flattened and passed to the method call by L</run>
=back
=head1 Subroutines/Methods
=head2 run
$exit_code = $self->run;
Call the method specified by the C<-c> option on the command
line. Returns the exit code
=head2 run_chain
$exit_code = $self->run_chain( $method );
Called by L</run> when L</select_method> cannot determine which method to
call. Outputs usage if C<method> is undefined. Logs an error if
C<method> is defined but not (by definition a callable method).
Returns exit code C<FAILED>
=head2 select_method
$method = $self->select_method;
Called by L</run> it examines the L</method> attribute and if necessary the
extra command line arguments to determine the method to call
=head1 Diagnostics
None
=head1 Dependencies
=over 3
=item L<Class::Usul::Options>
=item L<File::DataClass>
=item L<Moo::Role>
=item L<Try::Tiny>
=back
=head1 Incompatibilities
There are no known incompatibilities in this module
=head1 Bugs and Limitations
There are no known bugs in this module. Please report problems to
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Usul.
Patches are welcome
=head1 Acknowledgements
Larry Wall - For the Perl programming language
=head1 Author
Peter Flanigan, C<< <pjfl@cpan.org> >>
=head1 License and Copyright
Copyright (c) 2018 Peter Flanigan. All rights reserved
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. See L<perlartistic>
This program is distributed in the hope that it will be useful,
but WITHOUT WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
=cut
# Local Variables:
# mode: perl
# tab-width: 3
# End:
# vim: expandtab shiftwidth=3:
( run in 0.895 second using v1.01-cache-2.11-cpan-39bf76dae61 )