I have a string that I know is utf8 but perl's utf8 flag is not set on the string and I have not run decode('UTF-8' => $string).

If I print this string to a filehandle with :utf8 layer set (via binmode or at open), will the utf8 be garbled somehow, since the string is not marked/known utf8? Or will everything be OK, since the utf8 string is in byte mode anyway?

I have read perlunicode, perluniintro, utf8, encoding, Encode and other docs and this is one thing I cannot figure out.

This is important because LWP does not seem to set the utf8 flag for UTF-8 encoded HTML strings, even when UTF-8 is sent in the appropriate HTTP header. And I am doing some scraping on these pages and printing some of the text back out on a filehandle where I set the layer to ':utf8' and I'm hoping this will Just Work.

If not, I need to add some code that takes HTML::Encoding to the HTTP response and even the HTML itself to sniff out the encoding and, if present, call Encode::decode() on it.

An example of LWP not setting the utf8 flag on UTF-8 encoded HTML:

use strict; use warnings; use LWP::Simple; use Encode; #I have confirmed the correct HTTP header sent here my $html = get('http://-redacted-/file.utf8.html'); print "utf8: " . utf8::is_utf8($html) . "\n"; #Output is utf8: my $html2 = decode('UTF-8' => $html); print "utf8: " . utf8::is_utf8($html2) . "\n"; #Output is utf8: 1

In reply to Printing undecoded utf8 -- safe? by ryantate

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.