NAME

IPC::System::Simple - Run commands simply, with detailed diagnostics

SYNOPSIS

use IPC::System::Simple qw(capture run $EXITVAL EXIT_ANY);

# Run a command, throwing exception on failure

run("some_command");

run("some_command",@args);  # Run a command, avoiding the shell

# Run a command which must return 0..5, avoid the shell, and get the
# exit value (we could also look at $EXITVAL)

my $exit_value = run([0..5], "some_command", @args);

# The same, but any exit value will do.

my $exit_value = run(EXIT_ANY, "some_command", @args);

# Capture output into $result and throw exception on failure

my $result = capture("some_command");	

# Check exit value from captured command

print "some_command exited with status $EXITVAL\n";

# Captures into @lines, splitting on $/
my @lines = capture("some_command"); 

# Run a command which must return 0..5, capture the output into
# @lines, and avoid the shell.

my @lines  = capture([0..5], "some_command", @args);

DESCRIPTION

Calling Perl's in-built system() function is easy, but checking the results can be hard. IPC::System::Simple aims to make life easy for the common cases of calling system and backticks (aka qx()).

run

IPC::System::Simple provides a subroutine called run, that executes a command using the same semantics is Perl's built-in system:

use IPC::System::Simple qw(run);

run("cat *.txt");	# Execute command via the shell
run("cat","/etc/motd");	# Execute command without shell

capture

A second subroutine, named capture executes a command with the same semantics as Perl's built-in backticks (and qx()):

use IPC::System::Simple qw(capture);

# Capture text while invoking the shell.
my $file  = capture("cat /etc/motd");
my @lines = capture("cat /etc/passwd");

However unlike regular backticks, which always use the shell, capture will bypass the shell when called with multiple arguments:

# Capture text while avoiding the shell.
my $file  = capture("cat", "/etc/motd");
my @lines = capture("cat", "/etc/passwd");

Exception handling

In the case where the command returns an unexpected status, both run and capture will throw an exception, which if not caught will terminate your program with an error.

Capturing the exception is easy:

eval {
	run("cat *.txt");
};

if ($@) {
	print "Something went wrong - $@\n";
}

See the diagnostics section below for more details.

Exception cases

IPC::System::Simple considers the following to be unexpected, and worthy of exception:

  • Failing to start entirely (eg, command not found, permission denied).

  • Returning an exit value other than zero (but see below).

  • Being killed by a signal.

  • Being passed tainted data (in taint mode).

Exit values

Traditionally, system commands return a zero status for success and a non-zero status for failure. IPC::System::Simple will default to throwing an exception if a non-zero exit value is returned.

You may specify a range of values which are considered acceptable exit values by passing an array reference as the first argument. The special constant EXIT_ANY can be used to allow any exit value to be returned.

use IPC::System::Simple qw(run capture EXIT_ANY);

run( [0..5], "cat *.txt");             # Exit values 0-5 are OK

my @lines = capture( EXIT_ANY, "cat *.txt"); # Any exit is fine.

The run subroutine returns the exit value of the process:

my $exit_value = run( [0..5], "cat *.txt");

print "Program exited with value $exit_value\n";

$EXITVAL

The exit value of a command executed with either run or capture can always be retrieved from the $IPC::System::Simple::EXITVAL variable:

use IPC::System::Simple qw(capture $EXITVAL);

my @lines = capture("cat", "/etc/passwd");

print "Program exited with value $EXITVAL\n";

This is particularly useful when inspecting results from capture, which returns the captured text from the command.

$EXITVAL will be set to -1 if the command did not exit normally (eg, being terminated by a signal) or did not start.

WINDOWS-SPECIFIC NOTES

As of IPC::System::Simple v0.06, the run subroutine when called with multiple arguments will make available the full 16-bit exit value on Win32 systems. This is different from the previous versions of IPC::System::Simple and from Perl's in-build system() function, which can only handle 8-bit return values.

The capture subroutine always returns the 16-bit exit value under Windows. The capture subroutine also never uses the shell, even when passed a single argument.

Versions of IPC::System::Simple before v0.09 would not search the PATH environment variable when the multi-argument form of run() was called. Versions from v0.09 onwards correctly search the path provided the command is provided including the extension (eg, notepad.exe rather than just notepad, or gvim.bat rather than just gvim). If no extension is provided, .exe is assumed.

Signals are not supported on Windows systems. Sending a signal to a Windows process will usually cause it to exit with the signal number used.

DIAGNOSTICS

"%s" failed to start: "%s"

The command specified did not even start. It may not exist, or you may not have permission to use it. The reason it could not start (as determined from $!) will be provided.

"%s" unexpectedly returned exit value %d

The command ran successfully, but returned an exit value we did not expect. The value returned is reported.

"%s" died to signal "%s" (%d) %s

The command was killed by a signal. The name of the signal will be reported, or UNKNOWN if it cannot be determined. The signal number is always reported. If we detected that the process dumped core, then the string and dumped core is appeneded.

IPC::System::Simple::%s called with no arguments

You attempted to call run or capture but did not provide any arguments at all. At the very lease you need to supply a command to run.

IPC::System::Simple::%s called with no command

You called run or capture with a list of acceptable exit values, but no actual command.

IPC::System::Simple::%s called with tainted argument "%s"

You called run or capture with tainted (untrusted) arguments, which is almost certainly a bad idea. To untaint your arguments you'll need to pass your data through a regular expression and use the resulting match variables. See "Laundering and Detecting Tainted Data" in perlsec for more information.

IPC::System::Simple::%s called with tainted environment $ENV{%s}

You called run or capture but part of your environment was tainted (untrusted). You should either delete the named environment variable before calling run, or set it to an untainted value (usually one set inside your program). See "Cleaning Up Your Path" in perlsec for more information.

Error in IPC::System::Simple plumbing: "%s" - "%s"

Implementing the capture command involves dark and terrible magicks involving pipes, and one of them has sprung a leak. This could be due to a lack of file descriptors, although there are other possibilities.

If you are able to reproduce this error, you are encouraged to submit a bug report according to the "Reporting bugs" section below.

Internal error in IPC::System::Simple: "%s"

You've found a bug in IPC::System::Simple. Please check to see if an updated version of IPC::System::Simple is available. If not, please file a bug report according to the "Reporting bugs" section below.

DEPENDENCIES

This module depends upon Win32::Process when used on Win32 system. Win32::Process is bundled as a core module in ActivePerl 5.6 and above.

There are no non-core dependencies on non-Win32 systems.

BUGS

Core dumps are only checked for when a process dies due to a signal.

WIFSTOPPED status is not checked, as perl never spawns processes with the WUNTRACED option.

Signals are not supported under Win32 systems.

16-bit exit values are provided when run() is called with multiple arguments under Windows, but only 8-bit values are returned when run() is called with a single value. We should always return 16-bit value on systems that support them.

Reporting bugs

Before reporting a bug, please check to ensure you are using the most recent version of IPC::System::Simple. Your problem may have already been fixed in a new release.

You can find the IPC::System::Simple bug-tracker at http://rt.cpan.org/Public/Dist/Display.html?Name=IPC-System-Simple . Please check to see if your bug has already been reported; if in doubt, report yours anyway.

Submitting a patch and/or failing test case will greatly expediate the fixing of bugs.

SEE ALSO

POSIX, IPC::Run::Simple, perlipc, perlport, IPC::Run, IPC::Run3, Win32::Process

AUTHOR

Paul Fenwick <pjf@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2006-2008 by Paul Fenwick

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.6.0 or, at your option, any later version of Perl 5 you may have available.