perlperl5.8

Mocking backticks operator in perl 5.8.8


I am trying to mock the backticks operator in perl version 5.8.8 . From what i have understood is, it is not possible to mock it in perl version 5.8.8. But in perl version 5.9 onward, i can easily mock the backticks operator using

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

Is there way to mock backticks operator in perl version 5.8.8. I am able to mock system(), but not backticks.


Solution

  • 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 ``.

    Read complete discussion on perlmonks: mocking or trapping system calls

    Also check out IPC::System::Simple.