是否可以在qx内调用函数?

时间:2014-06-18 12:29:11

标签: perl qx

这里有一些符合我想要的Perl代码:

 my $value  = get_value();
 my $result = qx(some-shell-command $value);

 sub get_value {
   ...
   return ...
 }

是否可以在不使用$value的情况下实现相同的效果?像

这样的东西
my $result = qx (some-shell-command . ' '. get_value());

我知道为什么第二种方法不起作用,只是为了证明这个想法。

2 个答案:

答案 0 :(得分:6)

my $result = qx(some-shell-command  @{[ get_value() ]});

# or dereferencing single scalar value 
# (last one from get_value if it returns more than one)
my $result = qx(some-shell-command  ${ \get_value() });

但我宁愿使用你的第一个选项。

说明: perl数组在""qx()等内插。

上面是数组引用[]保存函数的结果,被@{}取消引用,并在qx()内插入。

答案 1 :(得分:2)

反引号和qx等同于内置readpipe函数,因此您可以明确地使用它:

$result = readpipe("some-shell-command " . get_value());
相关问题