gpt4 book ai didi

perl - 如何模拟 Perl 的内置反引号运算符?

转载 作者:行者123 更新时间:2023-12-04 01:50:11 24 4
gpt4 key购买 nike

我想对我使用反引号的 Perl 程序进行单元测试。有没有办法模拟反引号,以便它们执行与执行外部命令不同的操作?

Another question shows what I need ,但在 Ruby 中。不幸的是,我不能选择在这个项目中使用 Ruby,也不想避免使用反引号。

最佳答案

您可以*模拟内置 readpipe 功能。 Perl 会在遇到反引号或 qx 时调用您的模拟函数。表达。

BEGIN {
*CORE::GLOBAL::readpipe = \&mock_readpipe
};

sub mock_readpipe {
wantarray ? ("foo\n") : "foo\n";
}

print readpipe("ls -R");
print `ls -R`;
print qx(ls -R);
$ perl mock-readpipe.pl
foo
foo
foo

* - 如果您有 perl version 5.8.9或以后。

关于perl - 如何模拟 Perl 的内置反引号运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3678655/

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