Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can IPC::System::Simple capture STDERR?

I would like to invoke a script from my Perl code and capture its STDERR and STDOUT combined together.

I usually use capture from IPC::System::Simple but ti doesn't seem to allow capturing of STDERR.

like image 229
David B Avatar asked Feb 16 '26 03:02

David B


2 Answers

You can redirect STDERR (file descriptor 2) to STDOUT (file descriptor 1) with 2>&1.

From perlop on the qx// operator:

Because backticks do not affect standard error, use shell file descriptor syntax (assuming the shell supports this) if you care to address this. To capture a command's STDERR and STDOUT together:

$output = `cmd 2>&1`;

like image 149
Eugene Yarmash Avatar answered Feb 21 '26 12:02

Eugene Yarmash


On a POSIX system, you can do the following. On Windows, this would work in cygwin.

my @lines = capture("some command 2>&1");

However, if you want to distinguish STDERR lines from STDOUT lines, maybe you need to use IPC::Open3 or the mis-named IPC::Open3::Util.

like image 23
Leolo Avatar answered Feb 21 '26 14:02

Leolo