944,084 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 52896
  • Perl RSS
Mar 15th, 2005
0

Perl- How to call an event after a time delay

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nuwan is offline Offline
1 posts
since Mar 2005
Apr 28th, 2005
0

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

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:

Perl Syntax (Toggle Plain Text)
  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:

Perl Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 6th, 2005
0

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

I know this is an old thread. Just thought i'd add in another way, hehe!
TMTOWTDI
Anyways
Perl Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JEt3L is offline Offline
10 posts
since Jun 2005
Jul 7th, 2005
0

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

I like this bit from the Time::HiRes manpage

Perl Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
kordaff is offline Offline
20 posts
since Jun 2005
Oct 1st, 2010
0
Re: Perl- How to call an event after a time delay
Here is another way to do it..

system("sleep 5s");

Cheers
Prasanna Rathi
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prasannarathi is offline Offline
1 posts
since Oct 2010
Oct 8th, 2010
0
Re: Perl- How to call an event after a time delay
I'm not sure that answering a question after 5 years is really effective.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Oct 8th, 2010
0
Re: Perl- How to call an event after a time delay
Click to Expand / Collapse  Quote originally posted by mitchems ...
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.
Reputation Points: 153
Solved Threads: 142
Master Poster
d5e5 is offline Offline
740 posts
since Sep 2009
Oct 16th, 2010
0
Re: Perl- How to call an event after a time delay
sleep 1 ; # one second
Reputation Points: 61
Solved Threads: 150
Master Poster
richieking is offline Offline
757 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: perl programming
Next Thread in Perl Forum Timeline: Perl scanning





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC