blib/lib/IO/Async/Process.pm

filter: Statementstmt 97.7% 220 / 225 Branchbran 79.2% 122 / 154 Conditioncond 77.1% 27 / 35 MC/DCmcdc 43.4% 10 / 23 Subroutinesub 100% 36 / 36 Podpod 94.1% 16 / 17 totaltotal 87.9% 431 / 490 CC 99 SCAR 28.2
CC
99
Cov
89%
configure_fd
39.0
configure
18.5
_prepare_fds
17.7
CRAP 16.8
« blib/lib/IO/Async/Stream.pm ↑ Summary blib/lib/IO/Async/Routine.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2011-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Process 0.805;
7  
8 40 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use base qw( IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 ▶use Socket qw( SOCK_STREAM );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 40 ▶use IO::Async::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<IO::Async::Process> - start and manage a child process
23  
24 =head1 SYNOPSIS
25  
26 =for highlighter language=perl
27  
28 use IO::Async::Process;
29  
30 use IO::Async::Loop;
31 my $loop = IO::Async::Loop->new;
32  
33 my $process = IO::Async::Process->new(
34 command => [ "tr", "a-z", "n-za-m" ],
35 stdin => {
36 from => "hello world\n",
37 },
38 stdout => {
39 on_read => sub {
40 my ( $stream, $buffref ) = @_;
41 while( $$buffref =~ s/^(.*)\n// ) {
42 print "Rot13 of 'hello world' is '$1'\n";
43 }
44  
45 return 0;
46 },
47 },
48  
49 on_finish => sub {
50 $loop->stop;
51 },
52 );
53  
54 $loop->add( $process );
55  
56 $loop->run;
57  
58 Also accessible via the L<IO::Async::Loop/open_process> method:
59  
60 $loop->open_process(
61 command => [ "/bin/ping", "-c4", "some.host" ],
62  
63 stdout => {
64 on_read => sub {
65 my ( $stream, $buffref, $eof ) = @_;
66 while( $$buffref =~ s/^(.*)\n// ) {
67 print "PING wrote: $1\n";
68 }
69 return 0;
70 },
71 },
72  
73 on_finish => sub {
74 my $process = shift;
75 my ( $exitcode ) = @_;
76 my $status = ( $exitcode >> 8 );
77 ...
78 },
79 );
80  
81 =head1 DESCRIPTION
82  
83 This subclass of L<IO::Async::Notifier> starts a child process, and invokes a
84 callback when it exits. The child process can either execute a given block of
85 code (via C<fork(2)>), or a command.
86  
87 =cut
88  
89 =head1 EVENTS
90  
91 The following events are invoked, either using subclass methods or CODE
92 references in parameters:
93  
94 =head2 on_finish $exitcode
95  
96 Invoked after the process has exited by normal means (i.e. an C<exit(2)>
97 syscall from a process, or C<return>ing from the code block), and has closed
98 all its file descriptors.
99  
100 =head2 on_exception $exception, $errno, $exitcode
101  
102 Invoked when the process exits by an exception from C<code>, or by failing to
103 C<exec(2)> the given command. C<$errno> will be a dualvar, containing both
104 number and string values. After a successful C<exec()> call, this condition
105 can no longer happen.
106  
107 Note that this has a different name and a different argument order from
108 C<< Loop->open_process >>'s C<on_error>.
109  
110 If this is not provided and the process exits with an exception, then
111 C<on_finish> is invoked instead, being passed just the exit code.
112  
113 Since this is just the results of the underlying C<< $loop->spawn_child >>
114 C<on_exit> handler in a different order it is possible that the C<$exception>
115 field will be an empty string. It will however always be defined. This can be
116 used to distinguish the two cases:
117  
118 on_exception => sub {
119 my $self = shift;
120 my ( $exception, $errno, $exitcode ) = @_;
121  
122 if( length $exception ) {
123 print STDERR "The process died with the exception $exception " .
124 "(errno was $errno)\n";
125 }
126 elsif( ( my $status = W_EXITSTATUS($exitcode) ) == 255 ) {
127 print STDERR "The process failed to exec() - $errno\n";
128 }
129 else {
130 print STDERR "The process exited with exit status $status\n";
131 }
132 }
133  
134 =cut
135  
136 =head1 CONSTRUCTOR
137  
138 =cut
139  
140 =head2 new
141  
142 $process = IO::Async::Process->new( %args );
143  
144 Constructs a new C<IO::Async::Process> object and returns it.
145  
146 Once constructed, the C<Process> will need to be added to the C<Loop> before
147 the child process is started.
148  
149 =cut
150  
151 sub _init
152 {
153 274 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
154 274 $self->SUPER::_init( @_ );
155  
156 274 $self->{to_close} = {};
157 274 $self->{finish_futures} = [];
158 }
159  
160 =head1 PARAMETERS
161  
162 The following named parameters may be passed to C<new> or C<configure>:
163  
164 =head2 on_finish => CODE
165  
166 =head2 on_exception => CODE
167  
168 CODE reference for the event handlers.
169  
170 Once the C<on_finish> continuation has been invoked, the C<IO::Async::Process>
171 object is removed from the containing L<IO::Async::Loop> object.
172  
173 The following parameters may be passed to C<new>, or to C<configure> before
174 the process has been started (i.e. before it has been added to the C<Loop>).
175 Once the process is running these cannot be changed.
176  
177 =head2 command => ARRAY or STRING
178  
179 Either a reference to an array containing the command and its arguments, or a
180 plain string containing the command. This value is passed into perl's
181 C<exec(2)> function.
182  
183 =head2 code => CODE
184  
185 A block of code to execute in the child process. It will be called in scalar
186 context inside an C<eval> block.
187  
188 =head2 setup => ARRAY
189  
190 Optional reference to an array to pass to the underlying C<Loop>
191 C<spawn_child> method.
192  
193 =head2 fdI<n> => HASH
194  
195 A hash describing how to set up file descriptor I<n>. The hash may contain the
196 following keys:
197  
198 =over 4
199  
200 =item via => STRING
201  
202 Configures how this file descriptor will be configured for the child process.
203 Must be given one of the following mode names:
204  
205 =over 4
206  
207 =item pipe_read
208  
209 The child will be given the writing end of a C<pipe(2)>; the parent may read
210 from the other.
211  
212 =item pipe_write
213  
214 The child will be given the reading end of a C<pipe(2)>; the parent may write
215 to the other. Since an EOF condition of this kind of handle cannot reliably be
216 detected, C<on_finish> will not wait for this type of pipe to be closed.
217  
218 =item pipe_rdwr
219  
220 Only valid on the C<stdio> filehandle. The child will be given the reading end
221 of one C<pipe(2)> on STDIN and the writing end of another on STDOUT. A single
222 Stream object will be created in the parent configured for both filehandles.
223  
224 =item socketpair
225  
226 The child will be given one end of a C<socketpair(2)>; the parent will be
227 given the other. The family of this socket may be given by the extra key
228 called C<family>; defaulting to C<unix>. The socktype of this socket may be
229 given by the extra key called C<socktype>; defaulting to C<stream>. If the
230 type is not C<SOCK_STREAM> then a L<IO::Async::Socket> object will be
231 constructed for the parent side of the handle, rather than
232 L<IO::Async::Stream>.
233  
234 =back
235  
236 Once the filehandle is set up, the C<fd> method (or its shortcuts of C<stdin>,
237 C<stdout> or C<stderr>) may be used to access the
238 L<IO::Async::Handle>-subclassed object wrapped around it.
239  
240 The value of this argument is implied by any of the following alternatives.
241  
242 =item on_read => CODE
243  
244 The child will be given the writing end of a pipe. The reading end will be
245 wrapped by an L<IO::Async::Stream> using this C<on_read> callback function.
246  
247 =item into => SCALAR
248  
249 The child will be given the writing end of a pipe. The referenced scalar will
250 be filled by data read from the child process. This data may not be available
251 until the pipe has been closed by the child.
252  
253 =item from => STRING
254  
255 The child will be given the reading end of a pipe. The string given by the
256 C<from> parameter will be written to the child. When all of the data has been
257 written the pipe will be closed.
258  
259 =item prefork => CODE
260  
261 Only valid for handles with a C<via> of C<socketpair>. The code block runs
262 after the C<socketpair(2)> is created, but before the child is forked. This
263 is handy for when you adjust both ends of the created socket (for example, to
264 use C<setsockopt(3)>) from the controlling parent, before the child code runs.
265 The arguments passed in are the L<IO::Socket> objects for the parent and child
266 ends of the socket.
267  
268 $prefork->( $localfd, $childfd );
269  
270 =back
271  
272 =head2 stdin => ...
273  
274 =head2 stdout => ...
275  
276 =head2 stderr => ...
277  
278 Shortcuts for C<fd0>, C<fd1> and C<fd2> respectively.
279  
280 =head2 stdio => ...
281  
282 Special filehandle to affect STDIN and STDOUT at the same time. This
283 filehandle supports being configured for both reading and writing at the same
284 time.
285  
286 =cut
287  
288 sub configure
289 {
290 274 ▶ my $self = shift;
Subroutine configure: called CC=18 SCAR=29.2
Pod documented
291 274 my %params = @_;
292  
293 274 foreach (qw( on_finish on_exception )) {
294 548 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 353 195 548
295 }
296  
297 # All these parameters can only be configured while the process isn't
298 # running
299 274 my %setup_params;
300 274 foreach (qw( code command setup stdin stdout stderr stdio ), grep { m/^fd\d+$/ } keys %params ) {
301 1926 ▶ $setup_params{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 737 1189 1926
302 }
303  
304 274 ▶ if( $self->is_running ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_running) 0 274 274
305 0 ▶ keys %setup_params and croak "Cannot configure a running Process with " . join ", ", keys %setup_params;
Branch: 1 branch
BranchTrueFalseTotal
keys %setup_params and croak('Cannot configure a running Process with ' . join(", ", keys %setup_params)) 0 0 0
306 }
307  
308 defined( exists $setup_params{code} ? $setup_params{code} : $self->{code} ) +
309 274 ▶ defined( exists $setup_params{command} ? $setup_params{command} : $self->{command} ) <= 1 or
Branch: 3 branches
BranchTrueFalseTotal
defined(exists $setup_params{"code"} ? $setup_params{"code"} : $self->{"code"}) + defined(exists $setup_params{"command"} ? $setup_params{"command"} : $self->{"command"}) <= 1 or croak("Cannot have both 'code' and 'command'") 0 274 274
exists $setup_params{'code'} ? : 206 68 274
exists $setup_params{'command'} ? : 146 128 274
310 croak "Cannot have both 'code' and 'command'";
311  
312 274 foreach (qw( code command setup )) {
313 822 ▶ $self->{$_} = delete $setup_params{$_} if exists $setup_params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $setup_params{$_} 505 317 822
314 }
315  
316 274 ▶ $self->configure_fd( 0, %{ delete $setup_params{stdin} } ) if $setup_params{stdin};
Branch: 1 branch
BranchTrueFalseTotal
if $setup_params{"stdin"} 32 242 274
317 274 ▶ $self->configure_fd( 1, %{ delete $setup_params{stdout} } ) if $setup_params{stdout};
Branch: 1 branch
BranchTrueFalseTotal
if $setup_params{"stdout"} 131 143 274
318 274 ▶ $self->configure_fd( 2, %{ delete $setup_params{stderr} } ) if $setup_params{stderr};
Branch: 1 branch
BranchTrueFalseTotal
if $setup_params{"stderr"} 52 222 274
319  
320 274 ▶ $self->configure_fd( 'io', %{ delete $setup_params{stdio} } ) if $setup_params{stdio};
Branch: 1 branch
BranchTrueFalseTotal
if $setup_params{"stdio"} 9 265 274
321  
322 # All the rest are fd\d+
323 274 foreach ( keys %setup_params ) {
324 8 ▶ my ( $fd ) = m/^fd(\d+)$/ or croak "Expected 'fd\\d+'";
Branch: 1 branch
BranchTrueFalseTotal
my($fd) = /^fd(\d+)$/u or croak("Expected 'fd\\d+'") 0 8 8
325 8 $self->configure_fd( $fd, %{ $setup_params{$_} } );
326 }
327  
328 274 $self->SUPER::configure( %params );
329 }
330  
331 # These are from the perspective of the parent
332 40 ▶use constant FD_VIA_PIPEREAD => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
333 40 ▶use constant FD_VIA_PIPEWRITE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
334 40 ▶use constant FD_VIA_PIPERDWR => 3; # Only valid for stdio pseudo-fd
Subroutine BEGIN: called CC=1 SCAR=0.0
335 40 ▶use constant FD_VIA_SOCKETPAIR => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
336  
337 40 my %via_names = (
338 pipe_read => FD_VIA_PIPEREAD,
339 pipe_write => FD_VIA_PIPEWRITE,
340 pipe_rdwr => FD_VIA_PIPERDWR,
341 socketpair => FD_VIA_SOCKETPAIR,
342 );
343  
344 sub configure_fd
345 {
346 232 ▶ my $self = shift;
Subroutine configure_fd: called CC=35 SCAR=36.6
Pod undocumented
347 232 my ( $fd, %args ) = @_;
348  
349 232 ▶ $self->is_running and croak "Cannot configure fd $fd in a running Process";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_running and croak("Cannot configure fd $fd in a running Process") 0 232 232
350  
351 232 ▶ if( $fd eq "io" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fd eq 'io') { } 9 223 232
elsif ($fd == 0 or $fd == 1) { } 171 52 223
352 9 ▶ exists $self->{fd_opts}{$_} and croak "Cannot configure stdio since fd$_ is already defined" for 0 .. 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"fd_opts"}{$_} and croak("Cannot configure stdio since fd$_ is already defined") 0 18 18
353 }
354 ▶ elsif( $fd == 0 or $fd == 1 ) {
Condition100%3/3
$fd == 0 or $fd == 1
l!l&&r!l&&!r
3613552
MC/DC100%2/2
$fd == 0 or $fd == 1
$fd == 0 $fd == 1
Truth table100%3/3
A or B
A= $fd == 0B= $fd == 1
ABresult
1X1
011
000
355 171 ▶ exists $self->{fd_opts}{io} and croak "Cannot configure fd$fd since stdio is already defined";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"fd_opts"}{"io"} and croak("Cannot configure fd$fd since stdio is already defined") 0 171 171
356 }
357  
358 232 ▶ my $opts = $self->{fd_opts}{$fd} ||= {};
Condition50%1/2
$self->{"fd_opts"}{$fd} ||= {}
l!l
0232
MC/DC0%0/1
$self->{"fd_opts"}{$fd} ||= {}
$self->{"fd_opts"}{$fd}
Truth table50%1/2
A
A= $self->{"fd_opts"}{$fd}
Aresult
11
00
359 232 my $via = $opts->{via};
360  
361 232 my ( $wants_read, $wants_write );
362  
363 232 ▶ if( my $via_name = delete $args{via} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $via_name = delete $args{"via"}) 32 200 232
364 32 ▶ defined $via and
Branch: 1 branch
BranchTrueFalseTotal
defined $via and croak("Cannot change the 'via' mode of fd$fd now that it is already configured") 0 32 32
365 croak "Cannot change the 'via' mode of fd$fd now that it is already configured";
366  
367 32 ▶ $via = $via_names{$via_name} or
Branch: 1 branch
BranchTrueFalseTotal
$via = $via_names{$via_name} or croak("Unrecognised 'via' name of '${via_name}'") 0 32 32
368 croak "Unrecognised 'via' name of '$via_name'";
369 }
370  
371 232 ▶ if( my $on_read = delete $args{on_read} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $on_read = delete $args{'on_read'}) { } 10 222 232
elsif (my $into = delete $args{'into'}) { } 169 53 222
372 10 $opts->{handle}{on_read} = $on_read;
373  
374 10 $wants_read++;
375 }
376 elsif( my $into = delete $args{into} ) {
377 $opts->{handle}{on_read} = sub {
378 233 ▶ my ( undef, $buffref, $eof ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
379 233 ▶ $$into .= $$buffref if $eof;
Branch: 1 branch
BranchTrueFalseTotal
if $eof 144 89 233
380 233 return 0;
381 169 };
382  
383 169 $wants_read++;
384 }
385  
386 232 ▶ if( defined( my $from = delete $args{from} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $from = delete $args{"from"})) 23 209 232
387 23 $opts->{from} = $from;
388  
389 23 $wants_write++;
390 }
391  
392 232 ▶ if( defined $via and $via == FD_VIA_SOCKETPAIR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $via and $via == 4) 4 228 232
Condition100%3/3
defined $via and $via == 4
!ll&&!rl&&r
200284
MC/DC100%2/2
defined $via and $via == 4
defined $via $via == 4
Truth table100%3/3
A and B
A= defined $viaB= $via == 4
ABresult
0X0
100
111
393 4 $self->{fd_opts}{$fd}{$_} = delete $args{$_} for qw( family socktype prefork );
394 }
395  
396 232 ▶ keys %args and croak "Unexpected extra keys for fd $fd - " . join ", ", keys %args;
Branch: 1 branch
BranchTrueFalseTotal
keys %args and croak("Unexpected extra keys for fd $fd - " . join(", ", keys %args)) 0 232 232
397  
398 232 ▶ if( !defined $via ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $via) { } 200 32 232
elsif ($via == 1) { } 10 22 32
elsif ($via == 2) { } 15 7 22
elsif ($via == 3 or $via == 4) { } 7 0 7
399 200 ▶ $via = FD_VIA_PIPEREAD if $wants_read and !$wants_write;
Branch: 1 branch
BranchTrueFalseTotal
if $wants_read and not $wants_write 177 23 200
Condition100%3/3
$wants_read and not $wants_write
!ll&&!rl&&r
212177
MC/DC100%2/2
$wants_read and not $wants_write
$wants_read not $wants_write
Truth table100%3/3
A and not(B)
A= $wants_readB= not $wants_write
ABresult
0X0
100
111
400 200 ▶ $via = FD_VIA_PIPEWRITE if !$wants_read and $wants_write;
Branch: 1 branch
BranchTrueFalseTotal
if not $wants_read and $wants_write 21 179 200
Condition66%2/3
not $wants_read and $wants_write
!ll&&!rl&&r
179021
MC/DC50%1/2
not $wants_read and $wants_write
not $wants_read $wants_write
Truth table66%2/3
not(A) and B
A= not $wants_readB= $wants_write
ABresult
0X0
100
111
401 200 ▶ $via = FD_VIA_PIPERDWR if $wants_read and $wants_write;
Branch: 1 branch
BranchTrueFalseTotal
if $wants_read and $wants_write 2 198 200
Condition100%3/3
$wants_read and $wants_write
!ll&&!rl&&r
211772
MC/DC100%2/2
$wants_read and $wants_write
$wants_read $wants_write
Truth table100%3/3
A and B
A= $wants_readB= $wants_write
ABresult
0X0
100
111
402 }
403 elsif( $via == FD_VIA_PIPEREAD ) {
404 10 ▶ $wants_write and $via = FD_VIA_PIPERDWR;
Branch: 1 branch
BranchTrueFalseTotal
$wants_write and $via = 3 0 10 10
405 }
406 elsif( $via == FD_VIA_PIPEWRITE ) {
407 15 ▶ $wants_read and $via = FD_VIA_PIPERDWR;
Branch: 1 branch
BranchTrueFalseTotal
$wants_read and $via = 3 0 15 15
408 }
409 ▶ elsif( $via == FD_VIA_PIPERDWR or $via == FD_VIA_SOCKETPAIR ) {
Condition66%2/3
$via == 3 or $via == 4
l!l&&r!l&&!r
340
MC/DC0%0/2
$via == 3 or $via == 4
$via == 3 $via == 4
Truth table66%2/3
A or B
A= $via == 3B= $via == 4
ABresult
1X1
011
000
410 # Fine
411 }
412 else {
413 0 die "Need to check fd_via{$fd}\n";
414 }
415  
416 232 ▶ $via == FD_VIA_PIPERDWR and $fd ne "io" and
Branch: 1 branch
BranchTrueFalseTotal
$via == 3 and $fd ne "io" and croak("Cannot both read and write simultaneously on fd$fd") 0 232 232
Condition66%2/3
$via == 3 and $fd ne "io"
!ll&&!rl&&r
22750
MC/DC0%0/2
$via == 3 and $fd ne "io"
$via == 3 $fd ne "io"
Truth table66%2/3
A and B
A= $via == 3B= $fd ne "io"
ABresult
0X0
100
111
417 croak "Cannot both read and write simultaneously on fd$fd";
418  
419 232 ▶ defined $via and $opts->{via} = $via;
Branch: 1 branch
BranchTrueFalseTotal
defined $via and $opts->{"via"} = $via 232 0 232
420 }
421  
422 sub _prepare_fds
423 {
424 273 ▶ my $self = shift;
Subroutine _prepare_fds: called CC=17 SCAR=28.8
425 273 my ( $loop ) = @_;
426  
427 273 my $fd_handle = $self->{fd_handle};
428 273 my $fd_opts = $self->{fd_opts};
429  
430 273 my $finish_futures = $self->{finish_futures};
431  
432 273 my @setup;
433  
434 273 foreach my $fd ( keys %$fd_opts ) {
435 232 my $opts = $fd_opts->{$fd};
436 232 my $via = $opts->{via};
437  
438 232 my $handle = $self->fd( $fd );
439  
440 232 ▶ my $key = $fd eq "io" ? "stdio" : "fd$fd";
Branch: 1 branch
BranchTrueFalseTotal
$fd eq 'io' ? : 9 223 232
441 232 my $write_only;
442  
443 232 ▶ if( $via == FD_VIA_PIPEREAD ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($via == 1) { } 187 45 232
elsif ($via == 2) { } 36 9 45
elsif ($via == 3) { } 5 4 9
elsif ($via == 4) { } 4 0 4
444 187 ▶ my ( $myfd, $childfd ) = IO::Async::OS->pipepair or croak "Unable to pipe() - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->pipepair or croak("Unable to pipe() - $!") 0 187 187
445 187 $myfd->blocking( 0 );
446  
447 187 $handle->configure( read_handle => $myfd );
448  
449 187 push @setup, $key => [ dup => $childfd ];
450 187 $self->{to_close}{$childfd->fileno} = $childfd;
451 }
452 elsif( $via == FD_VIA_PIPEWRITE ) {
453 36 ▶ my ( $childfd, $myfd ) = IO::Async::OS->pipepair or croak "Unable to pipe() - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->pipepair or croak("Unable to pipe() - $!") 0 36 36
454 36 $myfd->blocking( 0 );
455 36 $write_only++;
456  
457 36 $handle->configure( write_handle => $myfd );
458  
459 36 push @setup, $key => [ dup => $childfd ];
460 36 $self->{to_close}{$childfd->fileno} = $childfd;
461 }
462 elsif( $via == FD_VIA_PIPERDWR ) {
463 5 ▶ $key eq "stdio" or croak "Oops - should only be FD_VIA_PIPERDWR on stdio";
Branch: 1 branch
BranchTrueFalseTotal
$key eq "stdio" or croak("Oops - should only be FD_VIA_PIPERDWR on stdio") 0 5 5
464 # Can't use pipequad here for now because we need separate FDs so we
465 # can ->close them properly
466 5 ▶ my ( $myread, $childwrite ) = IO::Async::OS->pipepair or croak "Unable to pipe() - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->pipepair or croak("Unable to pipe() - $!") 0 5 5
467 5 ▶ my ( $childread, $mywrite ) = IO::Async::OS->pipepair or croak "Unable to pipe() - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->pipepair or croak("Unable to pipe() - $!") 0 5 5
468 5 $_->blocking( 0 ) for $myread, $mywrite;
469  
470 5 $handle->configure( read_handle => $myread, write_handle => $mywrite );
471  
472 5 push @setup, stdin => [ dup => $childread ], stdout => [ dup => $childwrite ];
473 5 $self->{to_close}{$childread->fileno} = $childread;
474 5 $self->{to_close}{$childwrite->fileno} = $childwrite;
475 }
476 elsif( $via == FD_VIA_SOCKETPAIR ) {
477 4 ▶ my ( $myfd, $childfd ) = IO::Async::OS->socketpair( $opts->{family}, $opts->{socktype} ) or croak "Unable to socketpair() - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = "IO::Async::OS"->socketpair($opts->{"family"}, $opts->{"socktype"}) or croak("Unable to socketpair() - $!") 0 4 4
478 4 $myfd->blocking( 0 );
479  
480 4 ▶ $opts->{prefork}->( $myfd, $childfd ) if $opts->{prefork};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"prefork"} 1 3 4
481  
482 4 $handle->configure( handle => $myfd );
483  
484 4 ▶ if( $key eq "stdio" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'stdio') { } 4 0 4
485 4 push @setup, stdin => [ dup => $childfd ], stdout => [ dup => $childfd ];
486 }
487 else {
488 0 push @setup, $key => [ dup => $childfd ];
489 }
490 4 $self->{to_close}{$childfd->fileno} = $childfd;
491 }
492 else {
493 0 croak "Unsure what to do with fd_via==$via";
494 }
495  
496 232 $self->add_child( $handle );
497  
498 232 ▶ unless( $write_only ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($write_only) 196 36 232
499 196 push @$finish_futures, $handle->new_close_future;
500 }
501 }
502  
503 273 return @setup;
504 }
505  
506 sub _add_to_loop
507 {
508 274 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=6 SCAR=17.9
509 274 my ( $loop ) = @_;
510  
511 $self->{code} or $self->{command} or
512 274 ▶ croak "Require either 'code' or 'command' in $self";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"code"} or $self->{"command"} or croak("Require either 'code' or 'command' in $self") 0 274 274
Condition66%2/3
$self->{"code"} or $self->{"command"}
l!l&&r!l&&!r
1591150
MC/DC0%0/2
$self->{"code"} or $self->{"command"}
$self->{"code"} $self->{"command"}
Truth table66%2/3
A or B
A= $self->{"code"}B= $self->{"command"}
ABresult
1X1
011
000
513  
514 274 ▶ $self->can_event( "on_finish" ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->can_event("on_finish") or croak("Expected either an on_finish callback or to be able to ->on_finish") 1 273 274
515 croak "Expected either an on_finish callback or to be able to ->on_finish";
516  
517 273 my @setup;
518 273 ▶ push @setup, @{ $self->{setup} } if $self->{setup};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"setup"} 76 197 273
519  
520 273 push @setup, $self->_prepare_fds( $loop );
521  
522 273 my $finish_futures = delete $self->{finish_futures};
523  
524 273 my ( $exitcode, $dollarbang, $dollarat );
525 273 push @$finish_futures, my $exit_future = $loop->new_future;
526  
527 $self->{pid} = $loop->spawn_child(
528 code => $self->{code},
529 command => $self->{command},
530  
531 setup => \@setup,
532  
533 on_exit => $self->_capture_weakself( sub {
534 235 ▶ ( my $self, undef, $exitcode, $dollarbang, $dollarat ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
535  
536 235 ▶ $self->debug_printf( "EXIT status=0x%04x", $exitcode ) if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 189 46 235
537 235 ▶ $exit_future->done unless $exit_future->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $exit_future->is_cancelled 189 46 235
538 273 } ),
539 );
540 247 $self->{running} = 1;
541  
542 247 $self->SUPER::_add_to_loop( @_ );
543  
544 247 $_->close for values %{ delete $self->{to_close} };
545  
546 247 my $is_code = defined $self->{code};
547  
548 247 my $f = $self->finish_future;
549  
550 $self->{_finish_future} = Future->needs_all( @$finish_futures )
551 ->on_done( $self->_capture_weakself( sub {
552 189 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 189 189
553  
554 189 ▶ $self->debug_printf( "FINISH status=0x%04x%s", $exitcode,
Branch: 2 branches
BranchTrueFalseTotal
$dollarbang ? : 94 95 189
$dollarat ? : 12 177 189
555 join " ", '', ( $dollarbang ? '$!' : '' ), ( $dollarat ? '$@' : '' )
556 );
557  
558 189 $self->{exitcode} = $exitcode;
559 189 $self->{dollarbang} = $dollarbang;
560 189 $self->{dollarat} = $dollarat;
561  
562 189 undef $self->{running};
563  
564 189 ▶ if( $is_code ? $dollarat eq "" : $dollarbang == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_code ? $dollarat eq '' : $dollarbang == 0) { } 176 13 189
$is_code ? : 102 87 189
565 176 $self->invoke_event( on_finish => $exitcode );
566 }
567 else {
568 13 ▶ $self->maybe_invoke_event( on_exception => $dollarat, $dollarbang, $exitcode ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->maybe_invoke_event("on_exception", $dollarat, $dollarbang, $exitcode) or $self->invoke_event("on_finish", $exitcode) 4 9 13
569 # Don't have a way to report dollarbang/dollarat
570 $self->invoke_event( on_finish => $exitcode );
571 }
572  
573 189 $f->done( $exitcode );
574  
575 189 $self->remove_from_parent;
576 247 } ),
577 );
578 }
579  
580 sub DESTROY
581 {
582 233 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
583 233 ▶ $self->{_finish_future}->cancel if $self->{_finish_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_finish_future"} 232 1 233
584 }
585  
586 sub notifier_name
587 {
588 12 ▶ my $self = shift;
Subroutine notifier_name: called CC=4 SCAR=14.0
Pod documented
589 12 ▶ if( length( my $name = $self->SUPER::notifier_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $name = $self->SUPER::notifier_name)) 0 12 12
590 0 return $name;
591 }
592  
593 12 ▶ return "nopid" unless my $pid = $self->pid;
Branch: 1 branch
BranchTrueFalseTotal
unless my $pid = $self->pid 4 8 12
594 8 ▶ return "[$pid]" unless $self->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_running 4 4 8
595 4 return "$pid";
596 }
597  
598 =head1 METHODS
599  
600 =cut
601  
602 =head2 finish_future
603  
604 $f = $process->finish_future;
605  
606 I<Since version 0.75.>
607  
608 Returns a L<Future> that completes when the process finishes. It will yield
609 the exit code from the process.
610  
611 =cut
612  
613 sub finish_future
614 {
615 255 ▶ my $self = shift;
Subroutine finish_future: called CC=2 SCAR=7.1
Pod documented
616 255 ▶ return $self->{finish_future} //= $self->loop->new_future;
Condition66%2/3
$self->{"finish_future"} //= $self->loop->new_future
l!l&&r!l&&!r
82470
MC/DC0%0/2
$self->{"finish_future"} //= $self->loop->new_future
$self->{"finish_future"} $self->loop->new_future
Truth table66%2/3
A //= B
A= $self->{"finish_future"}B= $self->loop->new_future
ABresult
1X1
011
000
617 }
618  
619 =head2 pid
620  
621 $pid = $process->pid;
622  
623 Returns the process ID of the process, if it has been started, or C<undef> if
624 not. Its value is preserved after the process exits, so it may be inspected
625 during the C<on_finish> or C<on_exception> events.
626  
627 =cut
628  
629 sub pid
630 {
631 132 ▶ my $self = shift;
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
632 132 return $self->{pid};
633 }
634  
635 =head2 kill
636  
637 $process->kill( $signal );
638  
639 Sends a signal to the process
640  
641 =cut
642  
643 sub kill
644 {
645 4 ▶ my $self = shift;
Subroutine kill: called CC=2 SCAR=7.1
Pod documented
646 4 my ( $signal ) = @_;
647  
648 4 ▶ kill $signal, $self->pid or croak "Cannot kill() - $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::kill $signal, $self->pid or croak("Cannot kill() - $!") 0 4 4
649 }
650  
651 =head2 is_running
652  
653 $running = $process->is_running;
654  
655 Returns true if the Process has been started, and has not yet finished.
656  
657 =cut
658  
659 sub is_running
660 {
661 985 ▶ my $self = shift;
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
662 985 return $self->{running};
663 }
664  
665 =head2 is_exited
666  
667 $exited = $process->is_exited;
668  
669 Returns true if the Process has finished running, and finished due to normal
670 C<exit(2)>.
671  
672 =cut
673  
674 sub is_exited
675 {
676 102 ▶ my $self = shift;
Subroutine is_exited: called CC=2 SCAR=7.2
Pod documented
677 102 ▶ return defined $self->{exitcode} ? ( $self->{exitcode} & 0x7f ) == 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'exitcode'} ? : 102 0 102
678 }
679  
680 =head2 exitstatus
681  
682 $status = $process->exitstatus;
683  
684 If the process exited due to normal C<exit(2)>, returns the value that was
685 passed to C<exit(2)>. Otherwise, returns C<undef>.
686  
687 =cut
688  
689 sub exitstatus
690 {
691 102 ▶ my $self = shift;
Subroutine exitstatus: called CC=2 SCAR=7.2
Pod documented
692 102 ▶ return defined $self->{exitcode} ? ( $self->{exitcode} >> 8 ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'exitcode'} ? : 102 0 102
693 }
694  
695 =head2 exception
696  
697 $exception = $process->exception;
698  
699 If the process exited due to an exception, returns the exception that was
700 thrown. Otherwise, returns C<undef>.
701  
702 =cut
703  
704 sub exception
705 {
706 9 ▶ my $self = shift;
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
707 9 return $self->{dollarat};
708 }
709  
710 =head2 errno
711  
712 $errno = $process->errno;
713  
714 If the process exited due to an exception, returns the numerical value of
715 C<$!> at the time the exception was thrown. Otherwise, returns C<undef>.
716  
717 =cut
718  
719 sub errno
720 {
721 1 ▶ my $self = shift;
Subroutine errno: called CC=1 SCAR=0.0
Pod documented
722 1 return $self->{dollarbang}+0;
723 }
724  
725 =head2 errstr
726  
727 $errstr = $process->errstr;
728  
729 If the process exited due to an exception, returns the string value of
730 C<$!> at the time the exception was thrown. Otherwise, returns C<undef>.
731  
732 =cut
733  
734 sub errstr
735 {
736 1 ▶ my $self = shift;
Subroutine errstr: called CC=1 SCAR=0.0
Pod documented
737 1 return $self->{dollarbang}."";
738 }
739  
740 =head2 fd
741  
742 $stream = $process->fd( $fd );
743  
744 Returns the L<IO::Async::Stream> or L<IO::Async::Socket> associated with the
745 given FD number. This must have been set up by a C<configure> argument prior
746 to adding the C<Process> object to the C<Loop>.
747  
748 The returned object have its read or write handle set to the other end of a
749 pipe or socket connected to that FD number in the child process. Typically,
750 this will be used to call the C<write> method on, to write more data into the
751 child, or to set an C<on_read> handler to read data out of the child.
752  
753 The C<on_closed> event for these streams must not be changed, or it will break
754 the close detection used by the C<Process> object and the C<on_finish> event
755 will not be invoked.
756  
757 =cut
758  
759 sub fd
760 {
761 435 ▶ my $self = shift;
Subroutine fd: called CC=10 SCAR=23.2
Pod documented
762 435 my ( $fd ) = @_;
763  
764 435 ▶ return $self->{fd_handle}{$fd} ||= do {
Condition66%2/3
$self->{"fd_handle"}{$fd} ||= do { my $opts = $self->{"fd_opts"}{$fd} or croak("$self does not have an fd Stream for $fd"); my $handle_class; if (defined $opts->{"socktype"} and "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1) { require IO::Async::Socket; $handle_class = "IO::Async::Socket"; } else { require IO::Async::Stream; $handle_class = "IO::Async::Stream"; } ; my $handle = $handle_class->new("notifier_name", $fd eq 0 ? "stdin" : ($fd eq 1 ? "stdout" : ($fd eq 2 ? "stderr" : ($fd eq "io" ? "stdio" : "fd$fd"))), %{$$opts{"handle"};}); if (defined $opts->{"from"}) { $handle->write($opts->{"from"}, "on_flush", sub { my($handle) = @_; $handle->close_write; } ); } ; $handle }
l!l&&r!l&&!r
2032320
MC/DC0%0/2
$self->{"fd_handle"}{$fd} ||= do { my $opts = $self->{"fd_opts"}{$fd} or croak("$self does not have an fd Stream for $fd"); my $handle_class; if (defined $opts->{"socktype"} and "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1) { require IO::Async::Socket; $handle_class = "IO::Async::Socket"; } else { require IO::Async::Stream; $handle_class = "IO::Async::Stream"; } ; my $handle = $handle_class->new("notifier_name", $fd eq 0 ? "stdin" : ($fd eq 1 ? "stdout" : ($fd eq 2 ? "stderr" : ($fd eq "io" ? "stdio" : "fd$fd"))), %{$$opts{"handle"};}); if (defined $opts->{"from"}) { $handle->write($opts->{"from"}, "on_flush", sub { my($handle) = @_; $handle->close_write; } ); } ; $handle }
$self->{"fd_handle"}{$fd} do { my $opts = $self->{"fd_opts"}{$fd} or croak("$self does not have an fd Stream for $fd"); my $handle_class; if (defined $opts->{"socktype"} and "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1) { require IO::Async::Socket; $handle_class = "IO::Async::Socket"; } else { require IO::Async::Stream; $handle_class = "IO::Async::Stream"; } ; my $handle = $handle_class->new("notifier_name", $fd eq 0 ? "stdin" : ($fd eq 1 ? "stdout" : ($fd eq 2 ? "stderr" : ($fd eq "io" ? "stdio" : "fd$fd"))), %{$$opts{"handle"};}); if (defined $opts->{"from"}) { $handle->write($opts->{"from"}, "on_flush", sub { my($handle) = @_; $handle->close_write; } ); } ; $handle }
Truth table66%2/3
A ||= B
A= $self->{"fd_handle"}{$fd}B= do { my $opts = $self->{"fd_opts"}{$fd} or croak("$self does not have an fd Stream for $fd"); my $handle_class; if (defined $opts->{"socktype"} and "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1) { require IO::Async::Socket; $handle_class = "IO::Async::Socket"; } else { require IO::Async::Stream; $handle_class = "IO::Async::Stream"; } ; my $handle = $handle_class->new("notifier_name", $fd eq 0 ? "stdin" : ($fd eq 1 ? "stdout" : ($fd eq 2 ? "stderr" : ($fd eq "io" ? "stdio" : "fd$fd"))), %{$$opts{"handle"};}); if (defined $opts->{"from"}) { $handle->write($opts->{"from"}, "on_flush", sub { my($handle) = @_; $handle->close_write; } ); } ; $handle }
ABresult
1X1
011
000
765 232 ▶ my $opts = $self->{fd_opts}{$fd} or
Branch: 1 branch
BranchTrueFalseTotal
my $opts = $self->{"fd_opts"}{$fd} or croak("$self does not have an fd Stream for $fd") 0 232 232
766 croak "$self does not have an fd Stream for $fd";
767  
768 232 my $handle_class;
769 232 ▶ if( defined $opts->{socktype} && IO::Async::OS->getsocktypebyname( $opts->{socktype} ) != SOCK_STREAM ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'socktype'} and 'IO::Async::OS'->getsocktypebyname($opts->{'socktype'}) != 1) { } 1 231 232
Condition66%2/3
defined $opts->{"socktype"} and "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1
!ll&&!rl&&r
23101
MC/DC50%1/2
defined $opts->{"socktype"} and "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1
defined $opts->{"socktype"} "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1
Truth table66%2/3
A and B
A= defined $opts->{"socktype"}B= "IO::Async::OS"->getsocktypebyname($opts->{"socktype"}) != 1
ABresult
0X0
100
111
770 1 require IO::Async::Socket;
771 1 $handle_class = "IO::Async::Socket";
772 }
773 else {
774 231 require IO::Async::Stream;
775 231 $handle_class = "IO::Async::Stream";
776 }
777  
778 my $handle = $handle_class->new(
779 notifier_name => $fd eq "0" ? "stdin" :
780 $fd eq "1" ? "stdout" :
781 $fd eq "2" ? "stderr" :
782 $fd eq "io" ? "stdio" : "fd$fd",
783 232 ▶ %{ $opts->{handle} },
Branch: 4 branches
BranchTrueFalseTotal
$fd eq '0' ? : 36 196 232
$fd eq '1' ? : 135 61 196
$fd eq '2' ? : 52 9 61
$fd eq 'io' ? : 9 0 9
784 );
785  
786 232 ▶ if( defined $opts->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"from"}) 23 209 232
787 $handle->write( $opts->{from},
788 on_flush => sub {
789 17 ▶ my ( $handle ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
790 17 $handle->close_write;
791 },
792 23 );
793 }
794  
795 $handle
796 232 };
797 }
798  
799 =head2 stdin
800  
801 =head2 stdout
802  
803 =head2 stderr
804  
805 =head2 stdio
806  
807 $stream = $process->stdin;
808  
809 $stream = $process->stdout;
810  
811 $stream = $process->stderr;
812  
813 $stream = $process->stdio;
814  
815 Shortcuts for calling C<fd> with 0, 1, 2 or C<io> respectively, to obtain the
816 L<IO::Async::Stream> representing the standard input, output, error, or
817 combined input/output streams of the child process.
818  
819 =cut
820  
821 64 ▶sub stdin { shift->fd( 0 ) }
Subroutine stdin: called CC=1 SCAR=0.0
Pod documented
822 80 ▶sub stdout { shift->fd( 1 ) }
Subroutine stdout: called CC=1 SCAR=0.0
Pod documented
823 26 ▶sub stderr { shift->fd( 2 ) }
Subroutine stderr: called CC=1 SCAR=0.0
Pod documented
824 33 ▶sub stdio { shift->fd( 'io' ) }
Subroutine stdio: called CC=1 SCAR=0.0
Pod documented
825  
826 =head1 EXAMPLES
827  
828 =head2 Capturing the STDOUT stream of a process
829  
830 By configuring the C<stdout> filehandle of the process using the C<into> key,
831 data written by the process can be captured.
832  
833 my $stdout;
834 my $process = IO::Async::Process->new(
835 command => [ "writing-program", "arguments" ],
836 stdout => { into => \$stdout },
837 on_finish => sub {
838 my $process = shift;
839 my ( $exitcode ) = @_;
840 print "Process has exited with code $exitcode, and wrote:\n";
841 print $stdout;
842 }
843 );
844  
845 $loop->add( $process );
846  
847 Note that until C<on_finish> is invoked, no guarantees are made about how much
848 of the data actually written by the process is yet in the C<$stdout> scalar.
849  
850 See also the C<run_child> method of L<IO::Async::Loop>.
851  
852 To handle data more interactively as it arrives, the C<on_read> key can
853 instead be used, to provide a callback function to invoke whenever more data
854 is available from the process.
855  
856 my $process = IO::Async::Process->new(
857 command => [ "writing-program", "arguments" ],
858 stdout => {
859 on_read => sub {
860 my ( $stream, $buffref ) = @_;
861 while( $$buffref =~ s/^(.*)\n// ) {
862 print "The process wrote a line: $1\n";
863 }
864  
865 return 0;
866 },
867 },
868 on_finish => sub {
869 print "The process has finished\n";
870 }
871 );
872  
873 $loop->add( $process );
874  
875 If the code to handle data read from the process isn't available yet when
876 the object is constructed, it can be supplied later by using the C<configure>
877 method on the C<stdout> filestream at some point before it gets added to the
878 Loop. In this case, C<stdin> should be configured using C<pipe_read> in the
879 C<via> key.
880  
881 my $process = IO::Async::Process->new(
882 command => [ "writing-program", "arguments" ],
883 stdout => { via => "pipe_read" },
884 on_finish => sub {
885 print "The process has finished\n";
886 }
887 );
888  
889 $process->stdout->configure(
890 on_read => sub {
891 my ( $stream, $buffref ) = @_;
892 while( $$buffref =~ s/^(.*)\n// ) {
893 print "The process wrote a line: $1\n";
894 }
895  
896 return 0;
897 },
898 );
899  
900 $loop->add( $process );
901  
902 =head2 Sending data to STDIN of a process
903  
904 By configuring the C<stdin> filehandle of the process using the C<from> key,
905 data can be written into the C<STDIN> stream of the process.
906  
907 my $process = IO::Async::Process->new(
908 command => [ "reading-program", "arguments" ],
909 stdin => { from => "Here is the data to send\n" },
910 on_finish => sub {
911 print "The process has finished\n";
912 }
913 );
914  
915 $loop->add( $process );
916  
917 The data in this scalar will be written until it is all consumed, then the
918 handle will be closed. This may be useful if the program waits for EOF on
919 C<STDIN> before it exits.
920  
921 To have the ability to write more data into the process once it has started.
922 the C<write> method on the C<stdin> stream can be used, when it is configured
923 using the C<pipe_write> value for C<via>:
924  
925 my $process = IO::Async::Process->new(
926 command => [ "reading-program", "arguments" ],
927 stdin => { via => "pipe_write" },
928 on_finish => sub {
929 print "The process has finished\n";
930 }
931 );
932  
933 $loop->add( $process );
934  
935 $process->stdin->write( "Here is some more data\n" );
936  
937 =head2 Setting socket options
938  
939 By using the C<prefork> code block you can change the socket receive buffer
940 size at both ends of the socket before the child is forked (at which point it
941 would be too late for the parent to be able to change the child end of the
942 socket).
943  
944 use Socket qw( SOL_SOCKET SO_RCVBUF );
945  
946 my $process = IO::Async::Process->new(
947 command => [ "command-to-read-from-and-write-to", "arguments" ],
948 stdio => {
949 via => "socketpair",
950 prefork => sub {
951 my ( $parentfd, $childfd ) = @_;
952  
953 # Set parent end of socket receive buffer to 3 MB
954 $parentfd->setsockopt(SOL_SOCKET, SO_RCVBUF, 3 * 1024 * 1024);
955 # Set child end of socket receive buffer to 3 MB
956 $childfd ->setsockopt(SOL_SOCKET, SO_RCVBUF, 3 * 1024 * 1024);
957 },
958 },
959 );
960  
961 $loop->add( $process );
962  
963 =cut
964  
965 =head1 AUTHOR
966  
967 Paul Evans <leonerd@leonerd.org.uk>
968  
969 =cut
970  
971 40 0x55AA;
« blib/lib/IO/Async/Stream.pm ↑ Summary blib/lib/IO/Async/Routine.pm »