OK, I'm in UTF-8 hell here. I thought that I would at least verify that I could read and write UTF-8 strings to my database, but while working on that, I discovered that I can't even get a trivial Perl program to run understandably.

Here's my test program:

#!/usr/bin/perl -w use strict; use utf8; my ( $codepoint, $test_string, ); binmode STDOUT, ":utf8"; $codepoint = ord('我'); print "Codepoint of character is $codepoint\n"; $test_string = "Here's a test string with 我\n"; print "test_string is $test_string\n"; $test_string_dec = decode('utf8', $test_string);
(In my original, I had the literal Chinese character 我 where you see the big numeric constant.)

There are at least two issues here:

  1. The presence of the "use utf8" pragma: I gather from Googling that this is no longer required in current versions of Perl (I'm using 5.8.8.) But, if I leave it out, the codepoint reported by "ord" is 250, instead of 25105. Surely Perl should know that the Chinese character is Unicode?
  2. The "binmode" statement: This interacts with the "use utf8" pragma in the following ways:
    both present: correct codepoint, correct output character, no error message
    pragma present, binmode omitted: correct codepoint, correct output character, "Wide character in print" error message.
    pragma omitted, binmode present: wrong codepoint, wrong output character, no error message
    Both omitted: wrong codepoint, output shows correct Chinese char, no error message.

So, I'm really confused. What does the "use utf8" pragma actually do in Perl 5.8.8? Why do I get the correct character showing on output even when I get the "Wide character in output..." message?

--- Marais


In reply to Re: Perl/Tk: utf8 in Text widget? by Marais
in thread Perl/Tk: utf8 in Text widget? by Marais

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.