in reply to the 500 error

in addition, you've forgotten to set the content-type.

print <<HTML; Content-type: text/html <html> ...
I must be going blind. please ignore.