如何重定向库的输出';perl中的s函数?
我试图重定向库函数的输出,而不更改库中的代码: program.pl如何重定向库的输出';perl中的s函数?,perl,redirect,output,Perl,Redirect,Output,我试图重定向库函数的输出,而不更改库中的代码: program.pl use Lib::xxLib1xx; ... xxLib1xx::Function1($Arg1); xxLib1xx.pm Function1{ my $arg = shift; print "$arg\n"; } 如何修改program.pl中的代码,以便在调用Function1时看不到输出?我无法更改库本身中的代码。 如果我进行系统调用,它看起来会像: system("echo hello >
use Lib::xxLib1xx;
...
xxLib1xx::Function1($Arg1);
xxLib1xx.pm
Function1{
my $arg = shift;
print "$arg\n";
}
如何修改program.pl中的代码,以便在调用Function1时看不到输出?我无法更改库本身中的代码。
如果我进行系统调用,它看起来会像:
system("echo hello > nul");
退房
不使用CPAN模块的答案仍然非常简洁:
my $stdout;
{
local *STDOUT;
open STDOUT, ">", \$stdout;
xxLib1xx::Function1($Arg1);
}
print "Got '$stdout' from subroutine call!\n";
这是一个很好的回答
希望能有所帮助。通过shooper的回答得到帮助:
my $LOG;
open ($LOG, '>>', 'null');
select $LOG;
...
xxLib1xx::Function1($Arg1);
...
select STDOUT;
是否显式调用Function1?如果是,请复制:。
my $LOG;
open ($LOG, '>>', 'null');
select $LOG;
...
xxLib1xx::Function1($Arg1);
...
select STDOUT;