arrrrgv has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I wrote an script where I wait for events as soon as I'm waiting for messages through the network, any way I do so like this:

#!/usr/bin/perl use Event; sub whatever{ #here I do my staff waiting for messages through the spread toolk +it #Here I could check what time is if N time has passed i could do + anything, but i have no way to check it if not any event has hapenne +d. } #here I launch it on event Event->io(fd => $spread->fd, cb => \&put_output); Event::loop();

My main problem is that once I'm inside the sub I can control the time has passed as soon as messages (events are comming, but if i don't received anything I have no way to control if N time has passed.

What I can do to add control time the Event if nothing happens?

Replies are listed 'Best First'.
Re: on event but after an interval?
by moritz (Cardinal) on Aug 22, 2011 at 11:18 UTC

    I don't quite understand your question. Do you want to start a timer after you have received an event? Or do you want to prevent event execution before certain other events have happened?

    If it's the latter, you can just use an outer variable to check:

    my $seen_event_one; sub event_one { $seen_event_one++; # rest of event handler here } sub event_two { return unless $seen_event_one; # rest of event handler here }
Re: on event but after an interval?
by GrandFather (Saint) on Aug 22, 2011 at 12:10 UTC

    One technique you could use is to set an alarm. Consider:

    use strict; use warnings; waitForSomeTime (); sub waitForSomeTime { my $bail; local $SIG{ALRM} = sub {$bail = 1}; alarm 5; # Bail in 5 seconds if we don't stop beforehand while (! $bail) { # Do stuff in here that might terminate the loop # but don't use sleep() because alarm may get grumpy } if ($bail) { print "Must have bailed\n"; } else { alarm 0; # Reset the alarm print "Loop terminated some other how\n"; } }
    True laziness is hard work
Re: on event but after an interval?
by locked_user sundialsvc4 (Abbot) on Aug 22, 2011 at 12:26 UTC

    Let’s say that I want something to happen “100 milliseconds after” something else has occurred.   What I would do is simply to arrange for a timer to go off at one-half that interval.   (Thus controlling latency.)   When an event occurs, it sets a flag.   The timer handler checks to see if the flag has been set, and if so, clears the flag and does something interesting.   If that “something interesting” needs to wait until several successive quantums have passed, I will either use a counter, or, more likely, I’ll capture a snapshot of the timer value to use as a “deadline” value.   (“If the alarm goes off at 6:30, I have to be awake and on-the-road by (the next deadline is...) 7:00.   Is it 6:55 yet?   No?   Snooozzzzzeee...”   ;-)  )   Maybe, after doing something, the timer routine calculates the next deadline.

    Being, properly, “lazy,” I won’t get any fancier than this.   I’ll just let that timer run forever, even when there is nothing to do.