in reply to Perl/Tk: utf8 in Text widget?

Here's a tiny complete app which demonstrates the problem:
#!/usr/bin/perl -w use Tk; use strict; my ($mw, $test_string, $txt); $mw = MainWindow->new(-title => "UTF-8 Test", -width=>400, -height=>30 +0); $test_string = 'This is a sample Chinese character: 我'; $txt = $mw->Text(-height=>5)->pack; $txt->insert('end', $test_string); MainLoop;

The entity in the code above was inserted by the PerlMonks web site. In my original source code I have a Chinese character. If anyone wants to try out this issue, you'll have to somehow get a Unicode character into the string.
--- Marais

Update: Oh boy, life is complicated. I now find that this code, designed to demonstrate the problem, actually works IF you use this:
$test_string = 'This is a sample Chinese character: ' . chr(25105);
and it also works with the original Chinese character if I include
use utf8;

So clearly I have to give this some more thought. It's looking as if a string retrieved from the database must be different in some important way from a constant string. Gack.

Replies are listed 'Best First'.
Re^2: Perl/Tk: utf8 in Text widget?
by graff (Chancellor) on Sep 25, 2009 at 01:39 UTC
    Here's another way for that test snippet to work (knowing that 25105. == 0x6211):
    #!/usr/bin/perl -w use Tk; use strict; my ($mw, $test_string, $txt); $mw = MainWindow->new(-title => "UTF-8 Test", -width=>400, -height=>30 +0); $test_string = "This is a sample Chinese character: \x{6211}"; $txt = $mw->Text(-height=>5)->pack; $txt->insert('end', $test_string); MainLoop;
    Works for me, no problem -- I see a Chinese character. In fact, I distinctly remember that whenever I've installed Tk for perl 5.8, the "make test" phase spends a fair bit of time going through all the unicode characters (including the Chinese, Japanese and Korean).

    I don't think Tk can do Arabic (or any other right-to-left writing system), and I don't know how well it can do with Devanagari or similar Indic scripts, but all the left-to-right characters work fine.

    So if this little test snippet isn't working for you, there might be a problem with your particular installation of the Tk bundle (or maybe it's a font issue?)

    If this test snippet works, but you're still having trouble getting stuff from mysql to display correctly (even though you seem to be decoding it as you should, or have "utf8_enabled" in the DBI connection), then you may want to check other modes of access to the DB, to see if the actual content in your tables is not what you expect.