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

Recommended Answers

All 7 Replies

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.

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

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

Here is another way to do it..

system("sleep 5s");

Cheers
Prasanna Rathi

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

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.:)

sleep 1 ; # one second

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.