blib/lib/IO/Async/Notifier.pm

filter: Statementstmt 86.7% 150 / 173 Branchbran 75.7% 53 / 70 Conditioncond 61.5% 16 / 26 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 92.3% 36 / 39 Podpod 100% 19 / 19 totaltotal 82.4% 281 / 341 CC 49 SCAR 19.0
CC
49
Cov
81%
debug_printf
32.2
_debug_printf_event
30.0
__set_loop
8.1
CRAP 6.7
« blib/lib/IO/Async/FileStream.pm ↑ Summary blib/lib/IO/Async/Timer/Countdown.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, 2006-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Notifier 0.805;
7  
8 84 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 84 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 84 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 84 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 84 ▶use Future 0.26; # ->is_failed
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 84 ▶use IO::Async::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Perl 5.8.4 cannot do trampolines by modiying @_ then goto &$code
19 84 ▶use constant HAS_BROKEN_TRAMPOLINES => ( $] == "5.008004" );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 C<IO::Async::Notifier> - base class for L<IO::Async> event objects
24  
25 =head1 SYNOPSIS
26  
27 =for highlighter language=perl
28  
29 Usually not directly used by a program, but one valid use case may be:
30  
31 use IO::Async::Notifier;
32  
33 use IO::Async::Stream;
34 use IO::Async::Signal;
35  
36 use IO::Async::Loop;
37 my $loop = IO::Async::Loop->new;
38  
39 my $notifier = IO::Async::Notifier->new;
40  
41 $notifier->add_child(
42 IO::Async::Stream->new_for_stdin(
43 on_read => sub {
44 my $self = shift;
45 my ( $buffref, $eof ) = @_;
46  
47 while( $$buffref =~ s/^(.*)\n// ) {
48 print "You said $1\n";
49 }
50  
51 return 0;
52 },
53 )
54 );
55  
56 $notifier->add_child(
57 IO::Async::Signal->new(
58 name => 'INT',
59 on_receipt => sub {
60 print "Goodbye!\n";
61 $loop->stop;
62 },
63 )
64 );
65  
66 $loop->add( $notifier );
67  
68 $loop->run;
69  
70 =head1 DESCRIPTION
71  
72 This object class forms the basis for all the other event objects that an
73 L<IO::Async> program uses. It provides the lowest level of integration with a
74 L<IO::Async::Loop> container, and a facility to collect Notifiers together, in
75 a tree structure, where any Notifier can contain a collection of children.
76  
77 Normally, objects in this class would not be directly used by an end program,
78 as it performs no actual IO work, and generates no actual events. These are all
79 left to the various subclasses, such as:
80  
81 =over 4
82  
83 =item *
84  
85 L<IO::Async::Handle> - event callbacks for a non-blocking file descriptor
86  
87 =item *
88  
89 L<IO::Async::Stream> - event callbacks and write bufering for a stream
90 filehandle
91  
92 =item *
93  
94 L<IO::Async::Socket> - event callbacks and send buffering for a socket
95 filehandle
96  
97 =item *
98  
99 L<IO::Async::Timer> - base class for Notifiers that use timed delays
100  
101 =item *
102  
103 L<IO::Async::Signal> - event callback on receipt of a POSIX signal
104  
105 =item *
106  
107 L<IO::Async::PID> - event callback on exit of a child process
108  
109 =item *
110  
111 L<IO::Async::Process> - start and manage a child process
112  
113 =back
114  
115 For more detail, see the SYNOPSIS section in one of the above.
116  
117 One case where this object class would be used, is when a library wishes to
118 provide a sub-component which consists of multiple other C<Notifier>
119 subclasses, such as C<Handle>s and C<Timers>, but no particular object is
120 suitable to be the root of a tree. In this case, a plain C<Notifier> object
121 can be used as the tree root, and all the other notifiers added as children of
122 it.
123  
124 =cut
125  
126 =head1 AS A MIXIN
127  
128 Rather than being used as a subclass this package also supports being used as
129 a non-principle superclass for an object, as a mix-in. It still provides
130 methods and satisfies an C<isa> test, even though the constructor is not
131 directly called. This simply requires that the object be based on a normal
132 blessed hash reference and include C<IO::Async::Notifier> somewhere in its
133 C<@ISA> list.
134  
135 The methods in this class all use only keys in the hash prefixed by
136 C<"IO_Async_Notifier__"> for namespace purposes.
137  
138 This is intended mainly for defining a subclass of some other object that is
139 also an C<IO::Async::Notifier>, suitable to be added to an L<IO::Async::Loop>.
140  
141 package SomeEventSource::Async;
142 use base qw( SomeEventSource IO::Async::Notifier );
143  
144 sub _add_to_loop
145 {
146 my $self = shift;
147 my ( $loop ) = @_;
148  
149 # Code here to set up event handling on $loop that may be required
150 }
151  
152 sub _remove_from_loop
153 {
154 my $self = shift;
155 my ( $loop ) = @_;
156  
157 # Code here to undo the event handling set up above
158 }
159  
160 Since all the methods documented here will be available, the implementation
161 may wish to use the C<configure> and C<make_event_cb> or C<invoke_event>
162 methods to implement its own event callbacks.
163  
164 =cut
165  
166 =head1 EVENTS
167  
168 The following events are invoked, either using subclass methods or CODE
169 references in parameters:
170  
171 =head2 on_error $message, $name, @details
172  
173 Invoked by C<invoke_error>.
174  
175 =cut
176  
177 =head1 PARAMETERS
178  
179 A specific subclass of C<IO::Async::Notifier> defines named parameters that
180 control its behaviour. These may be passed to the C<new> constructor, or to
181 the C<configure> method. The documentation on each specific subclass will give
182 details on the parameters that exist, and their uses. Some parameters may only
183 support being set once at construction time, or only support being changed if
184 the object is in a particular state.
185  
186 The following parameters are supported by all Notifiers:
187  
188 =over 8
189  
190 =item on_error => CODE
191  
192 CODE reference for event handler.
193  
194 =item notifier_name => STRING
195  
196 Optional string used to identify this particular Notifier. This value will be
197 returned by the C<notifier_name> method.
198  
199 =back
200  
201 =cut
202  
203 =head1 CONSTRUCTOR
204  
205 =cut
206  
207 =head2 new
208  
209 $notifier = IO::Async::Notifier->new( %params );
210  
211 This function returns a new instance of a C<IO::Async::Notifier> object with
212 the given initial values of the named parameters.
213  
214 Up until L<IO::Async> version 0.19, this module used to implement the IO
215 handle features now found in the L<IO::Async::Handle> subclass. Code that
216 needs to use any of C<handle>, C<read_handle>, C<write_handle>,
217 C<on_read_ready> or C<on_write_ready> should use L<IO::Async::Handle> instead.
218  
219 =cut
220  
221 sub new
222 {
223 1413 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
224 1413 my %params = @_;
225  
226 1413 my $self = bless {}, $class;
227  
228 1413 $self->_init( \%params );
229  
230 1413 $self->configure( %params );
231  
232 1412 return $self;
233 }
234  
235 =head1 METHODS
236  
237 =cut
238  
239 =head2 configure
240  
241 $notifier->configure( %params );
242  
243 Adjust the named parameters of the C<Notifier> as given by the C<%params>
244 hash.
245  
246 =cut
247  
248 # for subclasses to override and call down to
249 sub configure
250 {
251 1754 ▶ my $self = shift;
Subroutine configure: called CC=4 SCAR=13.9
Pod documented
252 1754 my %params = @_;
253  
254 1754 foreach (qw( notifier_name on_error )) {
255 3508 ▶ $self->{"IO_Async_Notifier__$_"} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 662 2846 3508
256 }
257  
258 1754 ▶ $self->configure_unknown( %params ) if keys %params;
Branch: 1 branch
BranchTrueFalseTotal
if keys %params 2 1752 1754
259 }
260  
261 sub configure_unknown
262 {
263 1 ▶ my $self = shift;
Subroutine configure_unknown: called CC=1 SCAR=0.0
Pod documented
264 1 my %params = @_;
265  
266 1 my $class = ref $self;
267 1 croak "Unrecognised configuration keys for $class - " . join( " ", keys %params );
268 }
269  
270 =head2 loop
271  
272 $loop = $notifier->loop;
273  
274 Returns the L<IO::Async::Loop> that this Notifier is a member of.
275  
276 =cut
277  
278 sub loop
279 {
280 20196 ▶ my $self = shift;
Subroutine loop: called CC=1 SCAR=0.0
Pod documented
281 return $self->{IO_Async_Notifier__loop}
282 20196 }
283  
284 84 *get_loop = \&loop;
285  
286 # Only called by IO::Async::Loop, not external interface
287 sub __set_loop
288 {
289 3104 ▶ my $self = shift;
Subroutine __set_loop: called CC=8 SCAR=20.9
290 3104 my ( $loop ) = @_;
291  
292 # early exit if no change
293 3104 ▶ return if !$loop and !$self->loop or
Branch: 1 branch
BranchTrueFalseTotal
if not $loop || $self->loop or $loop and $self->loop and $loop == $self->loop 564 2540 3104
Condition75%9/12
not $loop || $self->loop or $loop and $self->loop and $loop == $self->loop
l!l&&r!l&&!r
02394710
$loop || $self->loop
l!l&&r!l&&!r
194211620
$loop and $self->loop and $loop == $self->loop
!ll&&!rl&&r
25400564
$loop and $self->loop
!ll&&!rl&&r
11621378564
MC/DC60%3/5
not $loop || $self->loop or $loop and $self->loop and $loop == $self->loop
$loop $self->loop $loop $self->loop $loop == $self->loop
Truth table33%3/9
not(A || B) or C and D and E
A= $loopB= $self->loopC= $loopD= $self->loopE= $loop == $self->loop
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
294 $loop and $self->loop and $loop == $self->loop;
295  
296 2540 ▶ $self->_remove_from_loop( $self->loop ) if $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->loop 1162 1378 2540
297  
298 2540 $self->{IO_Async_Notifier__loop} = $loop;
299 2540 weaken( $self->{IO_Async_Notifier__loop} ); # To avoid a cycle
300  
301 2540 ▶ $self->_add_to_loop( $self->loop ) if $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->loop 1378 1162 2540
302 }
303  
304 =head2 notifier_name
305  
306 $name = $notifier->notifier_name;
307  
308 Returns the name to identify this Notifier. If a has not been set, it will
309 return the empty string. Subclasses may wish to override this behaviour to
310 return some more useful information, perhaps from configured parameters.
311  
312 =cut
313  
314 sub notifier_name
315 {
316 50 ▶ my $self = shift;
Subroutine notifier_name: called CC=2 SCAR=6.9
Pod documented
317 50 ▶ return $self->{IO_Async_Notifier__notifier_name} || "";
Condition100%2/2
$self->{'IO_Async_Notifier__notifier_name'} || ''
l!l
3119
MC/DC100%1/1
$self->{'IO_Async_Notifier__notifier_name'} || ''
$self->{'IO_Async_Notifier__notifier_name'}
Truth table100%2/2
A
A= $self->{'IO_Async_Notifier__notifier_name'}
Aresult
11
00
318 }
319  
320 =head2 adopt_future
321  
322 $f = $notifier->adopt_future( $f );
323  
324 Stores a reference to the L<Future> instance within the notifier itself, so
325 the reference doesn't get lost. This reference will be dropped when the future
326 becomes ready (either by success or failure). Additionally, if the future
327 failed the notifier's C<invoke_error> method will be informed.
328  
329 This means that if the notifier does not provide an C<on_error> handler, nor
330 is there one anywhere in the parent chain, this will be fatal to the caller of
331 C<< $f->fail >>. To avoid this being fatal if the failure is handled
332 elsewhere, use the C<else_done> method on the future to obtain a sequence one
333 that never fails.
334  
335 $notifier->adopt_future( $f->else_done() );
336  
337 The future itself is returned.
338  
339 =cut
340  
341 sub adopt_future
342 {
343 56 ▶ my $self = shift;
Subroutine adopt_future: called CC=1 SCAR=0.0
Pod documented
344 56 my ( $f ) = @_;
345  
346 56 my $fkey = "$f"; # stable stringification
347  
348 56 $self->{IO_Async_Notifier__futures}{$fkey} = $f;
349  
350 $f->on_ready( $self->_capture_weakself( sub {
351 56 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
352 56 my ( $f ) = @_;
353  
354 56 delete $self->{IO_Async_Notifier__futures}{$fkey};
355  
356 56 ▶ $self->invoke_error( $f->failure ) if $f->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_failed 1 55 56
357 56 }));
358  
359 56 return $f;
360 }
361  
362 =head2 adopted_futures
363  
364 @f = $notifier->adopted_futures;
365  
366 I<Since version 0.73.>
367  
368 Returns a list of all the adopted and still-pending futures, in no particular
369 order.
370  
371 =cut
372  
373 sub adopted_futures
374 {
375 3 ▶ my $self = shift;
Subroutine adopted_futures: called CC=1 SCAR=0.0
Pod documented
376  
377 3 return values %{ $self->{IO_Async_Notifier__futures} };
378 }
379  
380 =head1 CHILD NOTIFIERS
381  
382 During the execution of a program, it may be the case that certain IO handles
383 cause other handles to be created; for example, new sockets that have been
384 C<accept()>ed from a listening socket. To facilitate these, a notifier may
385 contain child notifier objects, that are automatically added to or removed
386 from the L<IO::Async::Loop> that manages their parent.
387  
388 =cut
389  
390 =head2 parent
391  
392 $parent = $notifier->parent;
393  
394 Returns the parent of the notifier, or C<undef> if does not have one.
395  
396 =cut
397  
398 sub parent
399 {
400 3328 ▶ my $self = shift;
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
401 3328 return $self->{IO_Async_Notifier__parent};
402 }
403  
404 =head2 children
405  
406 @children = $notifier->children;
407  
408 Returns a list of the child notifiers contained within this one.
409  
410 =cut
411  
412 sub children
413 {
414 3073 ▶ my $self = shift;
Subroutine children: called CC=2 SCAR=6.9
Pod documented
415 3073 ▶ return unless $self->{IO_Async_Notifier__children};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"IO_Async_Notifier__children"} 2411 662 3073
416 662 return @{ $self->{IO_Async_Notifier__children} };
417 }
418  
419 =head2 add_child
420  
421 $notifier->add_child( $child );
422  
423 Adds a child notifier. This notifier will be added to the containing loop, if
424 the parent has one. Only a notifier that does not currently have a parent and
425 is not currently a member of any loop may be added as a child. If the child
426 itself has grandchildren, these will be recursively added to the containing
427 loop.
428  
429 =cut
430  
431 sub add_child
432 {
433 647 ▶ my $self = shift;
Subroutine add_child: called CC=4 SCAR=13.9
Pod documented
434 647 my ( $child ) = @_;
435  
436 647 ▶ croak "Cannot add a child that already has a parent" if defined $child->{IO_Async_Notifier__parent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $child->{"IO_Async_Notifier__parent"} 1 646 647
437  
438 646 ▶ croak "Cannot add a child that is already a member of a loop" if defined $child->loop;
Branch: 1 branch
BranchTrueFalseTotal
if defined $child->loop 0 646 646
439  
440 646 ▶ if( defined( my $loop = $self->loop ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $loop = $self->loop)) 624 22 646
441 624 $loop->add( $child );
442 }
443  
444 641 push @{ $self->{IO_Async_Notifier__children} }, $child;
445 641 $child->{IO_Async_Notifier__parent} = $self;
446 641 weaken( $child->{IO_Async_Notifier__parent} );
447  
448 641 return;
449 }
450  
451 =head2 remove_child
452  
453 $notifier->remove_child( $child );
454  
455 Removes a child notifier. The child will be removed from the containing loop,
456 if the parent has one. If the child itself has grandchildren, these will be
457 recurively removed from the loop.
458  
459 =cut
460  
461 sub remove_child
462 {
463 374 ▶ my $self = shift;
Subroutine remove_child: called CC=4 SCAR=13.9
Pod documented
464 374 my ( $child ) = @_;
465  
466 LOOP: {
467 374 my $childrenref = $self->{IO_Async_Notifier__children};
468 374 for my $i ( 0 .. $#$childrenref ) {
469 84 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
470 407 ▶ next unless $childrenref->[$i] == $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $childrenref->[$i] == $child 33 374 407
471 374 splice @$childrenref, $i, 1, ();
472 374 last LOOP;
473 }
474  
475 0 croak "Cannot remove child from a parent that doesn't contain it";
476 }
477  
478 374 undef $child->{IO_Async_Notifier__parent};
479  
480 374 ▶ if( defined( my $loop = $self->loop ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $loop = $self->loop)) 295 79 374
481 295 $loop->remove( $child );
482 }
483 }
484  
485 =head2 remove_from_parent
486  
487 $notifier->remove_from_parent;
488  
489 Removes this notifier object from its parent (either another notifier object
490 or the containing loop) if it has one. If the notifier is not a child of
491 another notifier nor a member of a loop, this method does nothing.
492  
493 =cut
494  
495 sub remove_from_parent
496 {
497 797 ▶ my $self = shift;
Subroutine remove_from_parent: called CC=3 SCAR=11.0
Pod documented
498  
499 797 ▶ if( my $parent = $self->parent ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $parent = $self->parent) { } 321 476 797
elsif (my $loop = $self->loop) { } 471 5 476
500 321 $parent->remove_child( $self );
501 }
502 elsif( my $loop = $self->loop ) {
503 471 $loop->remove( $self );
504 }
505 }
506  
507 =head1 SUBCLASS METHODS
508  
509 C<IO::Async::Notifier> is a base class provided so that specific subclasses of
510 it provide more specific behaviour. The base class provides a number of
511 methods that subclasses may wish to override.
512  
513 If a subclass implements any of these, be sure to invoke the superclass method
514 at some point within the code.
515  
516 =cut
517  
518 =head2 _init
519  
520 $notifier->_init( $paramsref );
521  
522 This method is called by the constructor just before calling C<configure>.
523 It is passed a reference to the HASH storing the constructor arguments.
524  
525 This method may initialise internal details of the Notifier as required,
526 possibly by using parameters from the HASH. If any parameters are
527 construction-only they should be C<delete>d from the hash.
528  
529 =cut
530  
531 sub _init
532 ▶{
Subroutine _init: called CC=1 SCAR=0.0
533 # empty default
534 }
535  
536 =head2 configure
537  
538 $notifier->configure( %params );
539  
540 This method is called by the constructor to set the initial values of named
541 parameters, and by users of the object to adjust the values once constructed.
542  
543 This method should C<delete> from the C<%params> hash any keys it has dealt
544 with, then pass the remaining ones to the C<SUPER::configure>. The base
545 class implementation will throw an exception if there are any unrecognised
546 keys remaining.
547  
548 =cut
549  
550 =head2 configure_unknown
551  
552 $notifier->configure_unknown( %params );
553  
554 This method is called by the base class C<configure> method, for any remaining
555 parameters that are not recognised. The default implementation throws an
556 exception using C<Carp> that lists the unrecognised keys. This method is
557 provided to allow subclasses to override the behaviour, perhaps to store
558 unrecognised keys, or to otherwise inspect the left-over arguments for some
559 other purpose.
560  
561 =cut
562  
563 =head2 _add_to_loop
564  
565 $notifier->_add_to_loop( $loop );
566  
567 This method is called when the Notifier has been added to a Loop; either
568 directly, or indirectly through being a child of a Notifer already in a loop.
569  
570 This method may be used to perform any initial startup activity required for
571 the Notifier to be fully functional but which requires a Loop to do so.
572  
573 =cut
574  
575 sub _add_to_loop
576 ▶{
Subroutine _add_to_loop: called CC=1 SCAR=0.0
577 # empty default
578 }
579  
580 =head2 _remove_from_loop
581  
582 $notifier->_remove_from_loop( $loop );
583  
584 This method is called when the Notifier has been removed from a Loop; either
585 directly, or indirectly through being a child of a Notifier removed from the
586 loop.
587  
588 This method may be used to undo the effects of any setup that the
589 C<_add_to_loop> method had originally done.
590  
591 =cut
592  
593 sub _remove_from_loop
594 ▶{
Subroutine _remove_from_loop: called CC=1 SCAR=0.0
595 # empty default
596 }
597  
598 =head1 UTILITY METHODS
599  
600 =cut
601  
602 =head2 _capture_weakself
603  
604 $mref = $notifier->_capture_weakself( $code );
605  
606 Returns a new CODE ref which, when invoked, will invoke the originally-passed
607 ref, with additionally a reference to the Notifier as its first argument. The
608 Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
609 stored in the Notifier itself without creating a cycle.
610  
611 For example,
612  
613 my $mref = $notifier->_capture_weakself( sub {
614 my ( $notifier, $arg ) = @_;
615 print "Notifier $notifier got argument $arg\n";
616 } );
617  
618 $mref->( 123 );
619  
620 This is provided as a utility for Notifier subclasses to use to build a
621 callback CODEref to pass to a Loop method, but which may also want to store
622 the CODE ref internally for efficiency.
623  
624 The C<$code> argument may also be a plain string, which will be used as a
625 method name; the returned CODE ref will then invoke that method on the object.
626 In this case the method name is stored symbolically in the returned CODE
627 reference, and dynamically dispatched each time the reference is invoked. This
628 allows it to follow code reloading, dynamic replacement of class methods, or
629 other similar techniques.
630  
631 If the C<$mref> CODE reference is being stored in some object other than the
632 one it refers to, remember that since the Notifier is only weakly captured, it
633 is possible that it has been destroyed by the time the code runs, and so the
634 reference will be passed as C<undef>. This should be protected against by the
635 code body.
636  
637 $other_object->{on_event} = $notifier->_capture_weakself( sub {
638 my $notifier = shift or return;
639 my ( @event_args ) = @_;
640 ...
641 } );
642  
643 For stand-alone generic implementation of this behaviour, see also L<curry>
644 and C<curry::weak>.
645  
646 =cut
647  
648 sub _capture_weakself
649 {
650 2180 ▶ my $self = shift;
Subroutine _capture_weakself: called CC=3 SCAR=11.0
651 2180 my ( $code ) = @_; # actually bare method names work too
652  
653 2180 ▶ if( !ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code) 130 2050 2180
654 130 my $class = ref $self;
655 # Don't save this coderef, or it will break dynamic method dispatch,
656 # which means code reloading, dynamic replacement, or other funky
657 # techniques stop working
658 130 ▶ $self->can( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->can($code) or croak(qq[Can't locate object method "$code" via package "$class"]) 1 129 130
659 croak qq(Can't locate object method "$code" via package "$class");
660 }
661  
662 2179 weaken $self;
663  
664 return sub {
665 2245 ▶ my $cv = ref( $code ) ? $code : $self->can( $code );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $code ? : 2133 112 2245
666  
667 2245 if( HAS_BROKEN_TRAMPOLINES ) {
668 return $cv->( $self, @_ );
669 }
670 else {
671 2245 unshift @_, $self;
672 2245 goto &$cv;
673 }
674 2179 };
675 }
676  
677 =head2 _replace_weakself
678  
679 $mref = $notifier->_replace_weakself( $code );
680  
681 Returns a new CODE ref which, when invoked, will invoke the originally-passed
682 ref, with a reference to the Notifier replacing its first argument. The
683 Notifier reference is stored weakly in C<$mref>, so this CODE ref may be
684 stored in the Notifier itself without creating a cycle.
685  
686 For example,
687  
688 my $mref = $notifier->_replace_weakself( sub {
689 my ( $notifier, $arg ) = @_;
690 print "Notifier $notifier got argument $arg\n";
691 } );
692  
693 $mref->( $object, 123 );
694  
695 This is provided as a utility for Notifier subclasses to use for event
696 callbacks on other objects, where the delegated object is passed in the
697 function's arguments.
698  
699 The C<$code> argument may also be a plain string, which will be used as a
700 method name; the returned CODE ref will then invoke that method on the object.
701 As with C<_capture_weakself> this is stored symbolically.
702  
703 As with C<_capture_weakself>, care should be taken against Notifier
704 destruction if the C<$mref> CODE reference is stored in some other object.
705  
706 =cut
707  
708 sub _replace_weakself
709 {
710 185 ▶ my $self = shift;
Subroutine _replace_weakself: called CC=3 SCAR=11.0
711 185 my ( $code ) = @_; # actually bare method names work too
712  
713 185 ▶ if( !ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code) 14 171 185
714 # Don't save this coderef, see _capture_weakself for why
715 14 my $class = ref $self;
716 14 ▶ $self->can( $code ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->can($code) or croak(qq[Can't locate object method "$code" via package "$class"]) 0 14 14
717 croak qq(Can't locate object method "$code" via package "$class");
718 }
719  
720 185 weaken $self;
721  
722 return sub {
723 41 ▶ my $cv = ref( $code ) ? $code : $self->can( $code );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $code ? : 31 10 41
724  
725 41 if( HAS_BROKEN_TRAMPOLINES ) {
726 return $cv->( $self, @_[1..$#_] );
727 }
728 else {
729 # Don't assign to $_[0] directly or we will change caller's first argument
730 41 shift @_;
731 41 unshift @_, $self;
732 41 goto &$cv;
733 }
734 185 };
735 }
736  
737 =head2 can_event
738  
739 $code = $notifier->can_event( $event_name );
740  
741 Returns a C<CODE> reference if the object can perform the given event name,
742 either by a configured C<CODE> reference parameter, or by implementing a
743 method. If the object is unable to handle this event, C<undef> is returned.
744  
745 =cut
746  
747 sub can_event
748 {
749 5223 ▶ my $self = shift;
Subroutine can_event: called CC=2 SCAR=6.9
Pod documented
750 5223 my ( $event_name ) = @_;
751  
752 5223 ▶ return $self->{$event_name} || $self->can( $event_name );
Condition100%3/3
$self->{$event_name} || $self->can($event_name)
l!l&&r!l&&!r
251214861225
MC/DC100%2/2
$self->{$event_name} || $self->can($event_name)
$self->{$event_name} $self->can($event_name)
Truth table100%3/3
A || B
A= $self->{$event_name}B= $self->can($event_name)
ABresult
1X1
011
000
753 }
754  
755 =head2 make_event_cb
756  
757 $callback = $notifier->make_event_cb( $event_name );
758  
759 Returns a C<CODE> reference which, when invoked, will execute the given event
760 handler. Event handlers may either be subclass methods, or parameters given to
761 the C<new> or C<configure> method.
762  
763 The event handler can be passed extra arguments by giving them to the C<CODE>
764 reference; the first parameter received will be a reference to the notifier
765 itself. This is stored weakly in the closure, so it is safe to store the
766 resulting C<CODE> reference in the object itself without causing a reference
767 cycle.
768  
769 =cut
770  
771 sub make_event_cb
772 {
773 731 ▶ my $self = shift;
Subroutine make_event_cb: called CC=3 SCAR=13.0
Pod documented
774 731 my ( $event_name ) = @_;
775  
776 731 ▶ my $code = $self->can_event( $event_name )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can_event($event_name) or croak("$self cannot handle $event_name event") 0 731 731
777 or croak "$self cannot handle $event_name event";
778  
779 731 my $caller = caller;
780  
781 return $self->_capture_weakself(
782 !$IO::Async::Debug::DEBUG ? $code : sub {
783 0 ▶ my $self = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
784 0 $self->_debug_printf_event( $caller, $event_name );
785 0 goto &$code;
786 }
787 731 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
!$IO::Async::Debug::DEBUG ? : 731 0 731
788 }
789  
790 =head2 maybe_make_event_cb
791  
792 $callback = $notifier->maybe_make_event_cb( $event_name );
793  
794 Similar to C<make_event_cb> but will return C<undef> if the object cannot
795 handle the named event, rather than throwing an exception.
796  
797 =cut
798  
799 sub maybe_make_event_cb
800 {
801 2 ▶ my $self = shift;
Subroutine maybe_make_event_cb: called CC=3 SCAR=12.0
Pod documented
802 2 my ( $event_name ) = @_;
803  
804 2 ▶ my $code = $self->can_event( $event_name )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can_event($event_name) or return undef 1 1 2
805 or return undef;
806  
807 1 my $caller = caller;
808  
809 return $self->_capture_weakself(
810 !$IO::Async::Debug::DEBUG ? $code : sub {
811 0 ▶ my $self = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
812 0 $self->_debug_printf_event( $caller, $event_name );
813 0 goto &$code;
814 }
815 1 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
!$IO::Async::Debug::DEBUG ? : 1 0 1
816 }
817  
818 =head2 invoke_event
819  
820 @ret = $notifier->invoke_event( $event_name, @args );
821  
822 Invokes the given event handler, passing in the given arguments. Event
823 handlers may either be subclass methods, or parameters given to the C<new> or
824 C<configure> method. Returns whatever the underlying method or CODE reference
825 returned.
826  
827 =cut
828  
829 sub invoke_event
830 {
831 1372 ▶ my $self = shift;
Subroutine invoke_event: called CC=3 SCAR=11.3
Pod documented
832 1372 my ( $event_name, @args ) = @_;
833  
834 1372 ▶ my $code = $self->can_event( $event_name )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can_event($event_name) or croak("$self cannot handle $event_name event") 0 1372 1372
835 or croak "$self cannot handle $event_name event";
836  
837 1372 ▶ $self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Async::Debug::DEBUG 0 1372 1372
838 1372 return $code->( $self, @args );
839 }
840  
841 =head2 maybe_invoke_event
842  
843 $retref = $notifier->maybe_invoke_event( $event_name, @args );
844  
845 Similar to C<invoke_event> but will return C<undef> if the object cannot
846 handle the name event, rather than throwing an exception. In order to
847 distinguish this from an event-handling function that simply returned
848 C<undef>, if the object does handle the event, the list that it returns will
849 be returned in an ARRAY reference.
850  
851 =cut
852  
853 sub maybe_invoke_event
854 {
855 1243 ▶ my $self = shift;
Subroutine maybe_invoke_event: called CC=3 SCAR=11.0
Pod documented
856 1243 my ( $event_name, @args ) = @_;
857  
858 1243 ▶ my $code = $self->can_event( $event_name )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can_event($event_name) or return undef 1168 75 1243
859 or return undef;
860  
861 75 ▶ $self->_debug_printf_event( scalar caller, $event_name ) if $IO::Async::Debug::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Async::Debug::DEBUG 0 75 75
862 75 return [ $code->( $self, @args ) ];
863 }
864  
865 =head1 DEBUGGING SUPPORT
866  
867 =cut
868  
869 =head2 debug_printf
870  
871 $notifier->debug_printf( $format, @args );
872  
873 Conditionally print a debugging message to C<STDERR> if debugging is enabled.
874 If such a message is printed, it will be printed using C<printf> using the
875 given format and arguments. The message will be prefixed with a string, in
876 square brackets, to help identify the C<$notifier> instance. This string will
877 be the class name of the notifier, and any parent notifiers it is contained
878 by, joined by an arrow C<< <- >>. To ensure this string does not grow too
879 long, certain prefixes are abbreviated:
880  
881 IO::Async::Protocol:: => IaP:
882 IO::Async:: => Ia:
883 Net::Async:: => Na:
884  
885 Finally, each notifier that has a name defined using the C<notifier_name>
886 parameter has that name appended in braces.
887  
888 For example, invoking
889  
890 $stream->debug_printf( "EVENT on_read" );
891  
892 On an L<IO::Async::Stream> instance reading and writing a file descriptor
893 whose C<fileno> is 4, which is a child of an L<IO::Async::Protocol::Stream>,
894 will produce a line of output:
895  
896 [Ia:Stream{rw=4}<-IaP:Stream] EVENT on_read
897  
898 =cut
899  
900 sub debug_printf
901 {
902 1080 ▶ $IO::Async::Debug::DEBUG or return;
Subroutine debug_printf: called CC=6 SCAR=34.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$IO::Async::Debug::DEBUG or return 1080 0 1080
903  
904 0 my $self = shift;
905 0 my ( $format, @args ) = @_;
906  
907 0 my @id;
908 0 ▶ while( $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
909 0 push @id, ref $self;
910  
911 0 my $name = $self->notifier_name;
912 0 ▶ $id[-1] .= "{$name}" if defined $name and length $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
913  
914 0 $self = $self->parent;
915 }
916  
917 s/^IO::Async::Protocol::/IaP:/,
918 s/^IO::Async::/Ia:/,
919 0 s/^Net::Async::/Na:/ for @id;
920  
921 0 IO::Async::Debug::logf "[%s] $format\n", join("<-", @id), @args;
922 }
923  
924 sub _debug_printf_event
925 {
926 0 ▶ my $self = shift;
Subroutine _debug_printf_event: not called CC=5 SCAR=34.0
927 0 my ( $caller, $event_name ) = @_;
928  
929 0 my $class = ref $self;
930  
931 0 ▶ if( $IO::Async::Debug::DEBUG > 1 or $class eq $caller ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Async::Debug::DEBUG > 1 or $class eq $caller) 0 0 0
Condition0%0/3
$IO::Async::Debug::DEBUG > 1 or $class eq $caller
l!l&&r!l&&!r
000
MC/DC0%0/2
$IO::Async::Debug::DEBUG > 1 or $class eq $caller
$IO::Async::Debug::DEBUG > 1 $class eq $caller
Truth table0%0/3
A or B
A= $IO::Async::Debug::DEBUG > 1B= $class eq $caller
ABresult
1X1
011
000
932 s/^IO::Async::Protocol::/IaP:/,
933 s/^IO::Async::/Ia:/,
934 0 s/^Net::Async::/Na:/ for my $str_caller = $caller;
935  
936 0 ▶ $self->debug_printf( "EVENT %s",
Branch: 1 branch
BranchTrueFalseTotal
$class eq $caller ? : 0 0 0
937 ( $class eq $caller ? $event_name : "${str_caller}::$event_name" )
938 );
939 }
940 }
941  
942 =head2 invoke_error
943  
944 $notifier->invoke_error( $message, $name, @details );
945  
946 Invokes the stored C<on_error> event handler, passing in the given arguments.
947 If no handler is defined, it will be passed up to the containing parent
948 notifier, if one exists. If no parent exists, the error message will be thrown
949 as an exception by using C<die()> and this method will not return.
950  
951 If a handler is found to handle this error, the method will return as normal.
952 However, as the expected use-case is to handle "fatal" errors that now render
953 the notifier unsuitable to continue, code should be careful not to perform any
954 further work after invoking it. Specifically, sockets may become disconnected,
955 or the entire notifier may now be removed from its containing loop.
956  
957 The C<$name> and C<@details> list should follow similar semantics to L<Future>
958 failures. That is, the C<$name> should be a string giving a category of
959 failure, and the C<@details> list should contain additional arguments that
960 relate to that kind of failure.
961  
962 =cut
963  
964 sub invoke_error
965 {
966 5 ▶ my $self = shift;
Subroutine invoke_error: called CC=4 SCAR=13.9
Pod documented
967 5 my ( $message, $name, @details ) = @_;
968  
969 5 ▶ if( my $code = $self->{IO_Async_Notifier__on_error} || $self->can( "on_error" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->{"IO_Async_Notifier__on_error"} || $self->can("on_error")) 3 2 5
Condition66%2/3
$self->{'IO_Async_Notifier__on_error'} || $self->can('on_error')
l!l&&r!l&&!r
302
MC/DC50%1/2
$self->{'IO_Async_Notifier__on_error'} || $self->can('on_error')
$self->{'IO_Async_Notifier__on_error'} $self->can('on_error')
Truth table66%2/3
A || B
A= $self->{'IO_Async_Notifier__on_error'}B= $self->can('on_error')
ABresult
1X1
011
000
970 3 return $code->( $self, $message, $name, @details );
971 }
972  
973 2 ▶ if( my $parent = $self->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) 1 1 2
974 1 return $parent->invoke_error( @_ );
975 }
976  
977 1 die "$message\n";
978 }
979  
980 =head1 AUTHOR
981  
982 Paul Evans <leonerd@leonerd.org.uk>
983  
984 =cut
985  
986 84 0x55AA;
« blib/lib/IO/Async/FileStream.pm ↑ Summary blib/lib/IO/Async/Timer/Countdown.pm »