Perl- How to call an event after a time delay

Reply

Join Date: Mar 2005
Posts: 1
Reputation: nuwan is an unknown quantity at this point 
Solved Threads: 0
nuwan nuwan is offline Offline
Newbie Poster

Perl- How to call an event after a time delay

 
0
  #1
Mar 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Perl- How to call an event after a time delay

 
0
  #2
Apr 28th, 2005
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:

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

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:

  1. $oldtime = (time + 10);
  2. while (1) {
  3. if (time > $oldtime) {
  4. &sub_to_call;
  5. $oldtime = (time + 10);
  6. }
  7. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 10
Reputation: JEt3L is an unknown quantity at this point 
Solved Threads: 0
JEt3L JEt3L is offline Offline
Newbie Poster

Re: Perl- How to call an event after a time delay

 
0
  #3
Jul 6th, 2005
I know this is an old thread. Just thought i'd add in another way, hehe!
TMTOWTDI
Anyways
  1. #!/usr/local/bin/perl
  2. use strict;
  3. my $pid;
  4. die "cant fork $!\n" unless defined($pid=fork());
  5. if($pid) {
  6. print "I'll do what i want here\n";
  7. my $in=<STDIN>;
  8. while ($in ne 'stop') {
  9. $in=<STDIN>;
  10. chomp $in;
  11. print "$in is your input\n";
  12. if($in eq 'stop') {
  13. print "exiting.........\n";
  14. }
  15. }
  16. }
  17. else {
  18. LOOP1:
  19. sleep 10;
  20. print "im printing this cause my timer just went off\n";
  21. goto LOOP1;
  22.  
  23. }
  24. print "you can say bye bye now or \n";
  25. print "do something else here before you go\n";
  26. kill ("TERM",$pid); # not that graceful of an exit though :D
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 16
Reputation: kordaff is an unknown quantity at this point 
Solved Threads: 0
kordaff kordaff is offline Offline
Newbie Poster

Re: Perl- How to call an event after a time delay

 
0
  #4
Jul 7th, 2005
I like this bit from the Time::HiRes manpage

  1. # Arm an interval timer to go off first at 10 seconds and
  2. # after that every 2.5 seconds, in process virtual time
  3.  
  4. use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
  5.  
  6. $SIG{VTALRM} = sub { print time, "\n" };
  7. 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC