develooper Front page | perl.perl6.users | Postings from August 2017

Re: Running external CLI tool and capturing output

Thread Previous | Thread Next
From:
Gabor Szabo
Date:
August 10, 2017 15:28
Subject:
Re: Running external CLI tool and capturing output
Message ID:
CABe4FJAuHGyE+W3GK-dhkPzs66SqpBOAk4NhMF2fX8WHbciaaw@mail.gmail.com
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
<awwaiid@thelackthereof.org> 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 <szabgab@gmail.com> 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<out>;
>>
>> or even
>>
>> say capture($*EXECUTABLE, 'bin/create_db.pl6')<out>;
>>
>> 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.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About