| 1 | package IO::AsyncX::System; | ||||||||||||||
| 2 | # ABSTRACT: system() in background for IO::Async | ||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | our $VERSION = '0.002'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | IO::AsyncX::System - fork+exec, capturing STDOUT/STDERR | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||
| 15 | |||||||||||||||
| 16 | version 0.002 | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||
| 19 | |||||||||||||||
| 20 | use feature qw(say); | ||||||||||||||
| 21 | use IO::Async::Loop; | ||||||||||||||
| 22 | use IO::AsyncX::System; | ||||||||||||||
| 23 | my $loop = IO::Async::Loop->new; | ||||||||||||||
| 24 | $loop->add( | ||||||||||||||
| 25 | my $system = IO::AsyncX::System->new | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | my ($code, $stdout, $stderr) = $system->run([qw(ls)])->get; | ||||||||||||||
| 28 | say for @$stdout; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||
| 31 | |||||||||||||||
| 32 | Intended as a replacement for L</system> in L<IO::Async>-using code. | ||||||||||||||
| 33 | Provides a single L</run> method which will fork+exec (via L<IO::Async::Process>), | ||||||||||||||
| 34 | capturing STDOUT/STDERR, and returning a L<Future> holding the exit code and output. | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =cut | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 2 | ▶ | use curry; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | 2 | ▶ | use Future; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 2 | ▶ | use Encode qw(decode_utf8); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 2 | ▶ | use IO::Async::Process; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | =head1 METHODS | ||||||||||||||
| 44 | |||||||||||||||
| 45 | =cut | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =head2 run | ||||||||||||||
| 48 | |||||||||||||||
| 49 | Takes a single parameter defining the command to run: | ||||||||||||||
| 50 | |||||||||||||||
| 51 | $system->run(['ls']); | ||||||||||||||
| 52 | |||||||||||||||
| 53 | plus optional named parameters: | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =over 4 | ||||||||||||||
| 56 | |||||||||||||||
| 57 | =item * utf8 - interprets all input/output as UTF-8, so STDOUT/STDERR will be returned as arrayrefs containing Perl strings rather than raw bytes | ||||||||||||||
| 58 | |||||||||||||||
| 59 | =item * binary - the reverse of utf8 (and the default) | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =item * stdin - an arrayref of data to pass as STDIN | ||||||||||||||
| 62 | |||||||||||||||
| 63 | =item * timeout - kill the process if it doesn't complete within this many seconds | ||||||||||||||
| 64 | |||||||||||||||
| 65 | =back | ||||||||||||||
| 66 | |||||||||||||||
| 67 | Returns a L<Future> which resolves to the exit code, STDOUT and STDERR arrayrefs: | ||||||||||||||
| 68 | |||||||||||||||
| 69 | $system->run([...]) ==> ($exitcode, $stdout_arrayref, $stderr_arrayref) | ||||||||||||||
| 70 | |||||||||||||||
| 71 | STDIN/STDOUT/STDERR are arrayrefs split by line. In binary mode, they will hold a single element each. | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =cut | ||||||||||||||
| 74 | |||||||||||||||
| 75 | sub run { | ||||||||||||||
| 76 | 4 | ▶ | my ($self, $cmd, %args) = @_; | ||||||||||||
|
Subroutine
run: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||
| 77 | 4 | my $stdout = []; | |||||||||||||
| 78 | 4 | my $stderr = []; | |||||||||||||
| 79 | 4 | my $stdin = []; | |||||||||||||
| 80 | my $stdio_def = { | ||||||||||||||
| 81 | ( | ||||||||||||||
| 82 | defined($stdin) | ||||||||||||||
| 83 | ? (from => join "\n", @$stdin) | ||||||||||||||
| 84 | : () | ||||||||||||||
| 85 | ), | ||||||||||||||
| 86 | on_read => ( | ||||||||||||||
| 87 | $args{utf8} | ||||||||||||||
| 88 | 4 | ▶ | ? $self->curry::read_utf8($stdout) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 89 | : $self->curry::read_binary($stdout) | ||||||||||||||
| 90 | ), | ||||||||||||||
| 91 | }; | ||||||||||||||
| 92 | my $stderr_def = { | ||||||||||||||
| 93 | on_read => ( | ||||||||||||||
| 94 | $args{utf8} | ||||||||||||||
| 95 | 4 | ▶ | ? $self->curry::read_utf8($stderr) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | : $self->curry::read_binary($stderr) | ||||||||||||||
| 97 | ), | ||||||||||||||
| 98 | }; | ||||||||||||||
| 99 | 4 | my $f = $self->loop->new_future; | |||||||||||||
| 100 | my $proc = IO::Async::Process->new( | ||||||||||||||
| 101 | command => $cmd, | ||||||||||||||
| 102 | stdio => $stdio_def, | ||||||||||||||
| 103 | stderr => $stderr_def, | ||||||||||||||
| 104 | 3 | ▶ | on_finish => sub { $f->done($_[1], $stdout, $stderr) unless $f->is_ready }, | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | ▶ | on_exception => sub { $f->fail($_[1]) unless $f->is_ready }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||
| 106 | 4 | ); | |||||||||||||
| 107 | 4 | $self->add_child($proc); | |||||||||||||
| 108 | 4 | ▶ | $f->on_ready(sub { $self->remove_child($proc) }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 109 | 4 | ▶ | return $f unless $args{timeout}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | Future->wait_any( | ||||||||||||||
| 111 | $f, | ||||||||||||||
| 112 | $self->loop->timeout_future(after => $args{timeout})->on_fail( | ||||||||||||||
| 113 | 1 | ▶ | sub { $proc->kill(9) if $proc->is_running } | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||
| 114 | ) | ||||||||||||||
| 115 | 1 | ); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | sub read_binary { | ||||||||||||||
| 119 | 6 | ▶ | my ($self, $target, $stream, $buf, $eof) = @_; | ||||||||||||
|
Subroutine
read_binary: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 120 | 6 | ▶ | push @$target, '' unless @$target; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 6 | $target->[0] .= $$buf; | |||||||||||||
| 122 | 6 | $$buf = ''; | |||||||||||||
| 123 | 6 | 0 | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | sub read_utf8 { | ||||||||||||||
| 127 | 3 | ▶ | my ($self, $target, $stream, $buf, $eof) = @_; | ||||||||||||
|
Subroutine
read_utf8: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 128 | 3 | ▶ | push @$target, decode_utf8($1) while $$buf =~ s/^(.*)\n//; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 3 | ▶ | return 0 unless length $$buf; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 2 | ▶ | push @$target, decode_utf8($$buf) if $eof; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 2 | 0 | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | 2 | 1; | |||||||||||||
| 134 | |||||||||||||||
| 135 | __END__ | ||||||||||||||