我想抑制子进程中的输出并只读stderr。perlfaq8建议执行以下操作:
# 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);
但随后perlcritic
争论使用裸字文件句柄。
我唯一能设计的是将select
新打开的描述符/dev/null
改为 on STDOUT
,如下所示:
# 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);
但是perlcritic
不喜欢使用 ofselect
。有更优雅的解决方案吗?