in reply to HTTP header

You need to print the content header so that the client application knows what to do with the data it is recieving. By printing,

print "Content-type: text/plain\n\n";

You are telling the browser, "hey this just text data" and the browser knows what to do. When you are sending data before the content header the browser says "WTF?" and does not know what to do. So browsers may do what you expect but you can be sure that all browsers will not do the same thing.