Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible editor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
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 rifling through the Monastery: (6)
As of 2021-05-11 14:12 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (116 votes). Check out past polls.