jyankel has asked for the wisdom of the Perl Monks concerning the following question:

There are other posts similar to this, but I've not been able to find a solution. I'm trying to receive a utf8 encoded string from a perl program to a C# application, but I always get the ANSI string. The perl's print command does show the correctly encoded output, but when using the system call the output is ANSI. This works in linux, but not Windows.

If I run Example.exe "Ħis. Ŧ good" it works correctly displaying the utf8 string.

However, running the perl program does not work. Please advise. Here are sample programs. To see the correct utf8 encoding in the Windows console you have to first change the active code page with the chcp 65001 command.
#!/usr/bin/perl -w use strict; use utf8; binmode(STDOUT,':encoding(UTF-8)'); my $string= "\x{126}is. \x{166}good"; #my $string = pack('U', 294) . "is. " . pack('U', 358) . " good"; print "Sent = $string\n"; system ("Example.exe \"$string\"");
C# app.
using System; using System.Text; namespace Example { class Example { static void Main(string[] args) { Console.WriteLine("Received " + args[0]); System.Environment.Exit(0); } } }

Replies are listed 'Best First'.
Re: Unicode (utf8) does not pass correctly to Windos system call.
by ikegami (Patriarch) on Nov 16, 2009 at 15:33 UTC
    You never encode the string.
    utf8::encode( my $enc_string = $string ); system("Example.exe \"$enc_string\"");
      It is encoded. The print command does display correctly. I did try your code snippet, but it still does not work.
        It is encoded. The print command does display correctly.

        It's not encoded. The print goes to STDOUT, to which you have applied an encoding IO layer.

        Anyway, if you have the option to change the C# program, try communicating by a pipe instead of passing it as a command line argument. That should work more platform independent.

        Perl 6 - links to (nearly) everything that is Perl 6.

        It is encoded. The print command does display correctly

        That's because you do encode what you send to STDOUT (via binmode).

        I did try your code snippet, but it still does not work.

        It does what you asked. Maybe you asked the wrong question?

Re: Unicode (utf8) does not pass correctly to Windos system call.
by jyankel (Initiate) on Nov 17, 2009 at 13:16 UTC
    I realized I posted an error in the perl code above. I corrected that (Just had some inconsistent string names when I simplified the example.) I will try and clarify my question.

    The output of the perl print statement is what I expect. The print command is correctly displayed in the uft8 encoding, however the encoded string is not encoded when it is passed through the system command to the C# application on windows. I'll try and post what I'm seeing.

    Output of perl program.
    >perl test.pl
    >Sent Ħis. Ŧ good
    >Received Ħis. Ŧ good

    Output of C# application when run by itself with the same encoded string argument that the perl program should pass to it.
    >Example.exe Ħis. Ŧ good
    >Received Ħis. Ŧ good


    By itself the C# program does display a utf8 encoded string if passed to it from the windows command prompt. On a linux system the perl script does work and the C# application receives the utf8 encoded string from the perl system() call.

    If I run the perl script on linux I get this output which is what I want.
    >perl test.pl
    >Sent Ħis. Ŧ good
    >Received Ħis. Ŧ good