File Coverage

blib/lib/Mojo/IOLoop/ReadWriteFork.pm
Criterion Covered Total %
statement 173 201 86.0
branch 54 90 60.0
condition 22 58 37.9
subroutine 38 43 88.3
pod 8 8 100.0
total 295 400 73.7


line stmt bran cond sub pod time code
1             package Mojo::IOLoop::ReadWriteFork;
2 23     23   4161512 use Mojo::Base 'Mojo::EventEmitter';
  23         246  
  23         106  
3              
4 23     23   34483 use Errno qw(EAGAIN ECONNRESET EINTR EPIPE EWOULDBLOCK EIO);
  23         20941  
  23         1886  
5 23     23   129 use IO::Handle;
  23         41  
  23         593  
6 23     23   7500 use IO::Pty;
  23         190404  
  23         1132  
7 23     23   7695 use Mojo::Asset::Memory;
  23         530674  
  23         180  
8 23     23   9068 use Mojo::IOLoop;
  23         2048136  
  23         128  
9 23     23   887 use Mojo::IOLoop::Stream;
  23         46  
  23         126  
10 23     23   7758 use Mojo::IOLoop::ReadWriteFork::SIGCHLD;
  23         55  
  23         3552  
11 23     23   683 use Mojo::Promise;
  23         34  
  23         116  
12 23     23   468 use Mojo::Util qw(term_escape);
  23         41  
  23         914  
13 23     23   98 use Scalar::Util qw(blessed);
  23         41  
  23         882  
14              
15 23   100 23   100 use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;
  23         38  
  23         1377  
16 23   50 23   114 use constant DEBUG => $ENV{MOJO_READWRITEFORK_DEBUG} && 1;
  23         36  
  23         62757  
17              
18             our $VERSION = '2.01';
19              
20             our @SAFE_SIG
21             = grep { !m!^(NUM\d+|__[A-Z0-9]+__|ALL|CATCHALL|DEFER|HOLD|IGNORE|MAX|PAUSE|RTMAX|RTMIN|SEGV|SETS)$! } keys %SIG;
22              
23             my $SIGCHLD = Mojo::IOLoop::ReadWriteFork::SIGCHLD->singleton;
24              
25             has conduit => sub { +{type => 'pipe'} };
26 8 100   8 1 227 sub pid { shift->{pid} || 0; }
27             has ioloop => sub { Mojo::IOLoop->singleton }, weak => 1;
28              
29             sub close {
30 3     3 1 151 my $self = shift;
31 3 50       30 my $what = $_[0] eq 'stdout' ? 'stdout_read' : 'stdin_write'; # stdout_read is EXPERIMENTAL
32 3 50       34 my $fh = delete $self->{$what} or return $self;
33 3 50       30 $fh->close or $self->emit(error => $!);
34 3         57 $self;
35             }
36              
37             sub run {
38 32 100   32 1 3681 my $args = ref $_[-1] eq 'HASH' ? pop : {};
39 32         101 my ($self, $program, @program_args) = @_;
40 32         323 return $self->start({%$args, program => $program, program_args => \@program_args});
41             }
42              
43             sub run_and_capture_p {
44 1     1 1 156 my $self = shift;
45 1         9 my $asset = Mojo::Asset::Memory->new(auto_upgrade => 1);
46 1     2   9 my $read_cb = $self->on(read => sub { $asset->add_chunk($_[1]) });
  2         27  
47 1     1   9 $asset->once(upgrade => sub { $asset = $_[1]; $self->emit(asset => $asset) });
  1         178  
  1         12  
48 1     1   87 return $self->emit(asset => $asset)->run_p(@_)->then(sub {$asset})
49 1     1   17 ->finally(sub { $self->unsubscribe(read => $read_cb) });
  1         267  
50             }
51              
52             sub run_p {
53 9     9 1 5978 my $self = shift;
54 9         65 my $p = Mojo::Promise->new;
55 9         350 my @cb;
56 9     0   69 push @cb, $self->once(error => sub { shift->unsubscribe(finish => $cb[1]); $p->reject(@_) });
  0         0  
  0         0  
57 9     8   175 push @cb, $self->once(finish => sub { shift->unsubscribe(error => $cb[0]); $p->resolve(@_) });
  8         309  
  8         268  
58 9         451 $self->run(@_);
59 9         54 return $p;
60             }
61              
62             sub start {
63 38     38 1 4339 my $self = shift;
64 38 100       214 my $args = ref $_[0] ? $_[0] : {@_};
65 38         151 my $conduit = $self->conduit;
66              
67 38   33     430 $args->{$_} //= $conduit->{$_} for keys %$conduit;
68 38   66     226 $args->{conduit} ||= delete $args->{type};
69 38   100     564 $args->{env} ||= {%ENV};
70 38         98 $self->{errno} = 0;
71 38 100       136 $args->{program} or die 'program is required input';
72 36     36   216 $self->ioloop->next_tick(sub { $self->_start($args) });
  36         4397  
73 36         2702 return $self;
74             }
75              
76             sub _start {
77 36     36   87 my ($self, $args) = @_;
78 36         62 my %fh;
79              
80 36 100       150 if ($args->{conduit} eq 'pipe') {
    50          
    0          
81 11         50 @fh{qw(stdin_read stdin_write)} = $self->_pipe;
82 11         37 @fh{qw(stdout_read stdout_write)} = $self->_pipe;
83             }
84             elsif ($args->{conduit} eq 'pty') {
85 25         334 $fh{stdin_write} = $fh{stdout_read} = IO::Pty->new;
86             }
87             elsif ($args->{conduit} eq 'pty3') {
88 0   0     0 $args->{$_} //= 1 for qw(stdin stdout stderr);
89 0         0 @fh{stdin_write} = IO::Pty->new;
90 0         0 @fh{qw(stdout_read stdout_write)} = $self->_pipe;
91             }
92             else {
93 0         0 warn "[RWF] Invalid conduit ($args->{conduit})\n" if DEBUG;
94 0         0 return $self->emit(error => "Invalid conduit ($args->{conduit})");
95             }
96              
97 36 100       14411 @fh{qw(stderr_read stderr_write)} = $self->_pipe if $args->{stderr};
98              
99 36         284 $self->emit(before_fork => \%fh); # LEGACY
100 36         519 $self->emit(prepare => \%fh);
101              
102 36 50       75136 return $self->emit(error => "Couldn't fork ($!)") unless defined($self->{pid} = fork);
103 36 100       3376 return $self->{pid} ? $self->_start_parent($args, \%fh) : $self->_start_child($args, \%fh);
104             }
105              
106             sub _start_child {
107 3     3   67 my ($self, $args, $fh) = @_;
108              
109 3 100 66     380 if (my $pty = blessed $fh->{stdin_write} && $fh->{stdin_write}->isa('IO::Pty') && $fh->{stdin_write}) {
110 2         67 $pty->make_slave_controlling_terminal;
111 2         1602 $fh->{stdin_read} = $pty->slave;
112 2 50       80 $fh->{stdin_read}->set_raw if $args->{raw};
113 2 50       54 $fh->{stdin_read}->clone_winsize_from($args->{clone_winsize_from}) if $args->{clone_winsize_from};
114 2   33     44 $fh->{stdout_write} ||= $fh->{stdin_read};
115             }
116              
117 3   33     162 my $stdout_no = ($args->{stdout} // 1) && fileno($fh->{stdout_write});
118 3   33     145 my $stderr_no = ($args->{stderr} // 1) && fileno($fh->{stderr_write} || $fh->{stdout_write});
119 3 50       1273 open STDIN, '<&' . fileno($fh->{stdin_read}) or exit $!;
120 3 50 33     95 open STDOUT, '>&' . $stdout_no or exit $! if $stdout_no;
121 3 50 33     114 open STDERR, '>&' . $stderr_no or exit $! if $stderr_no;
122 3 50       183 $stdout_no ? STDOUT->autoflush(1) : STDOUT->close;
123 3 50       304 $stderr_no ? STDERR->autoflush(1) : STDERR->close;
124              
125 3         164 $fh->{stdin_write}->close;
126 3         57 $fh->{stdout_read}->close;
127 3 50       30 $fh->{stderr_read}->close if $fh->{stderr_read};
128              
129 3         18 %ENV = %{$args->{env}};
  3         663  
130              
131 3         18 my $errno;
132 3 50       40 if (ref $args->{program} eq 'CODE') {
133 0         0 $! = 0;
134 0         0 @SIG{@SAFE_SIG} = ('DEFAULT') x @SAFE_SIG;
135 0 0       0 eval { $args->{program}->(@{$args->{program_args} || []}); };
  0         0  
  0         0  
136 0 0       0 $errno = $@ ? 255 : $!;
137 0 0       0 print STDERR $@ if length $@;
138             }
139             else {
140 3 50       17 exec $args->{program}, @{$args->{program_args} || []};
  3         0  
141             }
142              
143 0   0     0 eval { POSIX::_exit($errno // $!); };
  0         0  
144 0   0     0 exit($errno // $!);
145             }
146              
147             sub _start_parent {
148 33     33   746 my ($self, $args, $fh) = @_;
149              
150 33         156 $self->_d("Forked $args->{program} @{$args->{program_args} || []}") if DEBUG;
151 33         1554 @$self{qw(stdin_write stdout_read stderr_read)} = @$fh{qw(stdin_write stdout_read stderr_read)};
152 33         490 @$self{qw(wait_eof wait_sigchld)} = (1, 1);
153              
154 33 100 66     2343 $fh->{stdout_read}->close_slave if blessed $fh->{stdout_read} and $fh->{stdout_read}->isa('IO::Pty');
155 33 50       2381 $self->_stream(pty => $fh->{stdin_write}) if $args->{conduit} eq 'pty3';
156 33 100       332 $self->_stream(stderr => $fh->{stderr_read}) if $fh->{stderr_read};
157 33 100 100     1481 $self->_stream(stdout => $fh->{stdout_read}) if !$fh->{stderr_read} or $args->{stdout};
158              
159 33     32   23357 $SIGCHLD->waitpid($self->{pid} => sub { $self->_sigchld(@_) });
  32         235  
160 33         6277 $self->emit('fork'); # LEGACY
161 33         3692 $self->emit('spawn');
162 33         1873 $self->_write;
163             }
164              
165             sub write {
166 6     6 1 656 my ($self, $chunk, $cb) = @_;
167 6 100       46 $self->once(drain => $cb) if $cb;
168 6         64 $self->{stdin_buffer} .= $chunk;
169 6 100       29 $self->_write if $self->{stdin_write};
170 6         46 return $self;
171             }
172              
173             sub kill {
174 0     0 1 0 my $self = shift;
175 0   0     0 my $signal = shift // 15;
176 0 0       0 return undef unless my $pid = $self->{pid};
177 0         0 $self->_d("kill $signal $pid") if DEBUG;
178 0         0 return kill $signal, $pid;
179             }
180              
181             sub _error {
182 0 0 0 0   0 return if $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK;
      0        
183 0 0 0     0 return $_[0]->kill if $! == ECONNRESET || $! == EPIPE;
184 0         0 return $_[0]->emit(error => $!)->kill;
185             }
186              
187 0     0   0 sub _d { warn "-- [$_[0]->{pid}] $_[1]\n" }
188              
189             sub _maybe_terminate {
190 65     65   261 my ($self, $pending_event) = @_;
191 65         252 delete $self->{$pending_event};
192 65 100 66     663 return if $self->{wait_eof} or $self->{wait_sigchld};
193              
194 32         383 delete $self->{stdin_write};
195 32         1832 delete $self->{stdout_read};
196              
197 32         139 my @errors;
198 32         105 for my $cb (@{$self->subscribers('close')}, @{$self->subscribers('finish')}) {
  32         329  
  32         367  
199 38 100       1178 push @errors, $@ unless eval { $self->$cb(@$self{qw(exit_value signal)}); 1 };
  38         414  
  37         5140  
200             }
201              
202 32         928 $self->emit(error => $_) for @errors;
203             }
204              
205             sub _pipe {
206 24     24   41 my $self = shift;
207 24 50       649 pipe my $read, my $write or return $self->emit(error => "pipe: $!");
208 24         194 $write->autoflush(1);
209 24         931 return $read, $write;
210             }
211              
212             sub _sigchld {
213 32     32   118 my ($self, $status, $pid) = @_;
214 32         172 my ($exit_value, $signal) = ($status >> 8, $status & 127);
215 32         70 $self->_d("Exit exit_value=$exit_value, signal=$signal") if DEBUG;
216 32         228 @$self{qw(exit_value signal)} = ($exit_value, $signal);
217 32         153 $self->_maybe_terminate('wait_sigchld');
218             }
219              
220             sub _stream {
221 34     34   339 my ($self, $conduit, $handle) = @_;
222 34         2301 my $stream = Mojo::IOLoop::Stream->new($handle)->timeout(0);
223              
224 34 100       10787 my $event_name = $self->{stderr_read} ? $conduit : 'read';
225             my $read_cb = sub {
226 53     53   127458 $self->_d(sprintf ">>> RWF:%s ($event_name)\n%s", uc $conduit, term_escape $_[1]) if DEBUG;
227 53         330 $self->emit($event_name => $_[1]);
228 34         1175 };
229              
230 34 50   22   1420 $stream->on(error => sub { $! != EIO && $self->emit(error => "Read error: $_[1]") });
  22         46514  
231 34     33   1228 $stream->on(close => sub { $self->_maybe_terminate('wait_eof') });
  33         33906  
232 34         551 $stream->on(read => $read_cb);
233 34         720 $self->ioloop->stream($stream);
234             }
235              
236             sub _write {
237 35     35   76 my $self = shift;
238 35 100       2589 return unless length $self->{stdin_buffer};
239              
240 6         22 my $stdin_write = $self->{stdin_write};
241 6         152 my $written = $stdin_write->syswrite($self->{stdin_buffer});
242 6 50       317 return $self->_error unless defined $written;
243              
244 6         34 my $chunk = substr $self->{stdin_buffer}, 0, $written, '';
245 6         16 $self->_d(sprintf "<<< RWF:STDIN\n%s", term_escape $chunk) if DEBUG;
246              
247 6 50       26 if (length $self->{stdin_buffer}) {
248              
249             # This is one ugly hack because it does not seem like IO::Pty play
250             # nice with Mojo::Reactor(::EV) ->io(...) and ->watch(...)
251 0 0   0   0 $self->ioloop->timer(0.01 => sub { $self and $self->_write });
  0         0  
252             }
253             else {
254 6         52 $self->emit('drain');
255             }
256             }
257              
258             1;
259              
260             =encoding utf8
261              
262             =head1 NAME
263              
264             Mojo::IOLoop::ReadWriteFork - Fork a process and read/write from it
265              
266             =head1 VERSION
267              
268             2.01
269              
270             =head1 SYNOPSIS
271              
272             use Mojo::Base -strict, -signatures;
273             my $rwf = Mojo::IOLoop::ReadWriteFork->new;
274              
275             # Emitted if something terrible happens
276             $rwf->on(error => sub ($rwf, $error) { warn $error });
277              
278             # Emitted when the child completes
279             $rwf->on(finish => sub ($rwf, $exit_value, $signal) { Mojo::IOLoop->stop; });
280              
281             # Emitted when the child prints to STDOUT or STDERR
282             $rwf->on(read => sub ($rwf, $buf) { print qq(Child process sent us "$buf") });
283              
284             # Need to set "conduit" for bash, ssh, and other programs that require a pty
285             $rwf->conduit({type => 'pty'});
286              
287             # Start the application
288             $rwf->run('bash', -c => q(echo $YIKES foo bar baz));
289              
290             # Using promises
291             $rwf->on(read => sub ($rwf, $buf) { ... });
292             $rwf->run_p('bash', -c => q(echo $YIKES foo bar baz))->wait;
293              
294             See also
295             L
296             for an example usage from a L.
297              
298             =head1 DESCRIPTION
299              
300             L enable you to fork a child process and L
301             and L data to. You can also L to the child and see
302             when the process ends. The child process can be an external program (bash,
303             telnet, ffmpeg, ...) or a CODE block running perl.
304              
305             =head2 Conduits
306              
307             L can write to STDIN or a L object, and
308             read from STDOUT or STDERR, depending on the "type" given to L.
309              
310             Here is an overview of the different conduits:
311              
312             =over 2
313              
314             =item * pipe
315              
316             The "pipe" type will create a STDIN and a STDOUT conduit using a plain pipe.
317             Passing in C will also create a seperate pipe for STDERR.
318              
319             $rwf->conduit({type => 'pipe'});
320             $rwf->conduit({type => 'pipe', stderr => 1});
321             $rwf->write('some data'); # write to STDIN
322             $rwf->on(read => sub { ... }); # STDOUT and STDERR
323             $rwf->on(stdout => sub { ... }); # STDOUT
324             $rwf->on(stderr => sub { ... }); # STDERR
325              
326             This is useful if you want to run a program like "cat" that simply read/write
327             from STDIN, STDERR or STDOUT.
328              
329             =item * pty
330              
331             The "pty" type will create a STDIN and a STDOUT conduit using L.
332             Passing in "stderr" will also create a seperate pipe for STDERR.
333              
334             $rwf->conduit({type => 'pty'});
335             $rwf->conduit({type => 'pty', stderr => 1});
336             $rwf->write('some data'); # write to STDIN
337             $rwf->on(read => sub { ... }); # STDOUT and STDERR
338             $rwf->on(stdout => sub { ... }); # STDOUT
339             $rwf->on(stderr => sub { ... }); # STDERR
340              
341             The difference between "pipe" and "pty" is that a L object will be
342             used for STDIN and STDOUT instead of a plain pipe. In addition, it is possible
343             to pass in C and C:
344              
345             $rwf->conduit({type => 'pty', clone_winsize_from => \*STDOUT, raw => 1});
346              
347             This is useful if you want to run "bash" or another program that requires a
348             pseudo terminal.
349              
350             =item * pty3
351              
352             The "pty3" type will create a STDIN, a STDOUT, a STDERR and a PTY conduit.
353              
354             $rwf->conduit({type => 'pty3'});
355             $rwf->write('some data'); # write to STDIN/PTY
356             $rwf->on(pty => sub { ... }); # PTY
357             $rwf->on(stdout => sub { ... }); # STDOUT
358             $rwf->on(stderr => sub { ... }); # STDERR
359              
360             The difference between "pty" and "pty3" is that there will be a different
361             L event for bytes coming from the pseudo PTY. This type also supports
362             "clone_winsize_from" and "raw".
363              
364             $rwf->conduit({type => 'pty3', clone_winsize_from => \*STDOUT, raw => 1});
365              
366             This is useful if you want to run "ssh" or another program that sends password
367             prompts (or other output) on the PTY channel. See
368             L
369             for an example application.
370              
371             =back
372              
373             =head1 EVENTS
374              
375             =head2 asset
376              
377             $rwf->on(asset => sub ($rwf, $asset) { ... });
378              
379             Emitted at least once when calling L. C<$asset> can be
380             either a L or L object.
381              
382             $rwf->on(asset => sub ($rwf, $asset) {
383             # $asset->auto_upgrade(1) is set by default
384             $asset->max_memory_size(1) if $asset->can('max_memory_size');
385             });
386              
387             =head2 drain
388              
389             $rwf->on(drain => sub ($rwf) { ... });
390              
391             Emitted when the buffer has been written to the sub process.
392              
393             =head2 error
394              
395             $rwf->on(error => sub ($rwf, $str) { ... });
396              
397             Emitted when when the there is an issue with creating, writing or reading
398             from the child process.
399              
400             =head2 finish
401              
402             $rwf->on(finish => sub ($rwf, $exit_value, $signal) { ... });
403              
404             Emitted when the child process exit.
405              
406             =head2 pty
407              
408             $rwf->on(pty => sub ($rwf, $buf) { ... });
409              
410             Emitted when the child has written a chunk of data to a pty and L has
411             "type" set to "pty3".
412              
413             =head2 prepare
414              
415             $rwf->on(prepare => sub ($rwf, $fh) { ... });
416              
417             Emitted right before the child process is forked. C<$fh> can contain the
418             example hash below or a subset:
419              
420             $fh = {
421             stderr_read => $pipe_fh_w_or_pty_object,
422             stderr_read => $stderr_fh_r,
423             stdin_read => $pipe_fh_r,
424             stdin_write => $pipe_fh_r_or_pty_object,
425             stdin_write => $stderr_fh_w,
426             stdout_read => $pipe_fh_w_or_pty_object,
427             stdout_read => $stderr_fh_r,
428             stdout_write => $pipe_fh_w,
429             };
430              
431             =head2 read
432              
433             $rwf->on(read => sub ($rwf, $buf) { ... });
434              
435             Emitted when the child has written a chunk of data to STDOUT or STDERR, and
436             neither "stderr" nor "stdout" is set in the L.
437              
438             =head2 spawn
439              
440             $rwf->on(spawn => sub ($rwf) { ... });
441              
442             Emitted after C has been called. Note that the child process might not yet have
443             been started. The order of things is impossible to say, but it's something like this:
444              
445             .------.
446             | fork |
447             '------'
448             |
449             ___/ \_______________
450             | |
451             | (parent) | (child)
452             .--------------. |
453             | emit "spawn" | .--------------------.
454             '--------------' | set up filehandles |
455             '--------------------'
456             |
457             .---------------.
458             | exec $program |
459             '---------------'
460              
461             See also L for example usage of this event.
462              
463             =head2 stderr
464              
465             $rwf->on(stderr => sub ($rwf, $buf) { ... });
466              
467             Emitted when the child has written a chunk of data to STDERR and L
468             has the "stderr" key set to a true value or "type" is set to "pty3".
469              
470             =head2 stdout
471              
472             $rwf->on(stdout => sub ($rwf, $buf) { ... });
473              
474             Emitted when the child has written a chunk of data to STDOUT and L
475             has the "stdout" key set to a true value or "type" is set to "pty3".
476              
477             =head1 ATTRIBUTES
478              
479             =head2 conduit
480              
481             $hash = $rwf->conduit;
482             $rwf = $rwf->conduit(\%options);
483              
484             Used to set the conduit options. Possible values are:
485              
486             =over 2
487              
488             =item * clone_winsize_from
489              
490             See L. This only makes sense if L is set
491             to "pty". This can also be specified by using the L attribute.
492              
493             =item * raw
494              
495             See L. This only makes sense if L is set to "pty".
496             This can also be specified by using the L attribute.
497              
498             =item * stderr
499              
500             This will make L emit "stderr" events, instead of
501             "read" events. Setting this to "0" will close STDERR in the child.
502              
503             =item * stdout
504              
505             This will make L emit "stdout" events, instead of
506             "read" events. Setting this to "0" will close STDOUT in the child.
507              
508             =item * type
509              
510             "type" can be either "pipe", "pty" or "pty3". Default value is "pipe".
511              
512             See also L
513              
514             =back
515              
516             =head2 ioloop
517              
518             $ioloop = $rwf->ioloop;
519             $rwf = $rwf->ioloop(Mojo::IOLoop->singleton);
520              
521             Holds a L object.
522              
523             =head2 pid
524              
525             $int = $rwf->pid;
526              
527             Holds the child process ID. Note that L will start the process after
528             the IO loop is started. This means that the code below will not work:
529              
530             $rwf->run("bash", -c => q(echo $YIKES foo bar baz));
531             warn $rwf->pid; # pid() is not yet set
532              
533             This will work though:
534              
535             $rwf->on(fork => sub ($rwf) { warn $rwf->pid });
536             $rwf->run('bash', -c => q(echo $YIKES foo bar baz));
537              
538             =head1 METHODS
539              
540             =head2 close
541              
542             $rwf = $rwf->close('stdin');
543              
544             Close STDIN stream to the child process immediately.
545              
546             =head2 run
547              
548             $rwf = $rwf->run($program, @program_args);
549             $rwf = $rwf->run(\&Some::Perl::function, @function_args);
550              
551             Simpler version of L. Can either start an application or run a perl
552             function.
553              
554             =head2 run_and_capture_p
555              
556             $p = $rwf->run_and_capture_p(...)->then(sub { my $asset = shift });
557              
558             L takes the same arguments as L, but the
559             fullfillment callback will receive a L object that holds the
560             output from the command.
561              
562             See also the L event.
563              
564             =head2 run_p
565              
566             $p = $rwf->run_p($program, @program_args);
567             $p = $rwf->run_p(\&Some::Perl::function, @function_args);
568              
569             Promise based version of L. The L will be resolved on
570             L and rejected on L.
571              
572             =head2 start
573              
574             $rwf = $rwf->start(\%args);
575              
576             Used to fork and exec a child process. C<%args> can have:
577              
578             =over 2
579              
580             =item * program
581              
582             Either an application or a CODE ref.
583              
584             =item * program_args
585              
586             A list of options passed on to L or as input to the CODE ref.
587              
588             Note that this module will start L with this code:
589              
590             exec $program, @$program_args;
591              
592             This means that the code is subject for
593             L
594             unless invoked with more than one argument. This is considered a feature, but
595             something you should be avare of. See also L for more details.
596              
597             =item * env
598              
599             Passing in C will override the default set of environment variables,
600             stored in C<%ENV>.
601              
602             =back
603              
604             =head2 write
605              
606             $rwf = $rwf->write($chunk);
607             $rwf = $rwf->write($chunk, $cb);
608              
609             Used to write data to the child process STDIN. An optional callback will be
610             called once the C<$chunk> is written.
611              
612             Example:
613              
614             $rwf->write("some data\n", sub ($rwf) { $rwf->close });
615              
616             =head2 kill
617              
618             $bool = $rwf->kill;
619             $bool = $rwf->kill(15); # default
620              
621             Used to signal the child.
622              
623             =head1 SEE ALSO
624              
625             L.
626              
627             L
628              
629             =head1 COPYRIGHT AND LICENSE
630              
631             Copyright (C) 2013-2016, Jan Henning Thorsen
632              
633             This program is free software, you can redistribute it and/or modify it under
634             the terms of the Artistic License version 2.0.
635              
636             =head1 AUTHOR
637              
638             Jan Henning Thorsen - C
639              
640             =cut