Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Run subroutine occasionally

by stevieb (Canon)
on Mar 01, 2022 at 18:59 UTC ( [id://11141735]=note: print w/replies, xml ) Need Help??


in reply to Run subroutine occasionally

My Async::Event::Interval does this. It takes a callback as a parameter, and runs it at a given interval (in seconds). The callback is run in a separate process.

Example:

use warnings; use strict; use feature 'say'; use Async::Event::Interval; my $delayed_event = Async::Event::Interval->new(5, \&interval_sub); $delayed_event->start; while (1) { say time; sleep 1; } sub interval_sub { say "Running interval sub"; }

Output:

1646160956 1646160957 1646160958 1646160959 1646160960 Running interval sub 1646160961 1646160962 1646160963 1646160964 1646160965 Running interval sub 1646160966 1646160967 1646160968 1646160969 1646160970 ^C

An interval (first parameter to the new() method) of 0 (zero) will only run the routine once, but you can run it again at any time by calling $delayed_event->start if $delayed_event->waiting;. Floating point intervals are also supported if one needs such granularity.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141735]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found