Running external CLI tool and capturing output

2017-08-10 Thread Gabor Szabo
The documentation has a nice example showing how to run an external
program and how to get its output or even its standard error.
https://docs.perl6.org/type/Proc

However it looks a lot more complex than the plain backtick Perl 5 has
and more complex than the capture function of Capture::Tiny.
IMHO it is way too much code.

I wrote a simple function wrapping it:

sub capture(*@args) {
my $p = run @args, :out, :err;
my $output = $p.out.slurp: :close;
my $error  = $p.err.slurp: :close;
my $exit   = $p.exitcode;

return {
out  => $output,
err  => $error,
exit => $exit;
};
}

It can be used as:

my $res = capture($*EXECUTABLE, 'bin/create_db.pl6');
say $res;

or even

say capture($*EXECUTABLE, 'bin/create_db.pl6');

I wonder if I have just invented something that already exist in
Rakudo or if it is not there, then wouldn't it be a good idea to add
such a simple way to run external commands?

regards
   Gabor
ps. Backtick actually expected a single string and not a list of
parameters and supporting that mode, even if it is less secure, might
be also a good idea.


Re: Running external CLI tool and capturing output

2017-08-10 Thread Brandon Allbery
"ps. security bad, correctness bad, do the simplest thing even when it's
wrong."

On Thu, Aug 10, 2017 at 10:57 AM, Gabor Szabo  wrote:

> The documentation has a nice example showing how to run an external
> program and how to get its output or even its standard error.
> https://docs.perl6.org/type/Proc
>
> However it looks a lot more complex than the plain backtick Perl 5 has
> and more complex than the capture function of Capture::Tiny.
> IMHO it is way too much code.
>
> I wrote a simple function wrapping it:
>
> sub capture(*@args) {
> my $p = run @args, :out, :err;
> my $output = $p.out.slurp: :close;
> my $error  = $p.err.slurp: :close;
> my $exit   = $p.exitcode;
>
> return {
> out  => $output,
> err  => $error,
> exit => $exit;
> };
> }
>
> It can be used as:
>
> my $res = capture($*EXECUTABLE, 'bin/create_db.pl6');
> say $res;
>
> or even
>
> say capture($*EXECUTABLE, 'bin/create_db.pl6');
>
> I wonder if I have just invented something that already exist in
> Rakudo or if it is not there, then wouldn't it be a good idea to add
> such a simple way to run external commands?
>
> regards
>Gabor
> ps. Backtick actually expected a single string and not a list of
> parameters and supporting that mode, even if it is less secure, might
> be also a good idea.
>



-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: Running external CLI tool and capturing output

2017-08-10 Thread Brock Wilcox
How about qx and qxx? I guess those don't separate/capture stderr, and
don't separate out the params.

--Brock


On Thu, Aug 10, 2017 at 10:57 AM, Gabor Szabo  wrote:

> The documentation has a nice example showing how to run an external
> program and how to get its output or even its standard error.
> https://docs.perl6.org/type/Proc
>
> However it looks a lot more complex than the plain backtick Perl 5 has
> and more complex than the capture function of Capture::Tiny.
> IMHO it is way too much code.
>
> I wrote a simple function wrapping it:
>
> sub capture(*@args) {
> my $p = run @args, :out, :err;
> my $output = $p.out.slurp: :close;
> my $error  = $p.err.slurp: :close;
> my $exit   = $p.exitcode;
>
> return {
> out  => $output,
> err  => $error,
> exit => $exit;
> };
> }
>
> It can be used as:
>
> my $res = capture($*EXECUTABLE, 'bin/create_db.pl6');
> say $res;
>
> or even
>
> say capture($*EXECUTABLE, 'bin/create_db.pl6');
>
> I wonder if I have just invented something that already exist in
> Rakudo or if it is not there, then wouldn't it be a good idea to add
> such a simple way to run external commands?
>
> regards
>Gabor
> ps. Backtick actually expected a single string and not a list of
> parameters and supporting that mode, even if it is less secure, might
> be also a good idea.
>


Re: Running external CLI tool and capturing output

2017-08-10 Thread Gabor Szabo
Oh right. Thanks. I forgot about them. Maybe
https://docs.perl6.org/routine/run should mention them as well.

In any case a simpler way to capture everything might be useful.

Gabor

On Thu, Aug 10, 2017 at 6:09 PM, Brock Wilcox
 wrote:
> How about qx and qxx? I guess those don't separate/capture stderr, and don't
> separate out the params.
>
> --Brock
>
>
> On Thu, Aug 10, 2017 at 10:57 AM, Gabor Szabo  wrote:
>>
>> The documentation has a nice example showing how to run an external
>> program and how to get its output or even its standard error.
>> https://docs.perl6.org/type/Proc
>>
>> However it looks a lot more complex than the plain backtick Perl 5 has
>> and more complex than the capture function of Capture::Tiny.
>> IMHO it is way too much code.
>>
>> I wrote a simple function wrapping it:
>>
>> sub capture(*@args) {
>> my $p = run @args, :out, :err;
>> my $output = $p.out.slurp: :close;
>> my $error  = $p.err.slurp: :close;
>> my $exit   = $p.exitcode;
>>
>> return {
>> out  => $output,
>> err  => $error,
>> exit => $exit;
>> };
>> }
>>
>> It can be used as:
>>
>> my $res = capture($*EXECUTABLE, 'bin/create_db.pl6');
>> say $res;
>>
>> or even
>>
>> say capture($*EXECUTABLE, 'bin/create_db.pl6');
>>
>> I wonder if I have just invented something that already exist in
>> Rakudo or if it is not there, then wouldn't it be a good idea to add
>> such a simple way to run external commands?
>>
>> regards
>>Gabor
>> ps. Backtick actually expected a single string and not a list of
>> parameters and supporting that mode, even if it is less secure, might
>> be also a good idea.


Re: Running external CLI tool and capturing output

2017-08-10 Thread Tom Browder
On Thu, Aug 10, 2017 at 11:28 Gabor Szabo  wrote:

> Oh right. Thanks. I forgot about them. Maybe
> https://docs.perl6.org/routine/run should mention them as well.
>
> In any case a simpler way to capture everything might be useful.


Maybe not simpler but take a look at my published Perl 6 module
"Proc::More" and its  "run-command".

-Tom