# perldoc > alarm

---
type: CommandReference
command: alarm
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference

- `alarm(SECONDS)` — set a timer to deliver SIGALRM after SECONDS seconds
- `alarm` — same, using `$_` as SECONDS
- `alarm(0)` — cancel the previous timer
- `eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; ...; alarm 0; }` — typical pattern for timing out a system call

## Name

`alarm` — Arranges to have a SIGALRM delivered after a specified number of wallclock seconds.

## Synopsis

`alarm SECONDS`  
`alarm` (uses `$_`)

## Options

- `SECONDS` — number of seconds. If omitted, uses `$_`. Only one timer at a time; each call disables the previous timer. Argument of `0` cancels without starting a new timer. Returns time remaining on previous timer. Process scheduling may delay signal delivery. It is usually a mistake to intermix `alarm` and `sleep` calls, because `sleep` may be internally implemented with `alarm`.

## Examples

perl
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
}
## See Also

- [Time::HiRes](http://localhost/phpMan.php/perldoc/Time%3A%3AHiRes/markdown) — provides `ualarm` for finer granularity
- Perl's four-argument `select` with first three undefined
- `syscall` interface to `setitimer(2)` (see [man setitimer(2)](http://localhost/phpMan.php/man/setitimer/2/markdown))
- perlfaq8
- perlipc (Signals section)
- perlport (portability issues)