IO/System.pm

filter: Statementstmt 21.4% 12 / 56 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 4 / 8 Podpod 100% 3 / 3 totaltotal 16.9% 19 / 112 CC 16 SCAR 52.5
CC
16
Cov
13%
run
272.0
__ANON__
2.0
system
2.0
CRAP 189.7
↑ Summary IO.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, 2023-2026 -- leonerd@leonerd.org.uk
5  
6 package Future::IO::System 0.23;
7  
8 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<Future::IO::System> - C<system()>-like methods for L<Future::IO>
18  
19 =head1 SYNOPSIS
20  
21 =for highlighter language=perl
22  
23 use Future::IO;
24 use Future::IO::System;
25  
26 my $f = Future::IO::System->system( "cmd", "args go", "here" );
27 # $f will become done when the command completes
28  
29 my $f = Future::IO::System->system_out( "cmd", "-v" );
30 my ( $status, $out ) = $f->get;
31  
32 # $status will contain the exit code and $out will contain what it wrote
33 # to STDOUT
34  
35 =head1 DESCRIPTION
36  
37 This package contains a selection of methods that behave like the core
38 C<system()> and related functions, running asynchronously via L<Future::IO>.
39  
40 In particular, the L</system> behaves somewhat like C<CORE::system()> and
41 L</system_out> behaves somewhat like L<qx()>.
42  
43 =head2 Portability
44  
45 In order for this module to work at all, the underlying C<Future::IO>
46 implementation must support the L<Future::IO/waitpid> method. The default
47 minimal implementation included with the module does not, but most of the
48 additional implementations from CPAN will.
49  
50 In addition, the operation of this module uses techniques that only really
51 work on full POSIX systems (such as Linux, Mac OS X, the various BSDs, etc).
52 It is unlikely to work in places like MSWin32.
53  
54 =cut
55  
56 # TODO: Print at least some sort of warning if loaded on one of the weird
57 # non-POSIX OSes
58  
59 =head1 METHODS
60  
61 =cut
62  
63 =head2 run
64  
65 ( $exitcode, ... ) = await Future::IO::System->run(
66 argv => [ $path, @args ],
67 ...
68 );
69  
70 I<Since version 0.12.>
71  
72 Runs the given C<$path> with the given C<@args> as a sub-process, optionally
73 with some additional filehandles set up as determined by the other arguments.
74 The returned L<Future> will yield the C<waitpid()> exit code from the process
75 when it terminates, and optionally the bytes read from the other filehandles
76 that were set up.
77  
78 Takes the following named arguments
79  
80 =over 4
81  
82 =item argv => ARRAY
83  
84 An array reference containing the path and arguments to pass to C<exec()> in
85 the child process.
86  
87 =item in => STRING
88  
89 If defined, create a pipe and assign the reading end to the child process's
90 STDIN filehandle. The given string will then be written to the pipe, after
91 which the pipe will be closed.
92  
93 =item want_out => BOOL
94  
95 If true, create a pipe and assign the writing end to the child process's
96 STDOUT filehandle. The returned future will additionally contain all the bytes
97 read from it until EOF.
98  
99 =item want_err => BOOL
100  
101 If true, create a pipe and assign the writing end to the child process's
102 STDERR filehandle. The returned future will additionally contain all the bytes
103 read from it until EOF.
104  
105 =back
106  
107 The remaining methods in this class are simplified wrappers of this one.
108  
109 =cut
110  
111 sub run
112 0 ▶{
Subroutine run: not called CC=16 SCAR=56.1
Pod documented
113 0 shift;
114 0 my %params = @_;
115  
116 0 require POSIX;
117  
118 0 my $argv = $params{argv};
119 0 my $want_in = defined $params{in};
120 0 my $want_out = $params{want_out};
121 0 my $want_err = $params{want_err};
122  
123 0 my @infh;
124 0 ▶ pipe( $infh[0], $infh[1] ) or croak "Cannot pipe() - $!"
Branch: 1 branch
BranchTrueFalseTotal
if $want_in 0 0 0
Condition0%0/5
$want_in && (pipe($infh[0], $infh[1]) || croak("Cannot pipe() - $!"))
!ll&&!rl&&r
000
pipe $infh[0], $infh[1] or croak("Cannot pipe() - $!")
l!l
00
MC/DC0%0/2
$want_in && (pipe($infh[0], $infh[1]) || croak("Cannot pipe() - $!"))
$want_in pipe $infh[0], $infh[1]
Truth table0%0/3
A && B
A= $want_inB= pipe $infh[0], $infh[1]
ABresult
0X0
100
111
125 if $want_in;
126  
127 0 my @outfh;
128 0 ▶ pipe( $outfh[0], $outfh[1] ) or croak "Cannot pipe() - $!"
Branch: 1 branch
BranchTrueFalseTotal
if $want_out 0 0 0
Condition0%0/5
$want_out && (pipe($outfh[0], $outfh[1]) || croak("Cannot pipe() - $!"))
!ll&&!rl&&r
000
pipe $outfh[0], $outfh[1] or croak("Cannot pipe() - $!")
l!l
00
MC/DC0%0/2
$want_out && (pipe($outfh[0], $outfh[1]) || croak("Cannot pipe() - $!"))
$want_out pipe $outfh[0], $outfh[1]
Truth table0%0/3
A && B
A= $want_outB= pipe $outfh[0], $outfh[1]
ABresult
0X0
100
111
129 if $want_out;
130  
131 0 my @errfh;
132 0 ▶ pipe( $errfh[0], $errfh[1] ) or croak "Cannot pipe() - $!"
Branch: 1 branch
BranchTrueFalseTotal
if $want_err 0 0 0
Condition0%0/5
$want_err && (pipe($errfh[0], $errfh[1]) || croak("Cannot pipe() - $!"))
!ll&&!rl&&r
000
pipe $errfh[0], $errfh[1] or croak("Cannot pipe() - $!")
l!l
00
MC/DC0%0/2
$want_err && (pipe($errfh[0], $errfh[1]) || croak("Cannot pipe() - $!"))
$want_err pipe $errfh[0], $errfh[1]
Truth table0%0/3
A && B
A= $want_errB= pipe $errfh[0], $errfh[1]
ABresult
0X0
100
111
133 if $want_err;
134  
135 0 ▶ defined( my $pid = fork() )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or croak("Cannot fork() - $!") 0 0 0
136 or croak "Cannot fork() - $!";
137  
138 0 ▶ if( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
139 # parent
140  
141 0 my @f;
142 0 push @f, Future::IO->waitpid( $pid );
143  
144 0 ▶ if( $want_in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_in) 0 0 0
145 0 close $infh[0];
146 push @f, Future::IO->syswrite_exactly( $infh[1], $params{in} )
147 0 ▶ ->then( sub { Future->done() } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 }
149  
150 0 ▶ if( $want_out ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_out) 0 0 0
151 0 close $outfh[1];
152 0 push @f, Future::IO->read_until_eof( $outfh[0] );
153 }
154  
155 0 ▶ if( $want_err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_err) 0 0 0
156 0 close $errfh[1];
157 0 push @f, Future::IO->read_until_eof( $errfh[0] );
158 }
159  
160 0 return Future->needs_all( @f );
161 }
162 else {
163 # child
164  
165 0 ▶ if( $want_in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_in) 0 0 0
166 0 close $infh[1];
167 0 POSIX::dup2( $infh[0]->fileno, 0 );
168 }
169  
170 0 ▶ if( $want_out ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_out) 0 0 0
171 0 close $outfh[0];
172 0 POSIX::dup2( $outfh[1]->fileno, 1 );
173 }
174  
175 0 ▶ if( $want_err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_err) 0 0 0
176 0 close $errfh[0];
177 0 POSIX::dup2( $errfh[1]->fileno, 2 );
178 }
179  
180 0 ▶ exec( @$argv ) or
Branch: 1 branch
BranchTrueFalseTotal
exec @$argv or POSIX::_exit(-1) 0 0 0
181 POSIX::_exit( -1 );
182 }
183 }
184  
185 =head2 system
186  
187 $exitcode = await Future::IO::System->system( $path, @args );
188  
189 I<Since version 0.12.>
190  
191 Runs the given C<$path> with the given C<@args> as a sub-process with no extra
192 filehandles.
193  
194 =cut
195  
196 sub system
197 0 ▶{
Subroutine system: not called CC=1 SCAR=6.9
Pod documented
198 0 my $self = shift;
199 0 my @argv = @_;
200  
201 0 return $self->run( argv => \@argv );
202 }
203  
204 =head2 system_out
205  
206 ( $exitcode, $out ) = await Future::IO::System->system_out( $path, @args );
207  
208 I<Since version 0.12.>
209  
210 Runs the given C<$path> with the given C<@args> as a sub-process with a new
211 pipe as its STDOUT filehandle. The returned L<Future> will additionally yield
212 the bytes read from the STDOUT pipe.
213  
214 =cut
215  
216 sub system_out
217 0 ▶{
Subroutine system_out: not called CC=1 SCAR=6.9
Pod documented
218 0 my $self = shift;
219 0 my @argv = @_;
220  
221 0 return $self->run( argv => \@argv, want_out => 1 );
222 }
223  
224 =head1 TODO
225  
226 =over 4
227  
228 =item *
229  
230 Add some OS portability guard warnings when loading the module on platforms
231 not known to support it.
232  
233 =item *
234  
235 Consider what other features of modules like L<IPC::Run> or
236 L<IO::Async::Process> to support here. Try not to go overboard.
237  
238 =back
239  
240 =cut
241  
242 =head1 AUTHOR
243  
244 Paul Evans <leonerd@leonerd.org.uk>
245  
246 =cut
247  
248 1 0x55AA;
↑ Summary IO.pm »