IO.pm

filter: Statementstmt 81.6% 191 / 234 Branchbran 53.5% 45 / 84 Conditioncond 67.1% 43 / 64 MC/DCmcdc 48.5% 17 / 35 Subroutinesub 87.7% 43 / 49 Podpod 94.7% 18 / 19 totaltotal 73.6% 357 / 485 CC 77 SCAR 41.6
CC
77
Cov
73%
load_best_impl
380.0
_await_once
22.0
try_load_impl
20.0
CRAP 64.0
« IO/System.pm ↑ Summary IO/ImplBase.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, 2019-2026 -- leonerd@leonerd.org.uk
5  
6 package Future::IO 0.23;
7  
8 18 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # These need to be visible to sub override_impl
14 18 my @pollers;
15 my @alarms;
16  
17 18 our $IMPL;
18  
19 18 our $MAX_READLEN = 8192;
20 18 our $MAX_WRITELEN = 8192;
21  
22 18 ▶use IO::Poll qw( POLLIN POLLOUT POLLPRI POLLHUP POLLERR POLLNVAL );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 18 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 # This needs to happen at BEGIN time because stupid cyclic reasons
27 18 our @EXPORT_OK = qw( POLLIN POLLOUT POLLPRI POLLHUP POLLERR POLLNVAL );
28 }
29  
30 =head1 NAME
31  
32 C<Future::IO> - Future-returning IO methods
33  
34 =head1 SYNOPSIS
35  
36 =for highlighter language=perl
37  
38 use Future::IO;
39 Future::IO->load_best_impl;
40  
41 my $delay = Future::IO->sleep( 5 );
42 # $delay will become done in 5 seconds time
43  
44 my $input = Future::IO->read( \*STDIN, 4096 );
45 # $input will yield some input from the STDIN IO handle
46  
47 =head1 DESCRIPTION
48  
49 This package provides a few basic methods that behave similarly to the
50 same-named core perl functions relating to IO operations, but yield their
51 results asynchronously via L<Future> instances.
52  
53 This is provided primarily as a decoupling mechanism, to allow modules to be
54 written that perform IO in an asynchronous manner to depend directly on this,
55 while allowing asynchronous event systems to provide an implementation of
56 these operations.
57  
58 =head2 Default Implementation
59  
60 If the C<override_impl> method is not invoked, a default implementation of
61 these operations is provided. This implementation allows a single queue of
62 C<read> or C<write> calls on a single filehandle only, combined with C<sleep>
63 calls. It does not support C<waitpid>.
64  
65 It is provided for the simple cases where modules only need one filehandle
66 (most likely a single network socket or hardware device handle), allowing such
67 modules to work without needing a better event system.
68  
69 If there are both read/write and C<sleep> futures pending, the implementation
70 will use C<select()> to wait for either. This may be problematic on MSWin32,
71 depending on what type of filehandle is involved.
72  
73 If C<select()> is not being used then the default implementation will
74 temporarily set filehandles into blocking mode (by switching off the
75 C<O_NONBLOCK> flag) while performing IO on them.
76  
77 For cases where multiple filehandles are required, or for doing more involved
78 IO operations, a real implementation based on an actual event loop should be
79 loaded. It is recommended to use the L</load_best_impl> method to do this, if
80 there are no other specific requirements. If the program is already using some
81 other event system, such as L<UV> or L<IO::Async>, it is best to directly load
82 the relevant implementation module in the toplevel program.
83  
84 =head2 Unit Testing
85  
86 The replaceable implementation is also useful for writing unit test scripts.
87 If the implementation is set to an instance of some sort of test fixture or
88 mocking object, a unit test can check that the appropriate IO operations
89 happen as part of the test.
90  
91 A testing module which does this is provided by L<Test::Future::IO>.
92  
93 =head2 Cancellation
94  
95 Any C<Future> returned by a C<Future::IO> method should support being
96 cancelled by the L<Future/cancel> method. Doing so should not cause the
97 program to break overall, nor will it upset the specific implementation being
98 used.
99  
100 I<However>, the result of cancelling a future instance that performs some
101 actual IO work is I<unspecified>. It may cause no work to be performed, or it
102 may result in a partial (or even complete but as-yet unreported) IO operation
103 to have already taken place. In particular, operations that write bytes to, or
104 read bytes from filehandles may have already transferred some or all of those
105 bytes before the future was cancelled, and there is now nothing that the
106 program can do to "undo" those effects. In general it is likely that the only
107 situation where you will cancel an IO operation on a filehandle is when an
108 entire connection is being abandoned, filehandles closed, and so on.
109  
110 That said, it should be safe to cancel L</alarm> and L</sleep> futures, as
111 each one will operate entirely independently, and not cause any change of
112 state to any of the others. This typically allows you to wrap a "timeout"-like
113 behaviour around any other sort of IO operation by using L<Future/needs_any>
114 or similar. If the real IO operation was successful, the timeout can be safely
115 cancelled. If the timeout happens, the IO operation will be cancelled, and at
116 this point the application will have to discard the filehandle (or otherwise
117 resynchronse in some application-specific manner).
118  
119 =cut
120  
121 =head1 METHODS
122  
123 =cut
124  
125 =head2 accept
126  
127 $socketfh = await Future::IO->accept( $fh );
128  
129 I<Since version 0.11.>
130  
131 Returns a L<Future> that will become done when a new connection has been
132 accepted on the given filehandle, which should represent a listen-mode socket.
133 The returned future will yield the newly-accepted client socket filehandle.
134  
135 =cut
136  
137 sub accept
138 1 ▶{
Subroutine accept: called CC=2 SCAR=7.1
Pod documented
139 1 shift;
140 1 my ( $fh ) = @_;
141  
142 1 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->accept( $fh );
Condition50%1/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
01
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table50%1/2
A
A= $Future::IO::IMPL
Aresult
11
00
143 }
144  
145 =head2 alarm
146  
147 await Future::IO->alarm( $epoch );
148  
149 I<Since version 0.08.>
150  
151 Returns a L<Future> that will become done at a fixed point in the future,
152 given as an epoch timestamp (such as returned by C<time()>). This value may be
153 fractional.
154  
155 =cut
156  
157 sub alarm
158 1 ▶{
Subroutine alarm: called CC=3 SCAR=11.8
Pod documented
159 1 shift;
160 1 my ( $epoch ) = @_;
161  
162 1 ▶ $IMPL //= "Future::IO::_DefaultImpl";
Condition50%1/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
10
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table50%1/2
A
A= $Future::IO::IMPL
Aresult
11
00
163  
164 1 ▶ if( $IMPL->can( "alarm" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Future::IO::IMPL->can('alarm')) { } 1 0 1
165 1 return $IMPL->alarm( $epoch );
166 }
167 else {
168 0 return $IMPL->sleep( $epoch - Time::HiRes::time() );
169 }
170 }
171  
172 =head2 connect
173  
174 await Future::IO->connect( $fh, $name );
175  
176 I<Since version 0.11.>
177  
178 Returns a L<Future> that will become done when a C<connect()> has succeeded on
179 the given filehandle to the given sockname address.
180  
181 =cut
182  
183 sub connect
184 2 ▶{
Subroutine connect: called CC=2 SCAR=6.9
Pod documented
185 2 shift;
186 2 my ( $fh, $name ) = @_;
187  
188 2 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->connect( $fh, $name );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
11
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
189 }
190  
191 =head2 poll
192  
193 $revents = await Future::IO->poll( $fh, $events );
194  
195 I<Since version 0.19.>
196  
197 Returns a L<Future> that will become done when the indicated IO operations
198 can be performed on the given filehandle. I<$events> should be a bitfield of
199 one or more of the POSIX C<POLL*> constants, such as C<POLLIN>, C<POLLOUT> or
200 C<POLLPRI>. The result of the future will be a similar bitfield, indicating
201 which operations may now take place. If the C<POLLHUP>, C<POLLERR> or
202 C<POLLNVAL> events happen, they will always be reported; you do not need to
203 request these specifically. You should always request at least one of
204 C<POLLIN>, C<POLLOUT> or C<POLLPRI>. Older versions of some implementation
205 modules did accept just C<POLLHUP>, but that is not supported any more.
206  
207 Multiple outstanding futures may be enqueued for the same filehandle. When an
208 event happens, only the first outstanding future that is interested in it is
209 informed; the rest will remain pending for the next round of IO events, if the
210 condition still prevails.
211  
212 Note that, as compared to the real C<poll(2)> system call, this method only
213 operates on a single filehandle; all futures returned by it are independent
214 and refer just to that one filehandle each.
215  
216 Also note that, in general, it is better to use one of the higher-level
217 methods to perform whatver IO operation is required on the given filehandle.
218 The C<poll> method is largely intended as a lowest-level fallback, for example
219 if integrating with some other library or module that performs its own
220 filehandle IO and just needs to be informed when such IO operations may be
221 performed.
222  
223 For convenience, the C<POLL*> constants are exported by this module. They
224 should be used in preference to the ones from C<IO::Poll>, in case a platform
225 does not provide the latter module directly.
226  
227 =cut
228  
229 sub poll
230 18 ▶{
Subroutine poll: called CC=2 SCAR=6.9
Pod documented
231 18 shift;
232 18 my ( $fh, $events ) = @_;
233  
234 18 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->poll( $fh, $events );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
171
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
235 }
236  
237 =head2 read
238  
239 $bytes = await Future::IO->read( $fh, $length );
240  
241 I<Since version 0.17.> Before this version this method used to be named
242 C<sysread>, and still available via that alias.
243  
244 Returns a L<Future> that will become done when at least one byte can be read
245 from the given filehandle. It may return up to C<$length> bytes. On EOF, the
246 returned future will yield an empty list (or C<undef> in scalar context). On
247 any error (other than C<EAGAIN> / C<EWOULDBLOCK> which are ignored), the
248 future fails with a suitable error message.
249  
250 Note specifically this may perform only a single C<sysread()> call, and thus
251 is not guaranteed to actually return the full length.
252  
253 =cut
254  
255 sub read
256 12 ▶{
Subroutine read: called CC=2 SCAR=6.9
Pod documented
257 12 shift;
258 12 my ( $fh, $length ) = @_;
259  
260 12 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->sysread( $fh, $length );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
93
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
261 }
262  
263 18 *sysread = \&read;
264  
265 =head2 read_exactly
266  
267 $bytes = await Future::IO->read_exactly( $fh, $length );
268  
269 I<Since version 0.17.> Before this version this method used to be named
270 C<sysread_exactly>, and still available via that alias.
271  
272 Returns a L<Future> that will become done when exactly the given number of
273 bytes have been read from the given filehandle. It returns exactly C<$length>
274 bytes. On EOF, the returned future will yield an empty list (or C<undef> in
275 scalar context), even if fewer bytes have already been obtained. These bytes
276 will be lost. On any error (other than C<EAGAIN> / C<EWOULDBLOCK> which are
277 ignored), the future fails with a suitable error message.
278  
279 This may make more than one C<sysread()> call.
280  
281 =cut
282  
283 sub read_exactly
284 2 ▶{
Subroutine read_exactly: called CC=3 SCAR=11.2
Pod documented
285 2 shift;
286 2 my ( $fh, $length ) = @_;
287  
288 2 ▶ $IMPL //= "Future::IO::_DefaultImpl";
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
11
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
289  
290 2 ▶ if( my $code = $IMPL->can( "sysread_exactly" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $Future::IO::IMPL->can("sysread_exactly")) 0 2 2
291 0 return $IMPL->$code( $fh, $length );
292 }
293  
294 2 return _read_into_buffer( $IMPL, $fh, $length, \(my $buffer = '') );
295 }
296  
297 18 *sysread_exactly = \&read_exactly;
298  
299 sub _read_into_buffer
300 7 ▶{
Subroutine _read_into_buffer: called CC=1 SCAR=0.0
301 7 my ( $IMPL, $fh, $length, $bufref ) = @_;
302  
303 7 ▶ $IMPL->sysread( $fh, $length - length $$bufref )->then( sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
304 7 my ( $more ) = @_;
305 7 ▶ return Future->done() if !defined $more; # EOF
Branch: 1 branch
BranchTrueFalseTotal
unless defined $more 1 6 7
306  
307 6 $$bufref .= $more;
308  
309 6 ▶ return Future->done( $$bufref ) if length $$bufref >= $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $$bufref >= $length 1 5 6
310 5 return _read_into_buffer( $IMPL, $fh, $length, $bufref );
311 7 });
312 }
313  
314 =head2 read_until_eof
315  
316 $f = Future::IO->read_until_eof( $fh );
317  
318 I<Since version 0.17.> Before this version this method used to be named
319 C<sysread_until_eof>, and still available via that alias.
320  
321 Returns a L<Future> that will become done when the given filehandle reaches
322 the EOF condition. The returned future will yield all of the bytes read up
323 until that point.
324  
325 =cut
326  
327 sub read_until_eof
328 1 ▶{
Subroutine read_until_eof: called CC=2 SCAR=7.0
Pod documented
329 1 shift;
330 1 my ( $fh ) = @_;
331  
332 1 ▶ $IMPL //= "Future::IO::_DefaultImpl";
Condition50%1/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
01
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table50%1/2
A
A= $Future::IO::IMPL
Aresult
11
00
333  
334 1 return _read_until_eof( $IMPL, $fh, \(my $buffer = '') );
335 }
336  
337 18 *sysread_until_eof = \&read_until_eof;
338  
339 sub _read_until_eof
340 2 ▶{
Subroutine _read_until_eof: called CC=1 SCAR=0.0
341 2 my ( $IMPL, $fh, $bufref ) = @_;
342  
343 2 ▶ $IMPL->sysread( $fh, $MAX_READLEN )->then( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
344 2 my ( $more ) = @_;
345 2 ▶ return Future->done( $$bufref ) if !defined $more;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $more 1 1 2
346  
347 1 $$bufref .= $more;
348 1 return _read_until_eof( $IMPL, $fh, $bufref );
349 2 });
350 }
351  
352 =head2 recv
353  
354 =head2 recvfrom
355  
356 $bytes = await Future::IO->recv( $fh, $length );
357 $bytes = await Future::IO->recv( $fh, $length, $flags );
358  
359 ( $bytes, $from ) = await Future::IO->recvfrom( $fh, $length );
360 ( $bytes, $from ) = await Future::IO->recvfrom( $fh, $length, $flags );
361  
362 I<Since version 0.17.>
363  
364 Returns a L<Future> that will become done when at least one byte is received
365 from the given filehandle (presumably a socket), by using a C<recv(2)> or
366 C<recvfrom(2)> system call. On any error (other than C<EAGAIN> /
367 C<EWOULDBLOCK> which are ignored) the future fails with a suitable error
368 message.
369  
370 Optionally a flags bitmask in C<$flags> can be passed. If no flags are
371 required, the value may be zero. The C<recvfrom> method additionally returns
372 the sender's address as a second result value; this is primarily used by
373 unconnected datagram sockets.
374  
375 =cut
376  
377 sub recv
378 4 ▶{
Subroutine recv: called CC=2 SCAR=6.9
Pod documented
379 4 shift;
380 4 my ( $fh, $length, $flags ) = @_;
381  
382 4 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->recv( $fh, $length, $flags );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
31
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
383 }
384  
385 sub recvfrom
386 4 ▶{
Subroutine recvfrom: called CC=2 SCAR=7.1
Pod documented
387 4 shift;
388 4 my ( $fh, $length, $flags ) = @_;
389  
390 4 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->recvfrom( $fh, $length, $flags );
Condition50%1/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
40
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table50%1/2
A
A= $Future::IO::IMPL
Aresult
11
00
391 }
392  
393 =head2 send
394  
395 $sent_len = await Future::IO->send( $fh, $bytes );
396 $sent_len = await Future::IO->send( $fh, $bytes, $flags );
397 $sent_len = await Future::IO->send( $fh, $bytes, $flags, $to );
398  
399 I<Since version 0.17.>
400  
401 Returns a L<Future> that will become done when at least one byte has been
402 sent to the given filehandle (presumably a socket), by using a C<send(2)>
403 system call. On any error (other than C<EAGAIN> / C<EWOULDBLOCK> which are
404 ignored) the future fails with a suitable error message.
405  
406 Optionally a flags bitmask in C<$flags> or a destination address in C<$to> can
407 also be passed. If no flags are required, the value may be zero. If C<$to> is
408 specified then a C<sendto(2)> system call is used instead.
409  
410 =cut
411  
412 sub send
413 5 ▶{
Subroutine send: called CC=2 SCAR=6.9
Pod documented
414 5 shift;
415 5 my ( $fh, $bytes, $flags, $to ) = @_;
416  
417 5 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->send( $fh, $bytes, $flags, $to );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
41
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
418 }
419  
420 =head2 sleep
421  
422 await Future::IO->sleep( $secs );
423  
424 Returns a L<Future> that will become done a fixed delay from now, given in
425 seconds. This value may be fractional.
426  
427 =cut
428  
429 sub sleep
430 9 ▶{
Subroutine sleep: called CC=2 SCAR=6.9
Pod documented
431 9 shift;
432 9 my ( $secs ) = @_;
433  
434 9 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->sleep( $secs );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
81
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
435 }
436  
437 =head2 waitpid
438  
439 $wstatus = await Future::IO->waitpid( $pid );
440  
441 I<Since version 0.09.>
442  
443 Returns a L<Future> that will become done when the given child process
444 terminates. The future will yield the wait status of the child process.
445 This can be inspected by the usual bitshifting operations as per C<$?>:
446  
447 if( my $termsig = ($wstatus & 0x7f) ) {
448 say "Terminated with signal $termsig";
449 }
450 else {
451 my $exitcode = ($wstatus >> 8);
452 say "Terminated with exit code $exitcode";
453 }
454  
455 =cut
456  
457 sub waitpid
458 0 ▶{
Subroutine waitpid: not called CC=2 SCAR=17.9
Pod documented
459 0 shift;
460 0 my ( $pid ) = @_;
461  
462 0 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->waitpid( $pid );
Condition0%0/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
00
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table0%0/2
A
A= $Future::IO::IMPL
Aresult
11
00
463 }
464  
465 =head2 write
466  
467 $written_len = await Future::IO->write( $fh, $bytes );
468  
469 I<Since version 0.17.> Before this version this method used to be named
470 C<syswrite>, and still available via that alias.
471  
472 Returns a L<Future> that will become done when at least one byte has been
473 written to the given filehandle. It may write up to all of the bytes. On any
474 error (other than C<EAGAIN> / C<EWOULDBLOCK> which are ignored) the future
475 fails with a suitable error message.
476  
477 Note specifically this may perform only a single C<syswrite()> call, and thus
478 is not guaranteed to actually return the full length.
479  
480 =cut
481  
482 sub write
483 10 ▶{
Subroutine write: called CC=2 SCAR=6.9
Pod documented
484 10 shift;
485 10 my ( $fh, $bytes ) = @_;
486  
487 10 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->syswrite( $fh, $bytes );
Condition100%2/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
91
MC/DC100%1/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table100%2/2
A
A= $Future::IO::IMPL
Aresult
11
00
488 }
489  
490 18 *syswrite = \&write;
491  
492 =head2 write_exactly
493  
494 $written_len = await Future::IO->write_exactly( $fh, $bytes );
495  
496 I<Since version 0.17.> Before this version this method used to be named
497 C<syswrite_exactly>, and still available via that alias.
498  
499 Returns a L<Future> that will become done when exactly the given bytes have
500 been written to the given filehandle. On any error (other than C<EAGAIN> /
501 C<EWOULDBLOCK> which are ignored) the future fails with a suitable error
502 message.
503  
504 This may make more than one C<syswrite()> call.
505  
506 =cut
507  
508 sub write_exactly
509 1 ▶{
Subroutine write_exactly: called CC=3 SCAR=11.8
Pod documented
510 1 shift;
511 1 my ( $fh, $bytes ) = @_;
512  
513 1 ▶ $IMPL //= "Future::IO::_DefaultImpl";
Condition50%1/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
01
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table50%1/2
A
A= $Future::IO::IMPL
Aresult
11
00
514  
515 1 ▶ if( my $code = $IMPL->can( "syswrite_exactly" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $Future::IO::IMPL->can("syswrite_exactly")) 0 1 1
516 0 return $IMPL->$code( $fh, $bytes );
517 }
518  
519 1 return _write_from_buffer( $IMPL, $fh, \$bytes, length $bytes );
520 }
521  
522 18 *syswrite_exactly = \&write_exactly;
523  
524 sub _write_from_buffer
525 3 ▶{
Subroutine _write_from_buffer: called CC=1 SCAR=0.0
526 3 my ( $IMPL, $fh, $bufref, $len ) = @_;
527  
528 3 ▶ $IMPL->syswrite( $fh, substr $$bufref, 0, $MAX_WRITELEN )->then( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
529 3 my ( $written_len ) = @_;
530 3 substr $$bufref, 0, $written_len, "";
531  
532 3 ▶ return Future->done( $len ) if !length $$bufref;
Branch: 1 branch
BranchTrueFalseTotal
unless length $$bufref 1 2 3
533 2 return _write_from_buffer( $IMPL, $fh, $bufref, $len );
534 3 });
535 }
536  
537 =head2 override_impl
538  
539 Future::IO->override_impl( $impl );
540  
541 Sets a new implementation for C<Future::IO>, replacing the minimal default
542 internal implementation. This can either be a package name or an object
543 instance reference, but must provide the methods named above.
544  
545 This method is intended to be called by event loops and other similar places,
546 to provide a better integration. Another way, which doesn't involve directly
547 depending on C<Future::IO> or loading it, is to use the C<$IMPL> variable; see
548 below.
549  
550 Can only be called once, and only if the default implementation is not in use,
551 therefore a module that wishes to override this ought to invoke it as soon as
552 possible on program startup, before any of the main C<Future::IO> methods may
553 have been called.
554  
555 =cut
556  
557 18 my $overridden;
558  
559 sub override_impl
560 1 ▶{
Subroutine override_impl: called CC=4 SCAR=15.6
Pod documented
561 1 shift;
562 1 ▶ croak "Future::IO implementation is already overridden" if defined $IMPL;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Future::IO::IMPL 0 1 1
563 1 ▶ croak "Future::IO implementation cannot be set once default is already in use"
Branch: 1 branch
BranchTrueFalseTotal
if @pollers or @alarms 0 1 1
Condition33%1/3
@pollers or @alarms
l!l&&r!l&&!r
001
MC/DC0%0/2
@pollers or @alarms
@pollers @alarms
Truth table33%1/3
A or B
A= @pollersB= @alarms
ABresult
1X1
011
000
564 if @pollers or @alarms;
565  
566 1 ( $IMPL ) = @_;
567 }
568  
569 sub try_load_impl
570 0 ▶{
Subroutine try_load_impl: not called CC=4 SCAR=30.0
Pod undocumented
571 0 shift;
572 0 my ( $name ) = @_;
573  
574 0 ▶ $name =~ m/::/ or $name = "Future::IO::Impl::$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /::/u or $name = "Future::IO::Impl::$name" 0 0 0
575 0 my $module = "$name.pm" =~ s{::}{/}gr;
576  
577 0 ▶ eval { require $module } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $module } } or return 0 0 0 0
578 0 ▶ $name->can( "poll" ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$name->can("poll") or return 0 0 0 0
579 # TODO: Consider some sort of API version check
580  
581 0 return 1;
582 }
583  
584 =head2 load_impl
585  
586 Future::IO->load_impl( @names );
587  
588 I<Since version 0.16.>
589  
590 Given a list of possible implementation module names, iterates through them
591 attempting to load each one until a suitable module is found. Any errors
592 encountered while loading each are ignored. If no module is found to be
593 suitable, an exception is thrown that likely aborts the program.
594  
595 C<@names> should contain a list of Perl module names (which likely live in the
596 C<Future::IO::Impl::*> prefix). If any name does not contain a C<::>
597 separator, it will have that prefix applied to it. This allows a conveniently
598 short list; e.g.
599  
600 Future::IO->load_impl( qw( UV Glib Ppoll ) );
601  
602 This method is intended to be called once, at startup, by the main containing
603 program. Since it sets the implementation, it would generally be considered
604 inappropriate to invoke this method from some additional module that might be
605 loaded by a containing program.
606  
607 This is now discouraged, in favour of letting C<Future::IO> decide instead by
608 using L</load_best_impl>.
609  
610 =cut
611  
612 sub load_impl
613 0 ▶{
Subroutine load_impl: not called CC=3 SCAR=24.8
Pod documented
614 0 shift;
615  
616 0 foreach ( @_ ) {
617 0 ▶ Future::IO->try_load_impl( $_ ) and return;
Branch: 1 branch
BranchTrueFalseTotal
"Future::IO"->try_load_impl($_) and return 0 0 0
618 }
619 0 die "Unable to find a usable Future::IO::Impl subclass\n";
620 }
621  
622 =head2 load_best_impl
623  
624 Future::IO->load_best_impl();
625  
626 I<Since version 0.18.>
627  
628 Attempt to work out and load an implementation module.
629  
630 In most situations, most programs don't really care what specific
631 implementation module they use, if they aren't already committed to some other
632 event system and also using C<Future::IO> alongside it. This method allows
633 programs to offload the decision-making about which specific implementations
634 to try to load, to C<Future::IO> itself.
635  
636 This method works by attempting a few different strategies to determine the
637 "best" implementation to use. It maintains a list of the currently-known CPAN
638 modules which provide implementations, and attempts them in a given preference
639 order.
640  
641 The environment variable C<PERL_FUTURE_IO_IMPL> offers further control of the
642 behaviour of this method. Its value should be a comma-separated list of
643 implementation names to be attempted, in preference to any of the others.
644 Names prefixed with a hyphen will be skipped entirely by any attempt.
645  
646 =over 4
647  
648 =item 1.
649  
650 First, if C<PERL_FUTURE_IO_IMPL> is set, any of the names given are tried, in
651 order.
652  
653 =item 2.
654  
655 Then any of the modules that attempt to wrap other event systems such as L<UV>
656 or L<Glib> are attempted if it is detected that the other event system is
657 already loaded.
658  
659 =item 3.
660  
661 If none of these were successful, next it attempts any OS-specific modules
662 based on the OS name (given by C<$^O>).
663  
664 =item 4.
665  
666 Finally, a list of other generic modules is attempted, which also includes
667 any of the wrapper implementations that can be started independently.
668  
669 =back
670  
671 For more details, consult the implementation code in this module to find the
672 current list of known modules and the order they are attempted in.
673  
674 =cut
675  
676 # Try to account for every Future::IO::Impl::* module on CPAN
677 #
678 # Purposely omitting:
679 # Future::IO::Impl::Tickit - requires a $tickit instance to work
680  
681 18 my @IMPLS_WRAPPER = (
682 "UV",
683 "Glib",
684 [ IOAsync => "IO::Async::Loop" ],
685 "POE",
686 "AnyEvent",
687 );
688  
689 18 my %IMPLS_FOR_OS = (
690 darwin => [qw( KQueue )],
691 freebsd => [qw( KQueue )], # TODO and probably other BSDs
692 linux => [qw( Uring )],
693 openbsd => [qw( KQueue )],
694 # TODO: other OSes?
695 );
696  
697 18 my @IMPLS_GENERIC = (qw(
698 Ppoll
699 UV
700 Glib
701 ));
702  
703 sub load_best_impl
704 0 ▶{
Subroutine load_best_impl: not called CC=19 SCAR=59.4
Pod documented
705 0 shift;
706  
707 0 my @prefer;
708 my %veto;
709  
710 0 ▶ foreach ( split m/,/, $ENV{PERL_FUTURE_IO_IMPL} // "" ) {
Condition0%0/2
$ENV{"PERL_FUTURE_IO_IMPL"} // ""
l!l
00
MC/DC0%0/1
$ENV{"PERL_FUTURE_IO_IMPL"} // ""
$ENV{"PERL_FUTURE_IO_IMPL"}
Truth table0%0/2
A
A= $ENV{"PERL_FUTURE_IO_IMPL"}
Aresult
11
00
711 0 ▶ if( s/^-// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^-//u) { } 0 0 0
712 0 $veto{$_} = 1;
713 }
714 else {
715 0 push @prefer, $_;
716 }
717 }
718  
719 0 foreach my $impl ( @prefer ) {
720 0 ▶ $veto{$impl} and next;
Branch: 1 branch
BranchTrueFalseTotal
$veto{$impl} and next 0 0 0
721 0 ▶ Future::IO->try_load_impl( $impl ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
"Future::IO"->try_load_impl($impl) and return 1 0 0 0
722 }
723  
724 # First, load a wrapper impl if the wrapped system is already loaded
725 0 foreach ( @IMPLS_WRAPPER ) {
726 0 ▶ my ( $impl, $package ) = ref $_ ? @$_ : ( $_, $_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
727 0 ▶ $veto{$impl} and next;
Branch: 1 branch
BranchTrueFalseTotal
$veto{$impl} and next 0 0 0
728 0 ▶ eval { $package->VERSION(0) } or next;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $package->VERSION(0) } } or next 0 0 0
729  
730 0 ▶ Future::IO->try_load_impl( $impl ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
"Future::IO"->try_load_impl($impl) and return 1 0 0 0
731 }
732  
733 # OK, maybe we can find a good impl for this particular OS
734 0 ▶ foreach my $impl ( @{ $IMPLS_FOR_OS{$^O} || [] } ) {
Condition0%0/2
$IMPLS_FOR_OS{$^O} || []
l!l
00
MC/DC0%0/1
$IMPLS_FOR_OS{$^O} || []
$IMPLS_FOR_OS{$^O}
Truth table0%0/2
A
A= $IMPLS_FOR_OS{$^O}
Aresult
11
00
735 0 ▶ $veto{$impl} and next;
Branch: 1 branch
BranchTrueFalseTotal
$veto{$impl} and next 0 0 0
736 0 ▶ Future::IO->try_load_impl( $impl ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
"Future::IO"->try_load_impl($impl) and return 1 0 0 0
737 }
738  
739 # Failing all of that, try the generic ones
740 0 foreach my $impl ( @IMPLS_GENERIC ) {
741 0 ▶ $veto{$impl} and next;
Branch: 1 branch
BranchTrueFalseTotal
$veto{$impl} and next 0 0 0
742 0 ▶ Future::IO->try_load_impl( $impl ) and return 1;
Branch: 1 branch
BranchTrueFalseTotal
"Future::IO"->try_load_impl($impl) and return 1 0 0 0
743 }
744  
745 0 die "Unable to find a usable Future::IO::Impl subclass\n";
746 }
747  
748 =head2 HAVE_MULTIPLE_FILEHANDLES
749  
750 $has = Future::IO->HAVE_MULTIPLE_FILEHANDLES;
751  
752 I<Since version 0.11.>
753  
754 Returns true if the underlying IO implementation actually supports multiple
755 filehandles. The default minimal internal implementation used not to support
756 this, but I<since version 0.21> it now does; so this method always returns
757 true.
758  
759 =cut
760  
761 sub HAVE_MULTIPLE_FILEHANDLES
762 0 ▶{
Subroutine HAVE_MULTIPLE_FILEHANDLES: not called CC=2 SCAR=17.9
Pod documented
763 0 ▶ return ( $IMPL //= "Future::IO::_DefaultImpl" )->HAVE_MULTIPLE_FILEHANDLES;
Condition0%0/2
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
l!l
00
MC/DC0%0/1
$Future::IO::IMPL //= "Future::IO::_DefaultImpl"
$Future::IO::IMPL
Truth table0%0/2
A
A= $Future::IO::IMPL
Aresult
11
00
764 }
765  
766 package
767 Future::IO::_DefaultImpl;
768 18 ▶use base qw( Future::IO::ImplBase );
Subroutine BEGIN: called CC=1 SCAR=0.0
769 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
770  
771 18 ▶use IO::Poll qw( POLLIN POLLOUT POLLPRI );
Subroutine BEGIN: called CC=1 SCAR=0.0
772 18 ▶use Struct::Dumb qw( readonly_struct );
Subroutine BEGIN: called CC=1 SCAR=0.0
773 18 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
774  
775 18 readonly_struct Poller => [qw( fh events f )];
776 18 readonly_struct Alarm => [qw( time f )];
777  
778 18 ▶use constant HAVE_MULTIPLE_FILEHANDLES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
779  
780 sub alarm
781 1 ▶{
Subroutine alarm: called CC=1 SCAR=0.0
782 1 my $class = shift;
783 1 return $class->_done_at( shift );
784 }
785  
786 sub sleep
787 7 ▶{
Subroutine sleep: called CC=1 SCAR=0.0
788 7 my $class = shift;
789 7 return $class->_done_at( time() + shift );
790 }
791  
792 sub poll
793 167 ▶{
Subroutine poll: called CC=1 SCAR=0.1
794 167 my $class = shift;
795 167 my ( $fh, $events ) = @_;
796  
797 167 my $f = Future::IO::_DefaultImpl::F->new;
798  
799 167 push @pollers, Poller( $fh, $events, $f );
800  
801 10 ▶ $f->on_cancel( sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
802 10 my $f = shift;
803  
804 10 my $idx = 0;
805 10 ▶ $idx++ while $idx < @pollers and $pollers[$idx]->f != $f;
Branch: 1 branch
BranchTrueFalseTotal
$idx < @pollers and $pollers[$idx]->f != $f and do { ++$idx } 0 10 10
Condition33%1/3
$idx < @pollers and $pollers[$idx]->f != $f
!ll&&!rl&&r
0100
MC/DC0%0/2
$idx < @pollers and $pollers[$idx]->f != $f
$idx < @pollers $pollers[$idx]->f != $f
Truth table33%1/3
A and B
A= $idx < @pollersB= $pollers[$idx]->f != $f
ABresult
0X0
100
111
806  
807 10 splice @pollers, $idx, 1, ();
808 167 });
809  
810 167 return $f;
811 }
812  
813 sub waitpid
814 0 ▶{
Subroutine waitpid: not called CC=1 SCAR=6.9
815 0 croak "This implementation cannot handle waitpid";
816 }
817  
818 sub _done_at
819 8 ▶{
Subroutine _done_at: called CC=3 SCAR=11.2
820 8 shift;
821 8 my ( $time ) = @_;
822  
823 8 my $f = Future::IO::_DefaultImpl::F->new;
824  
825 # TODO: Binary search
826 8 my $idx = 0;
827 8 ▶ $idx++ while $idx < @alarms and $alarms[$idx]->time < $time;
Branch: 1 branch
BranchTrueFalseTotal
$idx < @alarms and $alarms[$idx]->time < $time and do { ++$idx } 2 8 10
Condition66%2/3
$idx < @alarms and $alarms[$idx]->time < $time
!ll&&!rl&&r
802
MC/DC50%1/2
$idx < @alarms and $alarms[$idx]->time < $time
$idx < @alarms $alarms[$idx]->time < $time
Truth table66%2/3
A and B
A= $idx < @alarmsB= $alarms[$idx]->time < $time
ABresult
0X0
100
111
828  
829 8 splice @alarms, $idx, 0, Alarm( $time, $f );
830  
831 2 ▶ $f->on_cancel( sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
832 2 my $self = shift;
833  
834 2 my $idx = 0;
835 2 ▶ $idx++ while $idx < @alarms and $alarms[$idx]->f != $f;
Branch: 1 branch
BranchTrueFalseTotal
$idx < @alarms and $alarms[$idx]->f != $f and do { ++$idx } 0 2 2
Condition33%1/3
$idx < @alarms and $alarms[$idx]->f != $f
!ll&&!rl&&r
020
MC/DC0%0/2
$idx < @alarms and $alarms[$idx]->f != $f
$idx < @alarms $alarms[$idx]->f != $f
Truth table33%1/3
A and B
A= $idx < @alarmsB= $alarms[$idx]->f != $f
ABresult
0X0
100
111
836  
837 2 splice @alarms, $idx, 1, ();
838 8 } );
839  
840 8 return $f;
841 }
842  
843 package # hide
844 Future::IO::_DefaultImpl::F;
845 18 ▶use base qw( Future );
Subroutine BEGIN: called CC=1 SCAR=0.0
846 18 ▶use IO::Poll qw( POLLIN POLLOUT POLLPRI );
Subroutine BEGIN: called CC=1 SCAR=0.0
847 18 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
848  
849 sub _await_once
850 159 ▶{
Subroutine _await_once: called CC=22 SCAR=30.9
851 159 ▶ die "Cowardly refusing to sit idle and do nothing" unless @pollers || @alarms;
Branch: 1 branch
BranchTrueFalseTotal
unless @pollers or @alarms 0 159 159
Condition66%2/3
@pollers or @alarms
l!l&&r!l&&!r
15450
MC/DC0%0/2
@pollers or @alarms
@pollers @alarms
Truth table66%2/3
A or B
A= @pollersB= @alarms
ABresult
1X1
011
000
852  
853 159 my $rvec = '';
854 159 my $wvec = '';
855 159 my $evec = '';
856  
857 159 foreach my $p ( @pollers ) {
858 161 my $fileno = $p->fh->fileno;
859  
860 161 ▶ vec( $rvec, $fileno, 1 ) = 1 if $p->events & POLLIN;
Branch: 1 branch
BranchTrueFalseTotal
if $p->events & 1 135 26 161
861 161 ▶ vec( $wvec, $fileno, 1 ) = 1 if $p->events & POLLOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $p->events & 4 26 135 161
862 161 ▶ vec( $evec, $fileno, 1 ) = 1 if $p->events & POLLPRI;
Branch: 1 branch
BranchTrueFalseTotal
if $p->events & 2 2 159 161
863 }
864  
865 # If we always select() then problematic platforms like MSWin32 would
866 # always break. Instead, we'll only select() if we're waiting on alarms, or
867 # both POLLIN and POLLOUT, or POLLPRI. If not we'll just presume the one
868 # operation we're waiting for is definitely ready right now.
869 159 ▶ my $do_select = @alarms ||
Condition100%9/9
@alarms || $rvec ne '' && $wvec ne '' || $evec ne ''
l!l&&r!l&&!r
112146
@alarms or $rvec ne "" and $wvec ne ""
l!l&&r!l&&!r
74148
$rvec ne "" and $wvec ne ""
!ll&&!rl&&r
211274
MC/DC100%4/4
@alarms || $rvec ne '' && $wvec ne '' || $evec ne ''
@alarms $rvec ne "" $wvec ne "" $evec ne ''
Truth table83%5/6
A or B and C || D
A= @alarmsB= $rvec ne ""C= $wvec ne ""D= $evec ne ''
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
870 ( $rvec ne '' and $wvec ne '' ) ||
871 ( $evec ne '' );
872  
873 159 ▶ if( $do_select ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_select) 13 146 159
874 13 my $maxwait;
875 13 ▶ $maxwait = $alarms[0]->time - time() if @alarms;
Branch: 1 branch
BranchTrueFalseTotal
if @alarms 7 6 13
876  
877 13 my $ret = select( $rvec, $wvec, $evec, $maxwait );
878 }
879 # else just presume it's ready
880  
881 # Perl doesn't have an easy construction for iterating an array possibly
882 # splicing as you go...
883  
884 159 my $npollers = @pollers;
885 # Need to stop after the current set; ignoring any new that were pushed
886 # while running
887 159 ▶ for ( my $idx = 0; $idx < $npollers; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < $npollers) 161 159 320
888 161 my $p = $pollers[$idx];
889  
890 161 my $fh = $p->fh;
891 161 my $fileno = $fh->fileno;
892  
893 161 my $was_blocking;
894 161 ▶ $was_blocking = $fh->blocking(1) if !$do_select;
Branch: 1 branch
BranchTrueFalseTotal
unless $do_select 148 13 161
895  
896 161 my $revents = 0;
897 161 ▶ $revents |= POLLIN if vec( $rvec, $fileno, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if vec $rvec, $fileno, 1 131 30 161
898 161 ▶ $revents |= POLLOUT if vec( $wvec, $fileno, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if vec $wvec, $fileno, 1 29 132 161
899 161 ▶ $revents |= POLLPRI if vec( $evec, $fileno, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if vec $evec, $fileno, 1 0 161 161
900 161 $revents &= $p->events;
901  
902 161 ▶ if( $revents ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($revents) { } 155 6 161
903 155 splice @pollers, $idx, 1, ();
904 155 $npollers--;
905  
906 155 $p->f->done( $revents );
907 }
908 else {
909 6 $idx++;
910 }
911  
912 161 ▶ $fh->blocking(0) if !$do_select and !$was_blocking;
Branch: 1 branch
BranchTrueFalseTotal
unless $do_select or $was_blocking 4 157 161
Condition100%3/3
$do_select or $was_blocking
l!l&&r!l&&!r
131444
MC/DC100%2/2
$do_select or $was_blocking
$do_select $was_blocking
Truth table100%3/3
A or B
A= $do_selectB= $was_blocking
ABresult
1X1
011
000
913 }
914  
915 159 my $now = time();
916 159 ▶ while( @alarms and $alarms[0]->time <= $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@alarms and $alarms[0]->time <= $now) 6 159 165
Condition100%3/3
@alarms and $alarms[0]->time <= $now
!ll&&!rl&&r
15816
MC/DC100%2/2
@alarms and $alarms[0]->time <= $now
@alarms $alarms[0]->time <= $now
Truth table100%3/3
A and B
A= @alarmsB= $alarms[0]->time <= $now
ABresult
0X0
100
111
917 6 ( shift @alarms )->f->done;
918 }
919 }
920  
921 =head2 await
922  
923 $f = $f->await;
924  
925 I<Since version 0.07.>
926  
927 Blocks until this future is ready (either by success or failure). Does not
928 throw an exception if failed.
929  
930 =cut
931  
932 sub await
933 50 ▶{
Subroutine await: called CC=2 SCAR=6.9
934 50 my $self = shift;
935 50 ▶ _await_once until $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_ready or do { _await_once() } 50 159 209
936 50 return $self;
937 }
938  
939 =head1 THE C<$IMPL> VARIABLE
940  
941 I<Since version 0.02.>
942  
943 As an alternative to setting an implementation by using L<override_impl>, a
944 package variable is also available that allows modules such as event systems
945 to opportunistically provide an implementation without needing to depend on
946 the module, or loading it C<require>. Simply directly set that package
947 variable to the name of an implementing package or an object instance.
948  
949 Additionally, implementors may use a name within the C<Future::IO::Impl::>
950 namespace, suffixed by the name of their event system.
951  
952 For example, something like the following code arrangement is recommended.
953  
954 package Future::IO::Impl::BananaLoop;
955  
956 {
957 no warnings 'once';
958 ( $Future::IO::IMPL //= __PACKAGE__ ) eq __PACKAGE__ or
959 warn "Unable to set Future::IO implementation to " . __PACKAGE__ .
960 " as it is already $Future::IO::IMPL\n";
961 }
962  
963 sub poll
964 {
965 ...
966 }
967  
968 sub sleep
969 {
970 ...
971 }
972  
973 sub sysread
974 {
975 ...
976 }
977  
978 sub syswrite
979 {
980 ...
981 }
982  
983 sub waitpid
984 {
985 ...
986 }
987  
988 Optionally, you can also implement L</sysread_exactly> and
989 L</syswrite_exactly>:
990  
991 sub sysread_exactly
992 {
993 ...
994 }
995  
996 sub syswrite_exactly
997 {
998 ...
999 }
1000  
1001 If not, they will be emulated by C<Future::IO> itself, making multiple calls
1002 to the non-C<_exactly> versions.
1003  
1004 =head1 AUTHOR
1005  
1006 Paul Evans <leonerd@leonerd.org.uk>
1007  
1008 =cut
1009  
1010 18 0x55AA;
« IO/System.pm ↑ Summary IO/ImplBase.pm »