ActiveState::Run - Collection of small utility functions
use ActiveState::Run qw(run); run("ls -l");
This module provides a collection of small utility functions for running external programs.
The following functions are provided:
Will decode the given return code (defaults to $?) and return the exit value, the signal it was killed with, and if it dumped core.
In scalar context, it will return a string explaining what happened, or an empty string if no error occured.
my $foo = `ls`; my $err = decode_status; die "ls failed: $err" if $err;
In array context, it will return a list of key/value pairs containing:
True when the status code indicates normal termination.
If WIFEXITED, this will contain the low-order 8 bits of the status value the child passed to exit or returned from main.
Non-zero if process was terminated by a signal.
If WIFSIGNALED, the terminating signal.
Non-zero if the process was stopped.
If WIFSTOPPED, the signal that stopped the process.
Nonzero if the process dumped core.
Example:
my $foo = `ls`; my %err = decode_status; die "ls dumped core" if $err{WCOREDUMP};
Works like the builtin system()
but will by default print commands to
stdout before it execute them and raise an exception (die) if the
command fails (returns non-zero status). Like for the command
specifications for make(1)
, you can prefix the command with "@" to
suppress the echo and with "-" to suppress the status check.
The environment variables AS_RUN_SILENT and AS_RUN_PREFIX influence printing as well, see ENVIRONMENT.
The extended version of the run function with many additional ways to
control the how the command runs, but otherwise it acts as run()
.
The following options are recognized:
Specify the command line to run. It does not support the '@' and '-'
prefixes that the run()
command allow. This option is not optional :)
Only use this if you want to override what executable actually runs. Can be used to lie about what program actually runs, as this allow argv[0] to be different than the actual command.
Make this the current directory of the process. By default, the process shares the parent's current directory.
Override the environment for the process.
List of environment variables that will not be passed to the kid. You
might pass this as [keys %ENV]
if you want the process to start
out with an environment that only consist of what you passed in with the
env
option.
Make the new process run with stdin from the given filehandle. If not
given the process will be started with /dev/null as its input. If
you want the process to inherit the input of the parent you need to
pass *STDIN
explictly.
Redirect the combined STDOUT and STDERR to the given file. The command will croak if the file can't be opened. If not specified, then the STDOUT and STDERR of the process is simply inherited from the parent.
If TRUE don't echo commands as they are executed.
If TRUE don't croak if the command exits with a non-zero status.
Be nice!
If TRUE send the output captured in the output
file to the current STDOUT
as well. No effect unless output
is specified.
If TRUE start a new process group for the process.
Kill the process (or the process group if new_group
was specified)
if it runs for longer than the specified number of seconds.
Kill the process (or the process group if new_group
was specified)
if it output file grows bigger than the specified number of mega
bytes. No effect unless output
was specified.
Other limits might also be passed which will set how much resources the process
is allowed to use. The unit for all size limits are megabytes. See
BSD::Resource
for allowed values. Also consult the ulimit
command in you
shell.
Will quote the arguments provided so that they can be passed to the
command shell without interpretation by the shell. This is useful
with run()
when you can't provide separate @args, e.g.:
run(shell_quote("rm", "-f", @files) . " >dev/null");
In list context it returns the same number of values as arguments passed in. Only those arg values that need quoting will be quoted.
In scalar context it will return a single string with all the quoted @args separated by space.
In void context it will attempt inline modification of the @args passed.
If the AS_RUN_SILENT environment variable is TRUE, then printing of
the command about to run for run()
is suppressed.
If the AS_RUN_PREFIX environment variable is set, then the printed command is prefixed with the given string. If AS_RUN_SILENT is TRUE, then this value is ignored.
none.