Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

A simple hex dump of bytes is not always sufficient.

Perl scalars can store strings in two ways: legacy, using characters that match bytes, and Unicode, using characters that can have values larger than 255. In the first case, the unicode flag (unfortunately called "the UTF8 flag") is cleared, in the second case, it is set. You can query it using the core function utf8::is_utf8($scalar). More details: utf8, perlunifaq

Things get interesting when encoding and decoding happens, as the outside world usually talks bytes, not characters. If you handle all data as binary, "Unicode in, Unicode out" seems to work magically, as long as you don't touch the bytes. Perl does not see Unicode, but a string of bytes. The difference is subtile, but can cause huge problems. length('Bär') is suddenly not 3, but 4 (UTF-8), 6 (UTF-16) or 12 (UTF-32). You can't match the 'ä' in 'Bär'. And so on. See Match full utf-8 characters for a real-world example.

The correct way is, of course, to handle encodings correctly. That way, perl decodes input to strings of characters, and encodes those strings of characters back on the way to output. length('Bär') will always be 3, you can match the 'ä' in 'Bär', and so on.

To see if all encoding and decoding happened as expected, you have to look at the unicode flags of the scalars. Comparing actual length and expected length is also useful. Especially when working not only with STDIN and STDOUT, but e.g. also with a database interface, you really want to know if you have successfully read and written a string as Unicode or just happened to get the same bytes back as you wrote, and the other side saw just Mojibake. That's the main reasons why t/40UnicodeRoundTrip.t, t/41Unicode.t, and t/ in DBD::ODBC exist. Let me tell from experience gained while developing the initial Unicode patch for DBD::ODBC: It is really easy to mess up the encoding and decoding.

Oh, and just a little extra: CGI does NOT change STDOUT to use UTF-8 encoding when using the -utf8 pragma. It also does NOT change STDOUT when you send a document encoded as UTF-8. You have to do so manually (binmode STDOUT,':encoding(utf-8)'). If you find "wide character in print" warnings in the web server's error log, you have your string encodings right, but STDOUT was not set to UTF-8. If you don't see those warnings, with warnings enabled and no binmode STDOUT, your strings most likely aren't encoded properly.


Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^6: Text::CSV encoding parse() by afoken
in thread Text::CSV encoding parse() by slugger415

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 drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2020-02-18 04:07 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (74 votes). Check out past polls.