CPS::Governor(3pm)
β¨ NAME β¨
CPS::Governor β bestie, this is the thing that controls how the CPS functions iterate π
(it's a User Contributed Perl Documentation page, and yes it lives at CPS::Governor(3pm), we're fancy like that)
π DESCRIPTION π
Okay so listen. Objects built on this abstract class get handed to the gk* variants of the CPS functions, and their whole job is controlling how those functions behave. The vibe is: these objects are expected to provide a method called again, and the functions will call it to re-invoke iterations of loops and stuff like that.
And the iconic part? Because you're writing again yourself, governor objects can give you all kinds of behaviours β rate-limiting, asynchronisation, parallelism, or even integration with event-based IO frameworks. It's giving β¨main character energyβ¨ for your control flow.
π CONSTRUCTOR π
$gov = CPS::Governor->new
You must call this on a subclass that actually implements the again method β no shortcuts, bestie, this class is abstract fr fr. It hands you back a brand new instance of a governor object in that class. π«Ά
π¦ SUBCLASS METHODS π¦
Because this is an abstract class, you literally cannot construct instances of it directly β only on a subclass that implements the following methods. No cap. π
$gov->again( $code, @args )
Run the function that's chilling in the CODE reference $code, passing in the arguments @args.
Now here's the tea β β if you're going to execute that immediately, it should be invoked using a tail-call directly by the again method, so that the stack does not grow arbitrarily. Yes, arbitrarily. Don't do that to your stack, she's been through enough.
You can pull that off like this:
@_ = @args;
goto &$code;
Alternatively, Sub::Call::Tail can be used to apply some syntactic sugar, which lets you write this instead:
use Sub::Call::Tail;
...
tail $code->( @args );
Look at that. Gorgeous. ππ
π EXAMPLES π
A Governor With A Time Delay πΈ
Consider the following subclass β it implements a CPS::Governor subclass that calls sleep() between every single invocation. Every. Single. One. π
package Governor::Sleep
use base qw( CPS::Governor );
sub new
{
my $class = shift;
my ( $delay ) = @_;
my $self = $class->SUPER::new;
$self->{delay} = $delay;
return $self;
}
sub again
{
my $self = shift;
my $code = shift;
sleep $self->{delay};
# @args are still in @_
goto &$code;
}
Note the little comment in there:@argsare still in@_β that's the whole trick, bestie, that's why thegoto &$codeworks so cleanly. π§
β SEE ALSO β
Sub::Call::Tailβ Tail calls for subroutines and methods
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk> β the icon, the legend π
And that's a wrap, bestie! This page came to you courtesy of perl v5.34.0, dated 2022-06-30, and it's CPS::Governor(3pm) β go forth and govern your iterations, you absolute legend. Slay. πβ¨
π¦ where this page comes from
- debian bookworm: libcps-perl 0.19-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcps-perl 0.19-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcps-perl 0.19-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcps-perl 0.19-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcps-perl 0.19-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms