On Jun 22, 2006, at 2:29 PM, Tommy Nordgren wrote:

22 jun 2006 kl. 20.15 skrev Sherm Pendley:

On Jun 22, 2006, at 1:48 PM, Tommy Nordgren wrote:

How do I write proper utf 8 characters to a file? I write only two characters, and they come out as four
garbage characters when I view the file in an editor.

Quick answer:

        open FH, ">:utf8", "file";

Complete answer:

        perldoc perluniintro
        perldoc PerlIO

I've already tried that. That was what i was doing when I got garbage.

Well, the above is correct as far as Perl goes - but it doesn't rule out other problems. Are you certain that the editor you're using is interpreting the file correctly, as UTF8? Also, are you certain that your input really is UTF8?

For instance, I ran this script to generate a test file:

    #!/usr/bin/perl

    use strict;
    use warnings;
    use utf8; # This allows utf8 in string literals, like below

    open FH, '>:utf8', '/Users/sherm/hello.txt' or die $!;
    print FH "Hëllö, wörld!\n";
    close FH;

When I open the file in BBEdit, I see gibberish, because BBEdit can't determine that it's UTF8 (there's no BOM), and misinterprets it as the default Mac OS Roman instead. But, if I change BBEdit's default encoding, or use the "Reopen Using Encoding" function, BBEdit displays the file correctly.

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Reply via email to