You need to tell the browser what type of content to expect, and this is done with the following:
print "Content-type: text/html\n\n";
Note that you have to have two \n's at the end, or else it won't work.
There can be other causes for that error, but most of the time that's the reason. Put it towards the top before you try to execute any other commands and see if that helps.
Update: Take a look
here, that will show you more about using HTML headers and footers. There are built-in function for headers and footers for html, so you don't have to print the HTML/BODY/etc tags.