perlqx

Is it possible to call a function within qx?


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.


Solution

  • 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().