fork_call every 'n' seconds in AnyEvent I need to run fork workers every 5 seconds. Is there a way to do it more ideomatic? I'm embarrassed about $running variable.

use 5.010;
use strict;
use warnings;

use AnyEvent;
use AnyEvent::Util;

my $running;

sub work1 {
    say "work1";
    sleep 1;
}

sub work2 {
    say "work2";
    sleep 1;
}

sub do_work {

    my $worker_cv = AnyEvent-&gt;condvar;

    $worker_cv-&gt;begin;
    fork_call {
        work1();
    } sub {
        $worker_cv-&gt;end;
    };

    $worker_cv-&gt;begin;
    fork_call {
        work2();
    } sub {
        $worker_cv-&gt;end;
    };

    $worker_cv-&gt;cb(sub {
        $running = 0;
    });
}

my $timer = AnyEvent-&gt;timer(
    interval =&gt; 5,
    cb =&gt; sub {
        unless ($running) {
            $running = 1;
            do_work();
        }
    }
);

my $loop = AnyEvent-&gt;condvar;

$loop-&gt;recv;
Ok, here is a smaller scope for $running and do_work() doesn't need to bother with it,

sub do_work {

    my $worker_cv = AnyEvent-&gt;condvar;

    $worker_cv-&gt;begin;
    fork_call {
        work1();
    } sub {
        $worker_cv-&gt;end;
    };

    $worker_cv-&gt;begin;
    fork_call {
        work2();
    } sub {
        $worker_cv-&gt;end;
    };

    return $worker_cv;
}


my $running;
my $timer = AnyEvent-&gt;timer(
    interval =&gt; 5,
    cb =&gt; sub {      
        return if $running;

        $running = 1;
        do_work()-&gt;cb(sub{
            $running = 0;
        });
    }
);