【发布时间】:2012-06-14 06:32:35
【问题描述】:
似乎反引号中的变量在传递给 readpipe 函数时没有展开。如果我重写了 readpipe 函数,如何扩展变量?
BEGIN {
*CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}
`ls /root`;
my $dir = "/var";
`ls $dir`;
运行此命令:
Run:ls /root
Run:ls $dir
我正在尝试模拟对我正在编写的测试代码的外部调用。如果某个地方有一个 CPAN 模块可以帮助解决所有这些问题,那也会有所帮助。
更新:
我决定使用一个非常丑陋的解决方法来解决我的问题。事实证明,使用 readpipe() 而不是反引号可以正确扩展变量。我在运行测试之前使用了一个自动脚本清理器,它在运行测试之前将所有反引号转换为readpipe()。
例如跑步:
$ cat t.pl
BEGIN {
*CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}
`ls /root`;
my $dir = "/var";
`ls $dir`;
readpipe("ls $dir");
给予:
$ perl t.pl
Run:ls /root
Run:ls $dir
Run:ls /var
不过,我仍在寻找更清洁的解决方案。
【问题讨论】:
标签: perl unit-testing backticks