Alarm Handler

The following example demonstrates how eval is used with an alarm handler. If the time-out occurs, the eval block dies, returns 0 and control is transfered to the next statement.

...
#
$proto = getprotobyname('tcp');
$port = getservbyname('ssh', 'tcp');
for $sub ( 1 .. 255)
{
  $node = "131.169.$net.$sub";
  socket( SOCKET, PF_INET, SOCK_STREAM, $proto);
  $sin = sockaddr_in($port, inet_aton( $node));
  eval 
  {
    local $SIG{ALRM} = sub { die }; 
    alarm(1); 
    connect(SOCKET,$sin);
    alarm 0; 
  } || next; 
  $reply = <SOCKET>; 
  if( defined $reply)
  {
    ...
  }
  close( SOCKET); 
}