It's a problem with the output of the script; which is more than likely quite easy to fix. You print your headers using a call to CGI.pm (see the print header; line?), followed by a blank line, and then the page data (<html> etc.).
You get the "premature end of script headers error" if you didn't output the blank line. That means your problem may be something to do with your print header; line.