use IPC::System::Simple qw(system systemx capture capturex); system("some_command"); # Command succeeds or dies! system("some_command",@args); # Succeeds or dies, avoids shell if @args systemx("some_command",@args); # Succeeds or dies, NEVER uses the shell # Capture the output of a command (just like backticks). Dies on error. my $output = capture("some_command"); # Just like backticks in list context. Dies on error. my @output = capture("some_command"); # As above, but avoids the shell if @args is non-empty my $output = capture("some_command", @args); # As above, but NEVER invokes the shell. my $output = capturex("some_command", @args); my @output = capturex("some_command", @args);
"IPC::System::Simple" takes the hard work out of calling external commands. In fact, if you want to be really lazy, you can just write:
use IPC::System::Simple qw(system);
and all of your "system" commands will either succeed (run to completion and return a zero exit value), or die with rich diagnostic messages.
The "IPC::System::Simple" module also provides a simple replacement to Perl's backticks operator. Simply write:
use IPC::System::Simple qw(capture);
and then use the ``capture()'' command just like you'd use backticks. If there's an error, it will die with a detailed description of what went wrong. Better still, you can even use "capturex()" to run the equivalent of backticks, but without the shell:
use IPC::System::Simple qw(capturex); my $result = capturex($command, @args);
If you want more power than the basic interface, including the ability to specify which exit values are acceptable, trap errors, or process diagnostics, then read on!
use IPC::System::Simple qw( capture capturex system systemx run runx $EXITVAL EXIT_ANY ); # Run a command, throwing exception on failure run("some_command"); runx("some_command",@args); # Run a command, avoiding the shell # Do the same thing, but with the drop-in system replacement. system("some_command"); systemx("some_command", @args); # 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 = runx([0..5], "some_command", @args); # The same, but any exit value will do. my $exit_value = runx(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 = capturex([0..5], "some_command", @args);
use IPC::System::Simple qw(run); run("cat *.txt"); # Execute command via the shell run("cat","/etc/motd"); # Execute command without shell
The primary difference between Perl's in-built system and the "run" command is that "run" will throw an exception on failure, and allows a list of acceptable exit values to be set. See ``Exit values'' for further information.
In fact, you can even have "IPC::System::Simple" replace the default "system" function for your package so it has the same behaviour:
use IPC::System::Simple qw(system); system("cat *.txt"); # system now succeeds or dies!
"system" and "run" are aliases to each other.
See also ``runx(), systemx() and capturex()'' for variants of "system()" and "run()" that never invoke the shell, even with a single argument.
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");
See also ``runx(), systemx() and capturex()'' for a variant of "capture()" that never invokes the shell, even with a single argument.
systemx($cmd, @args);
The use of "systemx()" here guarantees that the shell will never be invoked, even if @args is empty.
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:
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 system capture EXIT_ANY); run( [0..5], "cat *.txt"); # Exit values 0-5 are OK system( [0..5], "cat *.txt"); # This works the same way my @lines = capture( EXIT_ANY, "cat *.txt"); # Any exit is fine.
The "run" and replacement "system" subroutines returns the exit value of the process:
my $exit_value = run( [0..5], "cat *.txt"); # OR: my $exit_value = system( [0..5] "cat *.txt"); print "Program exited with value $exit_value\n";
$EXITVAL
The exit value of any command executed by "IPC::System::Simple" can always be retrieved from the $IPC::System::Simple::EXITVAL variable:
This is particularly useful when inspecting results from "capture", which returns the captured text from the command.
use IPC::System::Simple qw(capture $EXITVAL EXIT_ANY); my @enemies_defeated = capture(EXIT_ANY, "defeat_evil", "/dev/mordor"); print "Program exited with value $EXITVAL\n";
$EXITVAL will be set to "-1" if the command did not exit normally (eg, being terminated by a signal) or did not start. In this situation an exception will also be thrown.
The "capture" subroutine always returns the 32-bit exit value under Windows. The "capture" subroutine also never uses the shell, even when passed a single argument.
The "run" subroutine always uses a shell when passed a single argument. On NT systems, it uses "cmd.exe" in the system root, and on non-NT systems it uses "command.com" in the system root.
As of "IPC::System::Simple" v1.25, the "runx" and "capturex" subroutines, as well as multiple-argument calls to the "run" and "capture" subroutines, have their arguments properly quoted, so that arugments with spaces and the like work properly. Unfortunately, this breaks any attempt to invoke the shell itself. If you really need to execute "cmd.exe" or "command.com", use the single-argument form. For single-argument calls to "run" and "capture", the argument must be properly shell-quoted in advance of the call.
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.
If you are able to reproduce this error, you are encouraged to submit a bug report according to the ``Reporting bugs'' section below.
There are no non-core dependencies on non-Win32 systems.
If an odd exit status is provided, you're informed of what it is. If a signal kills your process, you are informed of both its name and number. If tainted data or environment prevents your command from running, you are informed of exactly which data or environmental variable is tainted.
Core dumps are only checked for when a process dies due to a signal. It is not believed there are any systems where processes can dump core without dying to a signal.
"WIFSTOPPED" status is not checked, as perl never spawns processes with the "WUNTRACED" option.
Signals are not supported under Win32 systems, since they don't work at all like Unix signals. Win32 signals cause commands to exit with a given exit value, which this modules does capture.
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 expedite the fixing of bugs.
The module author loves to hear how "IPC::System::Simple" has made your life better (or worse). Feedback can be sent to <pjf@perltraining.com.au>.
POSIX, IPC::Run::Simple, perlipc, perlport, IPC::Run, IPC::Run3, Win32::Process
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.