问题描述
我正在使用IPC :: Open3编写一个简单的脚本.该脚本不会向stdout或stderr产生任何输出,而我希望两者都输出.
I am writing a simple script using IPC::Open3. The script produces no output to either stdout or stderr, while I would expect output to both.
完整的源代码:
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use IPC::Open3;
pipe my $input, my $output or die $!;
my $pid = open3(\*STDIN, $output, \*STDERR, 'dd', 'if=/usr/include/unistd.h') or die $!;
while(<$input>) {
print $_."\n";
}
waitpid $pid, 0;
我可以肯定我使用的IPC :: Open3错误.但是,我仍然对应该做的事情感到困惑.
I am fairly certain that I am using IPC::Open3 incorrectly. However, I am still confused as to what I should be doing.
推荐答案
它是pipe
.不知道为什么会在那里,我不能说更多.效果很好.
It's the pipe
. Without knowing why it's there I can't say more. This works fine.
my $reader;
my $pid = open3(\*STDIN, $reader, \*STDERR, 'dd', 'if=/usr/include/unistd.h') or die $!;
while(<$reader>) {
print $_."\n";
}
waitpid $pid, 0;
我意识到这可能只是一个例子,但万一不是……这完全是您正在做的事.您可以使用反引号来实现.
I realize it's probably just an example, but in case it's not... this is complete overkill for what you're doing. You can accomplish that with backticks.
print `dd if=/usr/include/unistd.h`
IPC :: Open3有点复杂.还有更好的模块,例如 IPC :: Run 和 IPC :: Run3 .
use strict;
use warnings;
use IPC::Run3;
run3(['perl', '-e', 'warn "Warning!"; print "Normal\n";'],
\*STDIN, \*STDOUT, \*STDERR
);
这篇关于IPC :: Open3遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!