Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl readpipe处理程序中展开变量?_Perl_Unit Testing_Backticks - Fatal编程技术网

如何在Perl readpipe处理程序中展开变量?

如何在Perl readpipe处理程序中展开变量?,perl,unit-testing,backticks,Perl,Unit Testing,Backticks,似乎backticks中的变量在传递到readpipe函数时没有展开。如果重写readpipe函数,如何展开变量 BEGIN { *CORE::GLOBAL::readpipe = sub {print "Run:@_\n"}; } `ls /root`; my $dir = "/var"; `ls $dir`; 运行此命令将提供: Run:ls /root Run:ls $dir 我试图模拟我正在编写的测试代码的外部调用。如果某个地方有一个CPAN模块可以帮助解决所有这些问题,那也会有帮

似乎backticks中的变量在传递到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()
而不是backticks可以正确地扩展变量。我在运行测试之前使用了一个自动脚本清理器,它在运行测试之前将所有反勾号转换为
readpipe()

e、 g运行:

$ 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中的一个bug。使用来报告它。

您可能想改用它

或者,如果您不想捕获输出,
system()
可能更好

system( "ls", $dir ) == 0 or die "Cannot system - $!";

感谢模块ref。不过,我正在寻找一种简单的方法模拟系统调用,如backticks和system(),以运行遗留代码的单元测试。@SandipBhattacharya我不确定我是否理解这个问题。当系统返回任何输出时,backticks和exec不返回任何输出。eval和qx符合要求吗?@shinronin我正在尝试覆盖/模拟现有遗留代码中的反勾号,以便安全地测试它们。虽然我可以将
*CORE::GLOBAL::readpipe
替换为模拟反勾号,但在参数包含要展开的变量的情况下,它们不起作用。由于backticks和
readpipe()
之间的这种不一致行为,很难模拟这样的代码。我不想写新代码。如果我这样做了,我会使用
readpipe()
而不是backticks,这样如果我愿意,我就可以模拟代码了。@shinronin:这部分是错的;backticks确实提供输出,而system()是静默的,exec无法返回输出(因为调用它的进程被替换)。@jeffbragg oops,true。我永远都不能让他们保持正直。perldoc-f是一个恒定的伴侣
system( "ls", $dir ) == 0 or die "Cannot system - $!";