Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何重定向库的输出';perl中的s函数?_Perl_Redirect_Output - Fatal编程技术网

如何重定向库的输出';perl中的s函数?

如何重定向库的输出';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 >

我试图重定向库函数的输出,而不更改库中的代码:

program.pl

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;