I want to supress output in child process and read only stderr. perlfaq8 advises to do following:
# To capture a program's STDERR, but discard its STDOUT:
use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);
But then perlcritic
argues on using bareword file handles.
The only thing i can devise is to select
newly opened descriptor to /dev/null
instead on STDOUT
, like this:
# To capture a program's STDERR, but discard its STDOUT:
use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open my $null, ">", File::Spec->devnull;
my $old_stdout = select( $null );
my $pid = open3(gensym, ">&STDOUT", \*PH, "cmd");
select( $old_stdout );
while( <PH> ) { }
waitpid($pid, 0);
But then perlcritic
doesn't like using of select
.
Is there more elegant solution?
select
doesn't actually do anything!select
doesn't changeSTDOUT
. – Indwell