传递给readpipe函数时,反引号中的变量似乎不会被展开.如果我覆盖readpipe函数,我该如何扩展变量?
BEGIN {
*CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}
`ls /root`;
my $dir = "/var";
`ls $dir`;
Run Code Online (Sandbox Code Playgroud)
运行这个给出:
Run:ls /root
Run:ls $dir
Run Code Online (Sandbox Code Playgroud)
我正在尝试模拟外部调用我正在编写的测试代码.如果在某个地方有一个可以帮助处理所有这些的CPAN模块,那也会有所帮助.
更新:
我决定使用一个非常难看的解决方法来解决我的问题.事实证明,使用readpipe()
而不是反引号可以正确扩展变量.我在运行测试之前使用自动脚本清理程序,在运行测试之前将所有反引号转换为readpipe()
.
例如跑步:
$ cat t.pl
BEGIN {
*CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}
`ls /root`;
my $dir = "/var";
`ls $dir`;
readpipe("ls $dir");
Run Code Online (Sandbox Code Playgroud)
得到:
$ perl t.pl
Run:ls /root
Run:ls $dir
Run:ls /var
Run Code Online (Sandbox Code Playgroud)
我仍在寻找更清洁的解决方案.