您不需要一直到open3
,这个IIRC只适用于需要读取和写入外部命令时,甚至还有其他方法。
对于你的问题,我建议使用Capture::Tiny
,它可以捕捉(或甚至发球)的STDOUT和STDERR从其块内运行的任何东西。例如,根据您的问题:
#!/usr/bin/env perl
use strict;
use warnings;
use Capture::Tiny qw/capture/;
...
my ($stdout, $stderr) = capture {
system ("snmpwalk -v $version -c $community $hostname $oid");
};
再比如考虑这个功能代码:
#!/usr/bin/env perl
use strict;
use warnings;
use Capture::Tiny qw/capture/;
my ($stdout, $stderr) = capture {
system ("echo 'hello'");
system ("date");
warn "Arg1!";
};
print "STDOUT:\n$stdout";
print "STDERR:\n$stderr";
这只是给了我:
STDOUT:
hello
Mon Dec 19 23:59:06 CST 2011
STDERR:
Arg1! at ./test.pl line 11.
你可能想看看[网-SNMP](http://search.cpan.org/~dtown/Net-SNMP-v6.0.1/)。这样你就可以完全控制错误,不用担心重定向输出。 –