views:

12415

answers:

5

Is it possible to run an external process from Perl, capture its stderr, stdout AND the process exit code?

I seem to be able to do combinations of these, e.g. use backticks to get stdout, IPC::Open3 to capture outputs, and system() to get exit codes.

How do you capture stderr, stdout, and the exit code all at once?

+15  A: 

If you reread the documentation for IPC::Open3, you'll see a note that you should call waitpid to reap the child process. Once you do this, the status should be available in $?. The exit value is $? >> 8. See $? in perldoc perlvar.

Michael Carman
I don't know why the links are screwed up. Everything looks fine in edit/preview.
Michael Carman
I've sent perl5porters a patch to IPC::Open2 and ::Open3 to show the waitpid stuff in the SYNOPSIS of those modules. :)
brian d foy
A: 

There are three basic ways of running external commands:

system $cmd;  # using system()
$output = `$cmd`;       # using backticks (``)
open (PIPE, "cmd |"); # using open()

With system(), both STDOUT and STDERR will go the same place as the script's STDOUT and STDERR, unless the system() command redirects them. Backticks and open() read only the STDOUT of your command.

You could also call something like the following with open to redirect both STDOUT and STDERR.

open(PIPE, "cmd 2>&1 |");

The return code is always stored in $? as noted by @Michael Carman.

hoyhoy
+10  A: 

(Update: I updated the API for IO::CaptureOutput to make this even easier.)

There are several ways to do this. Here's one option, using the IO::CaptureOutput module:

use IO::CaptureOutput qw/capture_exec/;

my ($stdout, $stderr, $success, $exit_code) = capture_exec( @cmd );

This is the capture_exec() function, but IO::CaptureOutput also has a more general capture() function that can be used to capture either Perl output or output from external programs. So if some Perl module happens to use some external program, you still get the output.

It also means you only need to remember one single approach to capturing STDOUT and STDERR (or merging them) instead of using IPC::Open3 for external programs and other modules for capturing Perl output.

xdg
+7  A: 

If you don't want the contents of STDERR, then the capture() command from IPC::System::Simple module is almost exactly what you're after:

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

   my $output = capture($cmd, @args);

   my $exit_value = $EXITVAL;

You can use capture() with a single argument to invoke the shell, or multiple arguments to reliably avoid the shell. There's also capturex() which never calls the shell, even with a single argument.

Unlike Perl's built-in system and backticks commands, IPC::System::Simple returns the full 32-bit exit value under Windows. It also throws a detailed exception if the command can't be started, dies to a signal, or returns an unexpected exit value. This means for many programs, rather than checking the exit values yourself, you can rely upon IPC::System::Simple to do the hard work for you:

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

 system( [0,1], "frobincate", @files);     # Must return exitval 0 or 1

 my @lines = capture($EXIT_ANY, "baznicate", @files);  # Any exitval is OK.

 foreach my $record (@lines) {
     system( [0, 32], "barnicate", $record);  # Must return exitval 0 or 32
 }

IPC::System::Simple is pure Perl, has no dependencies, and works on both Unix and Windows systems. Unfortunately, it doesn't provide a way of capturing STDERR, so it may not be suitable for all your needs.

IPC::Run3 provides a clean and easy interface into re-plumbing all three common filehandles, but unfortunately it doesn't check to see if the command was successful, so you'll need to inspect $? manually, which is not at all fun. Providing a public interface for inspecting $? is something which is on my to-do list for IPC::System::Simple, since inspecting $? in a cross-platform fashion is not a task I'd wish on anyone.

There are other modules in the IPC:: namespace that may also provide you with assistance. YMMV.

All the best,

Paul

pjf
Technically, it does have a dependency, but only on Windows, where it requires Win32::Process. While this is not in core Perl, it is bundled with the ActiveState Perl and Strawberry Perl distributions.
xdg
+1, I use IPC::System::Simple all the time.
Ether
A: 

If you're getting really complicated, you might want to try Expect.pm. But that's probably overkill if you don't need to also manage sending input to the process as well.

skiphoppy