Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to execute an external command from within my Perl script, putting the output of both stdout and stderr into a $variable of my choice, and to get the command's exit code into the $? variable.

I went through solutions in perlfaq8 and their forums, but they're not working for me. The strange thing is that I don't get the output of sdterr in any case, as long as the exit code is correct.

I'm using Perl version 5.8.8, on Red Hat Linux 5.

Here's an example of what I'm trying:

my $cmd="less";
my $out=`$cmd 2>&1`;

or

my $out=qx($cmd 2>&1);

or

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

When the command runs successfully, I can capture stdout.

I don't want to use additional capture modules. How can I capture the full results of the external command?

like image 866
Gregory Danenberg Avatar asked Jan 04 '12 19:01

Gregory Danenberg


3 Answers

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.

like image 190
Joel Berger Avatar answered Sep 21 '22 17:09

Joel Berger


Actually, the proper way to write this is:

#!/usr/bin/perl
$cmd = 'lsss';  
my $out=qx($cmd 2>&1);
my $r_c=$?;
print "output was $out\n";
print "return code = ", $r_c, "\n";

You will get a '0' if no error and '-1' if error.

like image 24
Radix Avatar answered Sep 20 '22 17:09

Radix


STDERR is intended to be used for errors or messages that might need to be separated from the STDOUT output stream. Hence, I would not expect any STDERR from the output of a command like less.

If you want both (or either) stream and the return code, you could do:

my $out=qx($cmd 2>&1);
my $r_c=$?
print "output was $out\n";
print "return code = ", $r_c == -1 ? $r_c : $r_c>>8, "\n";

If the command isn't executable (perhaps because you meant to use less but wrote lsss instead), the return code will be -1. Otherwise, the correct exit value is the high 8-bits. See system.

like image 41
JRFerguson Avatar answered Sep 18 '22 17:09

JRFerguson