perl-5.38.0
alarm SECONDS
alarm

Arranges to have a SIGALRM delivered to this process after the specified number of wallclock seconds has elapsed. If SECONDS is not specified, the value stored in $_ is used. (On some machines, unfortunately, the elapsed time may be up to one second less or more than you specified because of how seconds are counted, and process scheduling may delay the delivery of the signal even further.)

指定した壁時計秒数が経過した後に、自プロセスに SIGALRM が 送られてくるようにします。 SECONDS が指定されていない場合は、$_ に格納されている値を 使います。 (マシンによっては、秒の数え方が異なるため、指定した秒数よりも最大で 1 秒ずれます。)

Only one timer may be counting at once. Each call disables the previous timer, and an argument of 0 may be supplied to cancel the previous timer without starting a new one. The returned value is the amount of time remaining on the previous timer.

一度には一つのタイマだけが設定可能です。 呼び出しを行なう度に、以前のタイマを無効にしますし、 新しくタイマを起動しないで以前のタイマをキャンセルするために 引数に 0 を指定して呼び出すことができます。 以前のタイマの残り時間が、返り値となります。

For delays of finer granularity than one second, the Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) provides ualarm. You may also use Perl's four-argument version of select leaving the first three arguments undefined, or you might be able to use the syscall interface to access setitimer(2) if your system supports it. See perlfaq8 for details.

1 秒より精度の高いスリープを行なうには、Time::HiRes モジュール(CPAN から、 また Perl 5.8 からは標準配布されています) が usleep を提供します。 Perl の 4 引数版 select を最初の 3 引数を未定義にして使うか、setitimer(2) をサポートしているシステムでは、 Perl の syscall インタフェースを使って アクセスすることもできます。 詳しくは perlfaq8 を参照してください。

It is usually a mistake to intermix alarm and sleep calls, because sleep may be internally implemented on your system with alarm.

alarmsleep を混ぜて使うのは 普通は間違いです; なぜなら、sleep は内部的に alarm を使って内部的に実装されているかも しれないからです。

If you want to use alarm to time out a system call you need to use an eval/die pair. You can't rely on the alarm causing the system call to fail with $! set to EINTR because Perl sets up signal handlers to restart system calls on some systems. Using eval/die always works, modulo the caveats given in "Signals" in perlipc.

alarm をシステムコールの時間切れのために使いたいなら、 eval/die のペアで使う必要があります。 システムコールが失敗したときに $!EINTR が セットされることに頼ってはいけません; なぜならシステムによっては Perl は システムコールを再開するためにシグナルハンドラを設定するからです。 eval/die は常にうまく動きます; 注意点については "Signals" in perlipc を参照してください。

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

For more information see perlipc.

さらなる情報については perlipc を参照してください。

Portability issues: "alarm" in perlport.

移植性の問題: "alarm" in perlport