in reply to Perl UTF-8 serving HTML5

Also the Content-Length $byte_count never comes out correct.

You're taking the length of a string (unicode string, decoded), not of bytes/octets (utf encoded)

Try

binmode STDOUT, ':raw'; $rendered = Encode::encode(utf8 =>$rendered); $byte_count = length $rendered; print <<HTML; Content-Length: $byte_count Content-Type: text/plain; charset=utf-8 HTML exit 0;

Replies are listed 'Best First'.
Re^2: Perl UTF-8 serving HTML5
by Anonymous Monk on May 29, 2016 at 19:45 UTC

    The binmode STDOUT, ':raw' is because you have already encoded your data. If you put STDOUT in UTF-8 mode, it will end up double-encoded.

    If you are on a Windows system, the byte count discrepancy might be because Perl represents line breaks internally as line feed characters. When it does output it converts the line breaks to native format. If run on Windows, this adds one character per line. Putting the handle into ':raw' mode prevents this.

      If you are on a Windows system, the byte count discrepancy might be because Perl represents line breaks internally as line feed characters.

      That was it. I just count the line breaks on windows and added it to the overall count and it works.
      Thanks.

        Or you could turn it off the :crlf layer  binmode STDOUT, ':raw:encoding(UTF-8)';