$SIG{ALRM} = \&timed_out; eval { alarm(10); $buf = <>; alarm(0); }; if ($@ =~ /BLAH/) { print "Timed out."; } sub timed_out { die "BLAH" }