in reply to code problem
#!/bin/sh -x printf "Content-type: text/plain\n\n" exec 2>&1 ./your_script
One thing that occured to me is that perhaps the path to Perl at the top is wrong.