in reply to Browser choking on XML-header

But as soon as I run it from any browser, I get a 500 server error

That's because you need to do   $cgi->header(); before you start emitting XML. (And you need to arrange to emit a correct MIME type for XML. By default, header() emits one for HTML. I'll leave that as an exercise.)