my $timeout = 20; eval { local $SIG{ALRM} = sub { die "timedout" }; alarm $timeout; #Call your subroutine here.... alarm 0; }; if ($@) { # $@ will contain error message, if any }