UV::Idle - Idle handles in libuv
#!/usr/bin/env perl use strict; use warnings; use UV; # A new handle will be initialized against the default loop my $idle = UV::Idle->new(); # Use a different loop my $loop = UV::Loop->new(); # non-default loop my $idle = UV::Idle->new($loop); # setup the idle callback: $idle->on(idle => sub {say "We're IDLING!!!"}); # start the handle $idle->start(); # or, with an explicit callback defined $idle->start(sub {say "override any 'idle' callback we already have"}); # stop the check $idle->stop();
This module provides an interface to libuv's idle handle.
Idle handles will run the given callback once per loop iteration, right before the UV::Prepare handles.
* Note: The notable difference with UV::Prepare handles is that when there are active UV::Idle handles, the loop will perform a zero timeout poll instead of blocking for i/o.
UV::Idle inherits all events from UV::Handle and also makes the following extra events available.
$handle->on(idle => sub { my $invocant = shift; say "We are idling!"}); my $count = 0; $handle->on("idle", sub { my $invocant = shift; # the handle instance this event fired on if (++$count > 2) { say "We've idled twice. stopping!"; $invocant->stop(); } });
When the event loop runs and the idle is ready, this event will be fired.
* Note: Despite the name, UV::Idle handles will get their callbacks called on every loop iteration, not when the loop is actually "idle".
UV::Idle inherits all methods from UV::Handle and also makes the following extra methods available.
my $idle = UV::Idle->new(); # Or tell it what loop to initialize against my $idle = UV::Idle->new($loop);
This constructor method creates a new UV::Idle object and initializes the handle with the given UV::Loop. If no UV::Loop is provided, then the "default_loop" in UV::Loop is assumed.
# start the handle with the callback we supplied with ->on() or with no cb $idle->start(); # pass a callback for the "idle" event $idle->start(sub {say "yay"}); # providing the callback above completely overrides any callback previously # set in the ->on() method. It's equivalent to: $idle->on(idle => sub {say "yay"}); $idle->start();
The start method starts the handle.
$idle->stop();
The stop method stops the handle. The callback will no longer be called.
Chase Whitener <capoeirab@cpan.org>
Daisuke Murase <typester@cpan.org>
Copyright 2012, Daisuke Murase.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install UV, copy and paste the appropriate command in to your terminal.
cpanm
cpanm UV
CPAN shell
perl -MCPAN -e shell install UV
For more information on module installation, please visit the detailed CPAN module installation guide.