The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

UV::Idle - Idle handles in libuv

SYNOPSIS

  #!/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 => $loop,
    on_close => sub {say "close!"},
    on_idle => sub {say "idle!"},
  );

  # 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();

DESCRIPTION

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.

EVENTS

UV::Idle inherits all events from UV::Handle and also makes the following extra events available.

idle

    $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".

METHODS

UV::Idle inherits all methods from UV::Handle and also makes the following extra methods available.

new

    my $idle = UV::Idle->new();
    # Or tell it what loop to initialize against
    my $idle = UV::Idle->new(
        loop => $loop,
        on_close => sub {say "close!"},
        on_idle => sub {say "idle!"},
    );

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

    # 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.

Returns the $idle instance itself.

stop

    $idle->stop();

The stop method stops the handle. The callback will no longer be called.

AUTHOR

Chase Whitener <capoeirab@cpan.org>

AUTHOR EMERITUS

Daisuke Murase <typester@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2012, Daisuke Murase.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.