Hi folks. My reading of the docs for decode function in the Encode module suggests that the utf8 flag on the resulting string should be off if the input string was plain ASCII. i.e. I'd expect the following:
#!/usr/bin/perl use warnings; use strict; use Encode;; my $octets = "abcd"; my $ustr = Encode::decode('utf8', $octets); print Encode::is_utf8($ustr) ? "is" : "isn't", " tagged as a unicode string\n";
to print "isn't tagged as a unicode string".

But I don't, I get "is tagged as a unicode string" as the output.

What I believe to be the relevant section of the docs is:

Here is how Encode takes care of the utf8 flag. * When you encode, the resulting utf8 flag is always off. * When you decode, the resulting utf8 flag is on unless you can unambiguously represent data. Here is the definition of dis-ambiguity. After $utf8 = decode('foo', $octet);, When $octet is... The utf8 flag in $utf8 is --------------------------------------------- In ASCII only (or EBCDIC only) OFF In ISO-8859-1 ON In any other Encoding ON ---------------------------------------------
I'm sure I've seen this not-tagging-ASCII behaviour in older versions of Encode (I think 2.01), too.

I'm running perl 5.8.8, Encode 2.12.

Can anyone else confirm that this is a bug, or have I got my encodings and flags in a twist.


In reply to Behaviour of Encode::decode_utf8 on ASCII by jbert

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.