1,105,332 Community Members

Perl- How to call an event after a time delay

Member Avatar
nuwan
Newbie Poster
1 post since Mar 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear All

I`m very new to Perl programming and using Perl 5.8.0 on Linux environment. Can somebody post a reply explaining how to call an event(ex-a simple subroutine) from perl after a certain time delay and repeat again if needed.

I really appreciate if somebody can post a sample code explaining this and look forward to hear from somebody soon.

Thanks in advance,

Nuwan

Member Avatar
Comatose
Taboo Programmer
2,536 posts since Dec 2004
Reputation Points: 290 [?]
Q&As Helped to Solve: 227 [?]
Skill Endorsements: 34 [?]
Team Colleague
 
0
 

you need to be careful when talking about events in perl. Events are a term coined for OOP program, which perl is not really made for. It's possible, sure, with things such as POE and the Tk Modules, but namely for standard perl programming, it's pretty much not event driven. My suggestion here, is to do an infinate loop, something really easy like:

while (1) {
     # /* Stuff To Do In Loop Here */
}

Then grab a time stamp. You can use the epoch, I believe, with time. So you can take a time stamp, set a variable to contain the old time, plus however many seconds you want the "timer" to be, and then test it against the new time, so something like:

$oldtime = (time + 10);
while (1) {
     if (time > $oldtime) {
         &sub_to_call;
         $oldtime = (time + 10);         
     }
}

The above code will take a timestamp, and add 10 seconds to it. Then it will go into an infinate loop, and keep checking if the current time is greater than the time we specified. If it is, so either 10 or 11 seconds have passed, we call a sub called "sub_to_call", and then we reset our oldtime variable to now, plus 10 more seconds. Basically, every 10 seconds this program should call the sub.

Member Avatar
JEt3L
Newbie Poster
10 posts since Jun 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I know this is an old thread. Just thought i'd add in another way, hehe!
TMTOWTDI
Anyways

#!/usr/local/bin/perl
use strict;
my $pid;
die "cant fork $!\n" unless defined($pid=fork());
 if($pid) {
 print "I'll do what i want here\n";
 my $in=<STDIN>;
   while ($in ne 'stop') {
   $in=<STDIN>;
   chomp $in;
   print "$in is your input\n";
       if($in eq 'stop') {
       print "exiting.........\n";
       }
   }
 }
 else {
LOOP1:
  sleep 10;
  print "im printing this cause my timer just went off\n";
goto LOOP1;
  
 }
print "you can say bye bye now or \n";
print "do something else here before you go\n";
kill ("TERM",$pid); # not that graceful of an exit though :D
Member Avatar
kordaff
Newbie Poster
23 posts since Jun 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I like this bit from the Time::HiRes manpage

# Arm an interval timer to go off first at 10 seconds and
# after that every 2.5 seconds, in process virtual time
                                                                                
use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
                                                                                
$SIG{VTALRM} = sub { print time, "\n" };
setitimer(ITIMER_VIRTUAL, 10, 2.5);

I've used that in a chat script to run the main processing loop on my listening socket and the connected user sockets 10x per second, except my is $SIG{ALRM}, using ITIMER_REAL instead of the other.

Kordaff

Member Avatar
prasannarathi
Newbie Poster
1 post since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here is another way to do it..

system("sleep 5s");

Cheers
Prasanna Rathi

Member Avatar
mitchems
Posting Whiz in Training
294 posts since Feb 2009
Reputation Points: 12 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm not sure that answering a question after 5 years is really effective.

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

I'm not sure that answering a question after 5 years is really effective.

system("sleep 157784630s");#Wait 5 years Note: I haven't had time to test this.:)

Member Avatar
richieking
Posting Shark
926 posts since Jun 2009
Reputation Points: 40 [?]
Q&As Helped to Solve: 172 [?]
Skill Endorsements: 3 [?]
 
0
 

sleep 1 ; # one second

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article