Open (HANDLE, "- |", $ cmd, @args): how to read stderr?

I want to read the output of a command, including stderr, in HANDLE:

open(HANDLE, "-|", $cmd, @args); 

But the above command just reads stdin?

How can I also read stderr?

+4
source share
3 answers

The IPC :: Run module provides a run function that works like a supercharged system . This allows us to collect the output of STDERR and STDOUT in combination:

 run [$cmd, @args], '&>', \my $output; 

after that, the variable $output contains the combined output as a string.

Example:

 use IPC::Run qw/ run /; run ['perl', '-E say "stdout"; say STDERR "stderr"'], '&>', \my $output; print uc $output; 

Output:

 STDOUT STDERR 

I don’t know how to use a file descriptor instead of a scalar link so that I can read the output normally in a while(<$fh>) .

+2
source

You need to look at IPC :: Open3 , which starts the process and provides separate files for writing to the child, and reading the child STDOUT and STDERR.

0
source

I am using Bash redirection as shown below in my perl code:

 open (CMDOUT, "df -h 2>&1 |"); 
-1
source

Source: https://habr.com/ru/post/1486246/


All Articles