in reply to Perl TK character disappearing

Hi, another thing to try, if you are reading in files with extended characters, is to use the Encode module to decode the input as utf8.
use Encode; my $buf; open (my $fh, "< slurp1"); read( $fh, $buf, -s FH ); close $fh; my $file = decode('utf8', $buf); # now just insert $file into the Tk::Text widget

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Perl TK character disappearing
by cr8josh (Sexton) on Jun 27, 2012 at 22:29 UTC

    Thanks so much for the suggestion. I tried that, with my test code, and now it says "This is a test of the ? character", with the ? in a black diamond...!

    If I add "use UTF-8" at the top, I get an error when it hits that character saying "Malformed UTF-8 character (unexpected continuation byte 0x9c, with no preceding start byte) at..."

    Finally, if I paste in the character from the Perlmonks page to my editor (komodo), it doesn't fix it for me...

    Would love to hear any other thoughts!

    use Encode; use Tk; use Tk::TextUndo; my $main = MainWindow->new; my $string = decode ('utf8', "This is a test of the œ character"); my $middle = $main->Frame(-borderwidth=>2,-relief=>'groove')->grid(-ro +w=>0,-column=>1,-sticky=>'nsew'); my $middlebox = $middle->Text ( -font => 'system 10', -wrap => 'word', -spacing2 => 10, -spacing3 => 30, -takefocus => 1, -background => 'white', -width => 70, -height => 10, )->grid(-row=>0, -column=>0, -sticky=>'nsew +');#pack(-expand => 1, -fill => 'both');#; $middlebox->insert('end',"$string"); MainLoop;
      You say to perl your script is utf-8, but it is not. It contains 0x9c, which is cp1252 for "oe". So, use
      use encoding 'cp1252';
      instead, or save your file in utf-8.
        Thanks! That worked!