my $timeout = 20; my $result = eval { local $SIG{ALRM} = sub { die "timed out\n" }; alarm $timeout; my $result = send_command(); alarm 0; return $result; }; if ($@) { print "command timed out\n"; do_something_else(); }