在 perl 5.8.8 中模拟反引号运算符
Mocking backticks operator in perl 5.8.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 版本 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。