eval { local $SIG{ALRM} = sub { die('ALARM'); }; alarm($REGEX_TIMEOUT); @return = ($raw =~ /$regex/msgx); alarm(0); } if ($@ =~ /ALARM/) { ... }