I have a few suggestions:
-
You should take your fork/exec logic out of that infinite loop.
If you are ending up with "heaps of processes" the loop is the
main reason why.
-
I think blindly exec-ing something from STDIN is a very bad idea. Maybe
you're the only one who'll be using that program, but if I
were you I'd want to examine $cmd before
running it. This might be a place where taint mode would
be useful.
-
Don't leave a child process hanging about if the exec failed:
exec $cmd or die;