Async-Event-Interval
view release on metacpan or search on metacpan
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)
Gets/sets the delay time (in seconds) between each execution of the event's
callback code. You can use this method to change the delay between calls
during the event's lifecycle.
Parameters:
$seconds
Optional, Integer: The number of seconds (can be floating point) to delay
between executions.
Return: Integer, the number of seconds between execution runs. If we're in
a run-once scenario, the return will be zero `0`.
## shared\_scalar
Returns a reference to a scalar variable that can be shared between the main
process and the events. This reference can be used within multiple events, and
multiple shared scalars can be created by each event.
To read from or assign to the returned scalar, you must dereference it. Eg.
`$$shared_scalar = 1;`.
# METHODS - EVENT INFORMATION
## errors
Returns the number of times a started or restarted event has crashed
unexpectedly.
## error\_message
( run in 0.687 second using v1.01-cache-2.11-cpan-39bf76dae61 )