perl - 在 perl 5.8.8 中模拟反引号运算符

标签 perl perl5.8

我正在尝试模拟 perl 版本 5.8.8 中的反引号运算符。据我所知,不可能在 perl 5.8.8 版中模拟它。但是在 perl 5.9 以后的版本中,我可以使用

轻松地模拟反引号运算符

*CORE::GLOBAL::readpipe =\&mock_readpipe

在 perl 5.8.8 版中是否有模拟反引号运算符的方法。我可以模拟 system(),但不能模拟反引号。

最佳答案

You can override system() and readpipe(), as they are second-class (overridable) keywords. In Perl 5.8, you can't override qx// or ``, even though they use the same underlying code as readpipe(), simply because they are first-class (non-overridable) keywords. See perl_keywords.pl and opcode.pl in the Perl source code. Why are some keywords not overridable? The main reason is that those keywords are used as part of some further parsing magic, i.e. they don't follow the usual function call style parsing.

The good news is that change #29168 to perl made qx// overridable. Hooray! That was released in Perl 5.9.5, and will eventually make it to a maintenance release as Perl 5.10.1. When that happens, setting *CORE::GLOBAL::readpipe will override readpipe(), qx// and ``.

阅读关于 perlmonks 的完整讨论:mocking or trapping system calls

另请查看 IPC::System::Simple .

关于perl - 在 perl 5.8.8 中模拟反引号运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471994/

相关文章:

perl - 如何将结果存储到变量并在条件中检查结果?

perl - Perl 5.8 与 5.10 和 5.12 的子例程中的代码延迟评估有什么区别?

perl - 在 Perl 中没有更简单的多行注释的原因是什么?

xml - perl LibXML 编写XML文档

linux - 从 perl 脚本打印的 bash 终端中的回车\r 导致卡住

perl - Inline::CPP - 如何将指针 (int*) 作为 C 函数的参数处理

perl - 在 perl 中使用变量作为目录名

regex - 在 Perl 正则表达式中匹配平衡括号