qx

How do I mock Perl's built-in backticks operator?

我是研究僧i 提交于 2019-12-30 04:06:09
问题 I'd like to unit test a Perl program of mine that is using backticks. Is there a way to mock the backticks so that they would do something different from executing the external command? Another question shows what I need, but in Ruby. Unfortunately, I cannot choose to use Ruby for this project, nor do I want to avoid the backticks. 回答1: You can * mock the built-in readpipe function. Perl will call your mock function when it encounters a backticks or qx expression. BEGIN { *CORE::GLOBAL:

How do I mock Perl's built-in backticks operator?

核能气质少年 提交于 2019-11-30 12:45:12
I'd like to unit test a Perl program of mine that is using backticks. Is there a way to mock the backticks so that they would do something different from executing the external command? Another question shows what I need , but in Ruby. Unfortunately, I cannot choose to use Ruby for this project, nor do I want to avoid the backticks. You can * mock the built-in readpipe function. Perl will call your mock function when it encounters a backticks or qx expression. BEGIN { *CORE::GLOBAL::readpipe = \&mock_readpipe }; sub mock_readpipe { wantarray ? ("foo\n") : "foo\n"; } print readpipe("ls -R");