The following example shows how a time-out can be installed for a critical function call.
#!/usr/bin/perl -w
use strict;
sub timeCriticalFunction
{
eval
{
local $SIG{ ALRM} = sub { die "time-out"; };
alarm(3);
#
# in a realistic case the sleep() function is replaced by some
# critical piece of code that needs a time-out handling, like connect()
#
sleep(4);
#
# the alarm is cleared, if all went well
alarm(0);
};
if( $@)
{
print "eval returned an error: $@\n";
return 0;
}
return 1;
}
my $ret = timeCriticalFunction();
print " function returns $ret \n";