gpt4 book ai didi

perl - 如何在 perl 中重定向库函数的输出?

转载 作者:行者123 更新时间:2023-12-02 08:30:54 24 4
gpt4 key购买 nike

我试图在不更改库中的代码的情况下重定向库函数的输出:

程序.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";

关于perl - 如何在 perl 中重定向库函数的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26982164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com