如何在Perl readpipe处理程序中扩展变量? [英] How do I expand variables in Perl readpipe handlers?

查看:131
本文介绍了如何在Perl readpipe处理程序中扩展变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在反引号中的变量传递给readpipe函数时不会扩展。如果重写了readpipe函数,如何扩展变量?

It seems that variables in backticks are not expanded when passed onto the readpipe function. If I override the readpipe function, how do I expand variables?

BEGIN {
*CORE::GLOBAL::readpipe = sub {print "Run:@_\n"};
}

`ls /root`;
my $dir = "/var";
`ls $dir`;

运行此命令可获得:

Run:ls /root
Run:ls $dir

我正在尝试模拟对正在编写的测试代码的外部调用。如果某个地方有CPAN模块可以帮助您解决所有这些问题,那也将有所帮助。

I am trying to mock external calls for a test code that I am writing. If there is a CPAN module somewhere which can help in taking care of all this, that would help too.

更新

我已决定对我的问题使用一种非常丑陋的解决方法。事实证明,使用 readpipe()代替反引号可以正确扩展变量。在运行测试之前,我正在使用自动脚本清洁程序,该脚本会在运行测试之前将所有反引号转换为 readpipe()

I have decided to use a really ugly workaround to my problem. It turns out that using readpipe() instead of backticks expands variables correctly. I am using an automatic script cleaner before running my tests which converts all backticks to readpipe() before running the tests.

例如跑步:

$ 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

尽管如此,我仍在寻找更清洁的解决方案。

I am still looking out for a cleaner solution though.

推荐答案

这似乎是Perl中的错误。使用 perlbug 进行报告。

That appears to be a bug in Perl. Use perlbug to report it.

这篇关于如何在Perl readpipe处理程序中扩展变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆