The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

UniEvent::FsPoll - monitor a given path for changes

SYNOPSIS

    my $h = UniEvent::FsPoll->new;
    
    # check every 5.5 seconds
    $h->start('/path/to/file', 5.5, sub {
        my ($handle, $prev_stat, $cur_stat, $error) = @_;
        if (!$error && $cur_stat->[UE::Fs::STAT_MTIME] != $prev_stat->[UE::Fs::STAT_MTIME]) {
            say "The contents of file has been modified";
        }
    });
    
    $h->start_callback(sub {
        my ($handle, $initial_stat, $error) = @_;
        # do something on the initial call with $initial_stat
    });

DESCRIPTION

FsPoll handles allow the user to monitor a given path for changes. Unlike UniEvent::FsEvent, fs poll handles use stat() to detect when a file has changed so they can work on file systems where fs event handles can't.

FsPoll periodically polls filesystem for the specified file metadata (i.e. change time, file size etc.) and invoke the user-specified callback with the current and previous metadata (file stats).

The UniEvent::FsPoll is inherited from UniEvent::Handle.

METHODS

All methods of UniEvent::Handle also apply.

create($path, $interval, $callback, [$loop = default])

    my $handle = UniEvent::FsPoll->create("my/file", 1, sub { say "hi" });

Creates and starts an fs poll handle. Alias for new($loop) + start($path, $interval, $callback).

new([$loop = default])

Constructs new FsPoll handle and binds it to the specified event loop.

start($path, [$interval = 1.0], [$callback])

Stars monitoring the specified $path for changes. Each filesystem poll would occur every $interval second(s).

If $callback is present, it is added as poll_event()->add($cb)

stop()

Stops monitoring.

poll_callback($sub)

poll_event()

Callbacks set via these methods will be invoked periodically, on file change.

Callback signature:

    my ($handle, $prev_stat, $cur_stat, $error) = @_;

Where $handle is the FsPoll handle object itself.

$prev_stat and $cur_stat are arrayrefs with the previous and new file stat data. Format is similar to what perl's stat() returns. See UniEvent::Fs's stat() for more details on stat structure.

The $err parameter will be an XS::ErrorCode object if any. If it is present, the contents of $prev_stat and/or $cur_stat may be undefined.

Error is considered part of file state. File might get deleted or become inaccessible and in this case, callback will be called with defined $prev_stat, with error and with undefined $cur_stat. And vice-versa, after that file might get created again in which case callback will be called with undefined $prev_stat, without error and with defined $cur_stat.

See "EVENT CALLBACKS" in UniEvent

start_callback($sub)

start_event()

Callbacks set via these methods will be invoked once after monitoring starts.

Callback signature:

    my ($handle, $initial_stat, $error) = @_;

Where $handle is the FsPoll handle object itself and $initial_stat is the initial file stat. It may be undefined in case if file does not exist or is inaccessible (in which case the error will also be set).

The $err parameter will be an XS::ErrorCode object if any. If it is present, the contents of $initial_stat may be undefined.

See "EVENT CALLBACKS" in UniEvent

event_listener($delegate, [$weak])

Methods on_fs_poll, on_fs_start will be called.

See "EVENT LISTENER" in UniEvent

path()

Returns the currently monitored path.