You can run the regex in another process...
#!/usr/bin/perl
eval {
local $SIG{ALRM} = sub { die('ALARM'); };
alarm(5);
#use forking open to start another processes
unless ($pid = open REGEX, "-|")
{
print "starting long regex match...\n";
#This string will take close to forever to match
$_ = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+aaaa";
print "matched\n" if
/a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a
+*a*[b]/;
exit;
}
while (<REGEX>) #read results from long running process
{
print "$_";
}
alarm(0);
};
if ($@ =~ /ALARM/)
{
print "got alarm\n";
kill 9, $pid;
}
else
{
print "no alarm\n"
}
-- All code is 100% tested and functional unless otherwise noted.