File: System.pm

package info (click to toggle)
libfuture-io-perl 0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 739; makefile: 2
file content (246 lines) | stat: -rw-r--r-- 5,569 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk

package Future::IO::System 0.16;

use v5.14;
use warnings;

use Carp;

use Future::IO;

=head1 NAME

C<Future::IO::System> - C<system()>-like methods for L<Future::IO>

=head1 SYNOPSIS

=for highlighter language=perl

   use Future::IO;
   use Future::IO::System;

   my $f = Future::IO::System->system( "cmd", "args go", "here" );
   # $f will become done when the command completes

   my $f = Future::IO::System->system_out( "cmd", "-v" );
   my ( $status, $out ) = $f->get;

   # $status will contain the exit code and $out will contain what it wrote
   # to STDOUT

=head1 DESCRIPTION

This package contains a selection of methods that behave like the core
C<system()> and related functions, running asynchronously via L<Future::IO>.

In particular, the L</system> behaves somewhat like C<CORE::system()> and
L</system_out> behaves somewhat like L<qx()>.

=head2 Portability

In order for this module to work at all, the underlying C<Future::IO>
implementation must support the L<Future::IO/waitpid> method. The default
minimal implementation included with the module does not, but most of the
additional implementations from CPAN will.

In addition, the operation of this module uses techniques that only really
work on full POSIX systems (such as Linux, Mac OS X, the various BSDs, etc).
It is unlikely to work in places like MSWin32.

=cut

# TODO: Print at least some sort of warning if loaded on one of the weird
# non-POSIX OSes

=head1 METHODS

=cut

=head2 run

   ( $exitcode, ... ) = await Future::IO::System->run(
      argv => [ $path, @args ],
      ...
   );

I<Since version 0.12.>

Runs the given C<$path> with the given C<@args> as a sub-process, optionally
with some additional filehandles set up as determined by the other arguments.
The returned L<Future> will yield the C<waitpid()> exit code from the process
when it terminates, and optionally the bytes read from the other filehandles
that were set up.

Takes the following named arguments

=over 4

=item argv => ARRAY

An array reference containing the path and arguments to pass to C<exec()> in
the child process.

=item in => STRING

If defined, create a pipe and assign the reading end to the child process's
STDIN filehandle. The given string will then be written to the pipe, after
which the pipe will be closed.

=item want_out => BOOL

If true, create a pipe and assign the writing end to the child process's
STDOUT filehandle. The returned future will additionally contain all the bytes
read from it until EOF.

=item want_err => BOOL

If true, create a pipe and assign the writing end to the child process's
STDERR filehandle. The returned future will additionally contain all the bytes
read from it until EOF.

=back

The remaining methods in this class are simplified wrappers of this one.

=cut

sub run
{
   shift;
   my %params = @_;

   my $argv     = $params{argv};
   my $want_in  = defined $params{in};
   my $want_out = $params{want_out};
   my $want_err = $params{want_err};

   my @infh;
   pipe( $infh[0], $infh[1] ) or croak "Cannot pipe() - $!"
      if $want_in;

   my @outfh;
   pipe( $outfh[0], $outfh[1] ) or croak "Cannot pipe() - $!"
      if $want_out;

   my @errfh;
   pipe( $errfh[0], $errfh[1] ) or croak "Cannot pipe() - $!"
      if $want_err;

   defined( my $pid = fork() )
      or croak "Cannot fork() - $!";

   if( $pid ) {
      # parent

      my @f;
      push @f, Future::IO->waitpid( $pid );

      if( $want_in ) {
         close $infh[0];
         push @f, Future::IO->syswrite_exactly( $infh[1], $params{in} )
            ->then( sub { close $infh[1]; Future->done() } );
      }

      if( $want_out ) {
         close $outfh[1];
         push @f, Future::IO->sysread_until_eof( $outfh[0] );
      }

      if( $want_err ) {
         close $errfh[1];
         push @f, Future::IO->sysread_until_eof( $errfh[0] );
      }

      return Future->needs_all( @f );
   }
   else {
      # child

      if( $want_in ) {
         close $infh[1];
         POSIX::dup2( $infh[0]->fileno, 0 );
      }

      if( $want_out ) {
         close $outfh[0];
         POSIX::dup2( $outfh[1]->fileno, 1 );
      }

      if( $want_err ) {
         close $errfh[0];
         POSIX::dup2( $errfh[1]->fileno, 2 );
      }

      exec( @$argv ) or
         POSIX::_exit( -1 );
   }
}

=head2 system

   $exitcode = await Future::IO::System->system( $path, @args );

I<Since version 0.12.>

Runs the given C<$path> with the given C<@args> as a sub-process with no extra
filehandles.

=cut

sub system
{
   my $self = shift;
   my @argv = @_;

   return $self->run( argv => \@argv );
}

=head2 system_out

   ( $exitcode, $out ) = await Future::IO::System->system_out( $path, @args );

I<Since version 0.12.>

Runs the given C<$path> with the given C<@args> as a sub-process with a new
pipe as its STDOUT filehandle. The returned L<Future> will additionally yield
the bytes read from the STDOUT pipe.

=cut

sub system_out
{
   my $self = shift;
   my @argv = @_;

   return $self->run( argv => \@argv, want_out => 1 );
}

=head1 TODO

=over 4

=item *

Add some OS portability guard warnings when loading the module on platforms
not known to support it.

=item *

Consider what other features of modules like L<IPC::Run> or
L<IO::Async::Process> to support here. Try not to go overboard.

=back

=cut

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;