1CPS::Governor(3) User Contributed Perl Documentation CPS::Governor(3)
2
3
4
6 "CPS::Governor" - control the iteration of the "CPS" functions
7
9 Objects based on this abstract class are used by the "gk*" variants of
10 the CPS functions, to control their behavior. These objects are
11 expected to provide a method, "again", which the functions will use to
12 re-invoke iterations of loops, and so on. By providing a different
13 implementation of this method, governor objects can provide such
14 behaviours as rate-limiting, asynchronisation or parallelism, and
15 integration with event-based IO frameworks.
16
18 $gov = CPS::Governor->new
19 Must be called on a subclass which implements the "again" method.
20 Returns a new instance of a governor object in that class.
21
23 Because this is an abstract class, instances of it can only be
24 constructed on a subclass which implements the following methods:
25
26 $gov->again( $code, @args )
27 Execute the function given in the "CODE" reference $code, passing in
28 the arguments @args. If this is going to be executed immediately, it
29 should be invoked using a tail-call directly by the "again" method, so
30 that the stack does not grow arbitrarily. This can be achieved by, for
31 example:
32
33 @_ = @args;
34 goto &$code;
35
36 Alternatively, the Sub::Call::Tail may be used to apply syntactic
37 sugar, allowing you to write instead:
38
39 use Sub::Call::Tail;
40 ...
41 tail $code->( @args );
42
44 A Governor With A Time Delay
45 Consider the following subclass, which implements a "CPS::Governor"
46 subclass that calls "sleep()" between every invocation.
47
48 package Governor::Sleep
49
50 use base qw( CPS::Governor );
51
52 sub new
53 {
54 my $class = shift;
55 my ( $delay ) = @_;
56
57 my $self = $class->SUPER::new;
58 $self->{delay} = $delay;
59
60 return $self;
61 }
62
63 sub again
64 {
65 my $self = shift;
66 my $code = shift;
67
68 sleep $self->{delay};
69
70 # @args are still in @_
71 goto &$code;
72 }
73
75 • Sub::Call::Tail - Tail calls for subroutines and methods
76
78 Paul Evans <leonerd@leonerd.org.uk>
79
80
81
82perl v5.34.0 2021-07-22 CPS::Governor(3)