Well, there actually isn't any "good" solution to your problem. Perl is Turing-complete, and as such, you
prove that any perl code will run forever or end someday. Your best bet would be to run the code in some sort of virtual environment (chroot or whatever) with a time limit. Another option would be to create a language interpreter that would dramatically reduce the capabilities of the input code by allowing a very limited subset of Perl. The best would be combination of the two : write a "similiPerl" interpreter in Javascript and run it in the client browser, to avoid any unwanted effect on the server :)