Although you didn't state it explicitly, it sounds like you're building a cgi-bin script.
The "Premature end of script headers" error sounds like your web server complaining, not like perl.
The basic problem is, in CGI land, your script must print out valid HTML headers before it prints out Anything Else.
Depending on your web server, there are several techniques you can use to try to debug this.
Looking in your web server's error log can sometimes give you more information on how your script failed.
Running the script from the command line (possibly with a
$DEBUG flag, which when active, sets values that would otherwise come from a POST or GET)
can help, but unless you have hot pipes on (
$|=1), the script output might come out in a different order than it does in CGI-land.
Another sometimes useful technique is to put a BEGIN block that prints out a valid header, for debugging:
BEGIN { print "Content-type: text/plain\n\n"; }
That will be run before the rest of your code, and will usually ensure the header gets printed out before anything else.
Then you can see what the Next Thing is that's printed out; if it's not a valid header, then you know you have a problem.
And of course, if you give more details on your setup and code snippets, I could make more suggestions.