Perl alarm Function



This function sets the "alarm," causing the current process to receive a SIGALRM signal in EXPR seconds. If EXPR is omitted, the value of $_ is used instead.

The actual time delay is not precise, since different systems implement the alarm functionality differently. The actual time may be up to a second more or less than the requested value. You can only set one alarm timer at any one time. If a timer is already running and you make a new call to the alarm function, the alarm timer is reset to the new value. A running timer can be reset without setting a new timer by specifying a value of 0.


Following is the simple syntax for this function −

alarm EXPR


Return Value

This function returns Integer value ie. number of seconds remaining for previous timer.


Following is the example code showing its basic usage −


eval {
   local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
   alarm $timeout;
   $nread = sysread SOCKET, $buffer, $size;
   alarm 0;
if ($@) {
   die unless $@ eq "alarm\n";   # propagate unexpected errors
      # timed out
} else {
   # didn't