http://www.perlmonks.org?node_id=655817


in reply to Cookie question

If, for some reason, you absolutely have to send part of your HTML output before setting the cookie, you can put some javascript later in the page that sets a cookie on the fly.

This may come with its own set of problems, though, so rearranging things to put all of your output at the end probably is a better solution.