Is it possible to call a function within qx?

252 Views Asked by At

Here's a bit of Perl code that does what I want:

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

 sub get_value {
   ...
   return ...
 }

Is it possible to achieve the same effect without using $value? Something like

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

I know why the second approach doesn't work, it's just to demonstrate the idea.

2

There are 2 best solutions below

5
On BEST ANSWER
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() });

but I would rather use your first option.

Explanation: perl arrays interpolate inside "", qx(), etc.

Above is array reference [] holding result of function, being dereferenced by @{}, and interpolated inside qx().

1
On

Backticks and qx are equivalent to the builtin readpipe function, so you could explicitly use that:

$result = readpipe("some-shell-command " . get_value());