Async-Event-Interval
view release on metacpan or search on metacpan
# NAME
Async::Event::Interval - Scheduled and one-off asynchronous events
<div>
<a href="https://github.com/stevieb9/async-event-interval/actions"><img src="https://github.com/stevieb9/async-event-interval/workflows/CI/badge.svg"/></a>
<a href='https://coveralls.io/github/stevieb9/async-event-interval?branch=master'><img src='https://coveralls.io/repos/stevieb9/async-event-interval/badge.svg?branch=master&service=github' alt='Coverage Status' /></a>
</div>
# SYNOPSIS
A simple event that updates JSON data from a website using a shared scalar
variable, while allowing the main application to continue running in the
foreground. Multiple events can be simultaneously used if desired.
See ["EXAMPLES"](#examples) for other various functionality of this module.
use warnings;
use strict;
use Async::Event::Interval;
my $event = Async::Event::Interval->new(2, \&callback);
my $json = $event->shared_scalar;
$event->start;
while (1) {
print "$$json\n";
#... do other things
$event->restart if $event->error;
}
sub callback {
$$json = ...; # Fetch JSON from website
}
# DESCRIPTION
Very basic implementation of asynchronous events triggered by a timed interval.
If a time of zero is specified, we'll run the event only once.
# METHODS - EVENT OPERATION
## new($delay, $callback, @params)
Returns a new `Async::Event::Interval` object. Does not create the event. Use
`start` for that.
Parameters:
$delay
Mandatory: The interval on which to trigger your event callback, in seconds.
Represent partial seconds as a floating point number. If zero is specified,
we'll simply run the event once and stop.
$callback
Mandatory: A reference to a subroutine that will be called every time the
interval expires.
@params
Optional, List: A list of parameters to pass to the callback. Note that these
are not shared parameters and are a copy only, so changes to them in the main
code will not be seen in the event, and vice-versa. See ["shared\_scalar"](#shared_scalar) if
you'd like to use variables that can be shared between the main application and
the events.
## start
Starts the event timer. Each time the interval is reached, the event callback
is executed.
## stop
Stops the event from being executed.
## restart
Alias for `start()`. Re-starts a `stop()`ped event.
## status
Returns the event's process ID (true) if it is running, `0` (false) if it
isn't.
## waiting
Returns true if the event is dormant and is ready for a `start()` or
`restart()` command. Returns false if the event is already running.
## error
Returns true if an event crashed unexpectedly in the background, and is ready
for a `start()` or `restart()` command. Returns false if the event is not in
an error state.
## interval($seconds)
( run in 1.946 second using v1.01-cache-2.11-cpan-39bf76dae61 )