讓我們來看看,這是怎麼回事:
my ($timeout, $size, $buffer) = (10, 10, undef);
eval {
#establish ALRM signal handler
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
#send alarm signal to program in 10 second
alarm $timeout;
#try yo read 10 bytes of data into $buffer
my $nread = sysread STDIN, $buffer, $size;
#cancel the previous timer without starting a new one
#if we returned from sysread. Yes, if 10 seconds pass
#before the next function is executed, the script will
#die even though the data was read
alarm 0;
#print number of bytes read (will be 10) and the string,
#read from input
print "$nread: $buffer";
};
$ @設置如果字符串是EVAL-ED沒編譯,或者Perl代碼評估模過程中執行的()d。在這種情況下$ @的值是編譯錯誤,或參數死:
if ([email protected]) {
warn [email protected];
}
所以,這將打印die消息「報警\ n」如果我們沒有在10從sysread執行返回第二。
在很偶然的情況下,當輸入將獲得10秒內之前,我們將無法運行報警0;,我建議使用下面的代碼:
my ($timeout, $size, $buffer) = (10, 10, undef);
#I define $nread before the signal handler as undef, so if it's defined
#it means, that sysread was executed and the value was assigned
my $nread = undef;
eval {
local $SIG{ALRM} = sub {
#if it's not defined - it means, that sysread wasn't executed
unless(defined($nread))
{
die "alarm\n";
}
};
alarm $timeout;
$nread = sysread STDIN, $buffer, $size;
alarm 0;
print "$nread: $buffer";
};
不幸,當賦值運算符沒有被執行時,它不會將我們從案例中解救出來。
鏈接:
http://perldoc.perl.org/functions/alarm.html
http://perldoc.perl.org/perlvar.html
http://perldoc.perl.org/functions/sysread.html
在「my $ nread = sysread STDIN,$ buffer,$ size;」之後和「報警0」之前。這裏!。可能是我們的警報已過並且交付了SIGALARM。 – drlexa
@drlexa現在就來看看。好,我寫了關於發生了什麼的仔細解釋。現在調查問題。 – user4035
@drlexa回答更新 – user4035