An example
This example is from perldoc
- Reference:
$perldoc -f alarm
# eval returns the value of last expression evaluated
# if no timeout, return value will be 'alarm 0;', which return 0
# otherwise, the last statement executed is 'sub { die "alarm\n" };'
# since die within eval will stuff error message into $@, the $@ will contain "alarm"
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
# after $timeout seconds, it will issue SIGALRM signal to
# this process unless it is cancelled by a new alarm (like
# 'alarm 0;' below)
alarm $timeout;
$nread = sysread SOCKET, $buffer, $size;
alarm 0;
};
# if $@ is 0, no timeout happen, otherwise, $@ contains error message "alarm"
if ($@) {
# propagate unexpected errors (if it is not timeout, die)
die unless $@ eq "alarm\n";
# timed out
}
else {
# didn’t
}
No comments :
Post a Comment