Can anyone explain what happens when Encode::encode_utf8 does when applied to a string with the UTF-8 flag switched on ?

AFAICS it merely removes the UTF-8 flag, as the program below seems to demonstrate.

The Encode documentation says that encode(ENCODING, ..) "Encodes a string from Perl's internal form into ENCODING and returns a sequence of octets"; Now, my understanding is that Perl's internal encoding *is* UTF-8, so that when applied to a string with the UTF-8 flag on, Encode::encode_utf8 is essentially a no-op, and merely switches off the UTF-8 flag.

Am I confused ?

Steve Collyer

################# code follows #######################

#!/usr/bin/perl use strict; use warnings; use Encode; use charnames qw(greek); binmode(STDOUT, ":utf8"); my $utf8_data = "<\N{alpha}\N{beta}\N{gamma}\N{delta}>"; print $utf8_data, "\n\n"; my $enc_utf8_data = Encode::encode_utf8($utf8_data); print Encode::is_utf8($enc_utf8_data) ? "\$enc_utf8_data marked as UTF-8\n\n" : "\$enc_utf8_data not marked as UTF-8\n\n"; print Encode::is_utf8($utf8_data) ? "\$utf8_data marked as UTF-8\n\n" : "\$utf8_data not marked as UTF-8\n\n"; if ($utf8_data eq $enc_utf8_data) { print "strings differ\n"; print "utf8_data ", unpack("H*", $utf8_data), "\n"; print "enc_utf8_data ", unpack("H*", $enc_utf8_data), "\n"; } else { print "strings are the same\n"; print "utf8_data ", unpack("H*", $utf8_data), "\n"; print "enc_utf8_data ", unpack("H*", $enc_utf8_data), "\n"; }

In reply to What does Encode::encode_utf8 do to UTF-8 data ? by scollyer

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.