NAME

Linux::Event::Loop - Backend-agnostic Linux event loop (Clock + Timer + Scheduler)

SYNOPSIS

use v5.36;
use Linux::Event;

my $loop = Linux::Event->new;

my $conn = My::Conn->new(...);

my $w = $loop->watch($fh,
  read  => \&My::Conn::on_read,
  write => \&My::Conn::on_write,
  data  => $conn,
);

$w->disable_write;  # enable later when output buffered

$loop->after(0.250, sub ($loop) {
  warn "250ms elapsed\n";
});

$loop->run;

DESCRIPTION

Linux::Event::Loop ties together:

The loop owns policy (scheduling, timer rearm, dispatch ordering). The backend owns readiness waiting and dispatch mechanism.

STATUS

EXPERIMENTAL / WORK IN PROGRESS

This API is a developer release and may change without notice.

TIMERS

Timers are scheduled using seconds (float allowed). Internally, the scheduler stores deadlines in nanoseconds.

after($seconds, $cb) -> $id

Schedule $cb to run after $seconds. The callback is invoked as:

$cb->($loop)

at($deadline_seconds, $cb) -> $id

Schedule $cb for an absolute monotonic deadline in seconds (same timebase as $loop->clock->now_ns).

cancel($id) -> $bool

Cancel a scheduled timer.

WATCHERS

watch($fh, %spec) -> Linux::Event::Watcher

Create a mutable watcher for a filehandle. Interest is inferred from installed handlers and enable/disable state.

Supported keys in %spec:

  • read - coderef (optional)

  • write - coderef (optional)

  • data - user data (optional). Use this to avoid closure captures.

  • edge_triggered - boolean (optional, advanced). Defaults to false.

  • oneshot - boolean (optional, advanced). Defaults to false.

Handlers are invoked as:

read  => sub ($loop, $fh, $watcher) { ... }
write => sub ($loop, $fh, $watcher) { ... }

The watcher can be modified later using its methods (enable/disable write, swap handlers, cancel, etc.).

LOOP CONTROL

run

Run until stopped.

run_once($timeout_s=undef)

Run a single wait/dispatch cycle.

stop

Stop a running loop.

REPOSITORY

The project repository is hosted on GitHub:

https://github.com/haxmeister/perl-linux-event