Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Can anyone explain why this is happening?
1) Internally, Perl has two different kinds of strings. We'll call them 'binary' and 'unicode' strings.
$ perl -MDevel::Peek -e 'Dump "Я"'
This is a binary string.
$ perl -MDevel::Peek -e 'use utf8; Dump "Я"'
This is a unicode string. It has the so-called 'UTF8 flag' turned on, while binary strings don't (internally, Perl 'unicode' strings are encoded in UTF-8).

2) Perl pretends that it doesn't have two different types of strings. Whenever a binary string enters a 'Unicode context' (so to say), Perl converts binary string to Unicode, with not entirely satisfactory results. Also, it sometimes try to convert Unicode strings to binary, which also doesn't work very well

$ perl -E 'binmode STDOUT, ":encoding(UTF-8)"; say "Я"'
Binary string "Я" got mangled in Unicode context.
$ perl -E 'use utf8; my $x = "Я"; no utf8; my $y = "Я"; say $x . $y'
Wide character in say at -e line 1.
Unicode string $x was concatenated with binary string $y, and $y was 'upgraded' to Unicode. At least, we got a warning...
$ perl -wE 'use utf8; my $x = ""; no utf8; my $y = ""; say $x . $y'
Where's my warning, Perl?... And what happened with $x???

3) Perl thinks that all binary strings (those without UTF-8 flag) are encoded in Latin-1. Whenever it sees fit, it converts them to Unicode. And vice versa.

c2 bb becomes U+00C2 U+00BB. That is, "" becomes "»" (from Latin-1 to Unicode). "" becomes "�" (from Unicode to Latin-1, which cannot be displayed on my terminal).

4) To make things more interesting, Perl doesn't always turn UTF-8 flag on

$ perl -MDevel::Peek -E 'use utf8; Dump "This is America!"'

Conclusion: to use Perl, you must either be an American, or an expert in Unicode and Perl internals. Well, you seem to be an American, Jim.

In reply to Re: Database vs XML output representation of two-byte UTF-8 character by Anonymous Monk
in thread Database vs XML output representation of two-byte UTF-8 character by jkeenan1

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2021-03-01 16:20 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (13 votes). Check out past polls.