App-Framework
view release on metacpan or search on metacpan
lib/App/Framework/Feature/Run.pm view on Meta::CPAN
=cut
my %FIELDS = (
# Object Data
'cmd' => undef,
'args' => undef,
'timeout' => undef,
'nice' => undef,
'dryrun' => 0,
'on_error' => $ON_ERROR_DEFAULT,
'error_str' => "",
'required' => {},
'check_results' => undef,
'progress' => undef,
'status' => 0,
'results' => [],
# Options/flags
'norun' => 0,
'log' => {
'all' => 0,
'cmd' => 0,
'results' => 0,
'status' => 0,
},
## Private
'_logobj' => undef,
) ;
#============================================================================================
=head2 CONSTRUCTOR
=over 4
=cut
#============================================================================================
=item B<new([%args])>
Create a new Run.
The %args are specified as they would be in the B<set> method (see L</Fields>).
=cut
sub new
{
my ($obj, %args) = @_ ;
my $class = ref($obj) || $obj ;
# Create object
my $this = $class->SUPER::new(%args) ;
return($this) ;
}
#============================================================================================
=back
=head2 CLASS METHODS
=over 4
=cut
#============================================================================================
#-----------------------------------------------------------------------------
=item B<init_class([%args])>
Initialises the Run object class variables.
=cut
sub init_class
{
my $class = shift ;
my (%args) = @_ ;
# Add extra fields
$class->add_fields(\%FIELDS, \%args) ;
# init class
$class->SUPER::init_class(%args) ;
}
#============================================================================================
=back
=head2 OBJECT DATA METHODS
=over 4
=cut
#============================================================================================
#-----------------------------------------------------------------------------
=item B<required([$required_href])>
Get/set the required programs list. If specified, B<$required_href> is a HASH ref where the
keys are the names of the required programs (the values are unimportant).
This method returns the B<$required_href> HASH ref having set the values associated with the
program name keys to the path for that program. Where a program is not found then
it's path is set to undef.
Also, if the L</on_error> field is set to 'warning' or 'fatal' then this method throws a warning
or fatal error if one or more required programs are not found. Sets the message string to indicate
which programs were not found.
=cut
sub required
{
my $this = shift ;
my ($new_required_href) = @_ ;
## my $required_href = $this->SUPER::required($new_required_href) ;
my $required_href = $this->field_access('required', $new_required_href) ;
if ($new_required_href)
{
## Test for available executables
foreach my $exe (keys %$new_required_href)
{
$required_href->{$exe} = which($exe) ;
}
## check for errors
my $throw = $this->_throw_on_error($this->on_error) ;
if ($throw)
{
my $error = "" ;
foreach my $exe (keys %$new_required_href)
{
if (!$required_href->{$exe})
{
$error .= " $exe\n" ;
}
}
if ($error)
{
$this->$throw("The following programs are required but not available:\n$error\n") ;
}
}
}
return $required_href ;
}
#============================================================================================
=back
=head2 OBJECT METHODS
=over 4
=cut
#============================================================================================
#--------------------------------------------------------------------------------------------
=item B<run( [args] )>
Execute a command if B<args> are specified. Whether B<args> are specified or not, always returns the run object.
This method has reasonably flexible arguments which can be one of:
=item (%args)
The args HASH contains the information needed to set the L</FIELDS> and then run teh command for example:
('cmd' => 'ping', 'args' => $host)
=item ($cmd)
( run in 1.080 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )