Getting STDOUT, STDERR, and response code from external *nix command in perl

This was exactly the challenge that David Golden faced when he wrote Capture::Tiny. I think it will help you do exactly what you need.

Basic example:

#!/usr/bin/env perl

use strict;
use warnings;

use Capture::Tiny 'capture';

my ($stdout, $stderr, $return) = capture {
  system( 'echo Hello' );
};

print "STDOUT: $stdout\n";
print "STDERR: $stderr\n";
print "Return: $return\n";

After rereading you might actually want capture_merged to join STDOUT and STDERR into one variable, but the example I gave is nice and general, so I will leave it.

Leave a Comment