How can I tell Perl to run some code every 20 seconds?
#!/usr/bin/perl -w
use strict;
# heartbeat
$SIG{ALRM} = sub {
# set up the next signal for 20 second from now
alarm(20);
printf " \n";
};
alarm(20); # start timer
my $i = 0;
while(1) {
$i++;
printf "loop" . $i . "\n";
sleep 2;
}
alarm(0);
printf "done!\n";
Output :
loop1
loop2
loop3
loop4
loop5
loop6
loop7
loop8
loop9
loop10
loop11
loop12
loop13
loop14
loop15
loop16
loop17
loop18
loop19
loop20