Class::Usul::IPC::Cmd - Execute system commands
use Class::Usul::IPC::Cmd; sub run_cmd { my ($self, $cmd, @args) = @_; my $attr = arg_list @args; $attr->{cmd } = $cmd or throw Unspecified, [ 'command' ]; $attr->{log } = $self->log; $attr->{rundir } = $self->config->rundir; $attr->{tempdir} = $self->config->tempdir; return Class::Usul::IPC::Cmd->new( $attr )->run_cmd; } $self->run_cmd( [ 'perl', '-v' ], { async => 1 } ); # Alternatively there is a functional interface use Class::Usul::IPC::Cmd { tempdir => ... }, 'run_cmd'; run_cmd( [ 'perl', '-v' ], { async => 1 } );
Refactored IPC::Cmd with a consistent OO API
Would have used MooseX::Daemonize but using Moo not Moose so robbed some code from there instead
Defines the following attributes;
async
Boolean defaults to false. If true the call to run_cmd will return without waiting for the child process to complete. If true the ignore_zombies attribute will default to true
run_cmd
ignore_zombies
close_all_files
Boolean defaults to false. If true and the detach attribute is also true then all open file descriptors in the child are closed except those in the keep_fhs list attribute
detach
keep_fhs
cmd
An array reference or a simple string. Required. The external command to execute
Boolean defaults to false. If true the child process will double fork, set the session id and ignore hangup signals
err
A File::DataClass::IO object reference or a simple str. Defaults to null. Determines where the standard error of the command will be redirected to. Values are the same as for out. Additionally a value of 'out' will redirect standard error to standard output
out
expected_rv
Positive integer default to zero. The maximum return value which is considered a success
Boolean defaults to false unless the async attribute is true in which case this attribute also defaults to true. If true ignores child processes. If you plan to call waitpid to wait for the child process to finish you should set this to false
waitpid
in
A File::DataClass::IO object reference or a simple str. Defaults to null. Determines where the standard input of the command will be redirected from. Object references should stringify to the name of the file containing input. A scalar is the input unless it is 'stdin' or 'null' which cause redirection from standard input and the null device
An array reference of file handles that are to be left open in detached children
log
A log object defaults to an instance of Class::Null. Calls are made to it at the debug level
max_pidfile_wait
Positive integer defaults to 15. The maximum number of seconds the parent process should wait for the child's PID file to appear and be populated
nap_time
Positive number defaults to 0.3. The number of seconds to wait between testing for the existence of the child's PID file
A File::DataClass::IO object reference or a simple str. Defaults to null. Determines where the standard output of the command will be redirected to. Values include;
null
Redirect to the null device as defined by File::Spec
stdout
Output is not redirected to standard output
$object_ref
The object reference should stringify to the name of a file to which standard output will be redirected
partition_cmd
Boolean default to true. If the IPC::Run implementation is selected the command array reference will be partitioned on meta character boundaries unless this attribute is set to false
pidfile
A File::DataClass::IO object reference. Defaults to a temporary file in the configuration rundir which will automatically unlink when closed
rundir
A File::DataClass::IO object reference. Defaults to the tempdir attribute. Directory in which the PID files a stored
tempdir
A File::DataClasS::IO object reference. Defaults to tmpdir from File::Spec. The directory for storing temporary files
tmpdir
timeout
Positive integer defaults to 0. If greater then zero an alarm will be raised after this many seconds if the external command has not completed
use_ipc_run
Boolean defaults to false. If true forces the use of the IPC::Rum implementation
use_system
Boolean defaults to false. If true forces the use of the system implementation
system
working_dir
A File::DataClass::IO object reference. Defaults to null. If set the child will chdir to this directory before executing the external command
chdir
BUILDARGS
$obj_ref = Class::Usul::IPC::Cmd->new( cmd => ..., out => ... ); $obj_ref = Class::Usul::IPC::Cmd->new( { cmd => ..., out => ... } ); $obj_ref = Class::Usul::IPC::Cmd->new( $cmd, out => ... ); $obj_ref = Class::Usul::IPC::Cmd->new( $cmd, { out => ... } ); $obj_ref = Class::Usul::IPC::Cmd->new( $cmd );
The constructor accepts a list of keys and values, a hash reference, the command followed by a list of keys and values, the command followed by a hash reference
BUILD
Set chomp and lock on the pidfile
$response_object = Class::Usul::IPC::Cmd->run_cmd( $cmd, @args );
Can be called as a class method or an object method
Runs a given external command. If the command argument is an array reference the internal fork and exec implementation will be used, if a string is passed the IPC::Open3 implementation will be use instead
fork
exec
Returns a Class::Ususl::Response::IPC object reference
Passing a logger object reference in with the log attribute will cause the run_cmd method to log at the debug level
There are no known incompatibilities in this module
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
Larry Wall - For the Perl programming language
MooseX::Daemonize - Stole some code from that module
Peter Flanigan, <pjfl@cpan.org>
<pjfl@cpan.org>
Copyright (c) 2016 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 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
To install Class::Usul, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Usul
CPAN shell
perl -MCPAN -e shell install Class::Usul
For more information on module installation, please visit the detailed CPAN module installation guide.