954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Perl- How to call an event after a time delay

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

nuwan
Newbie Poster
1 post since Mar 2005
Reputation Points: 10
Solved Threads: 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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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
JEt3L
Newbie Poster
10 posts since Jun 2005
Reputation Points: 10
Solved Threads: 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

kordaff
Newbie Poster
20 posts since Jun 2005
Reputation Points: 11
Solved Threads: 0
 

Here is another way to do it..

system("sleep 5s");

Cheers
Prasanna Rathi

prasannarathi
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 
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.:)

d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

sleep 1 ; # one second

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You