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

I am trying to write a string to a unicode file but not all the characters are written to the file my code is
$file="Test1.enc"; open my $FH, ">:raw:encoding(UTF16-LE):crlf:utf8", "$file"; print $FH "\x{FEFF}"; print $FH "„qy„{z„ˇ{„y„Ł„~"; print $FH "$enc1"; close $FH;
can anyone please tell me what am i doing wrong when i open the Test1.enc i get qy{z.{yŁ„~ Thanks

Replies are listed 'Best First'.
Re: unicode in perl
by ikegami (Patriarch) on Jun 27, 2011 at 07:08 UTC

    You're viewing your source as cp1252, but the encoder expects Unicode code points. Your string is really:

    0084 <-- A nameless, graphless control char 0071 LATIN SMALL LETTER Q 0079 LATIN SMALL LETTER Y 0084 007B LEFT CURLY BRACKET 007A LATIN SMALL LETTER Z 0084 00B7 MIDDLE DOT 007B LEFT CURLY BRACKET 0084 008F SINGLE SHIFT THREE 0079 LATIN SMALL LETTER Y 0084 00A3 POUND SIGN 007F DELETE 0084 008F SINGLE SHIFT THREE 007E TILDE

    You could do

    use Encode qw( decode ); print $FH decode('cp1252', "... the string ...");

    If you do, Perl will see the string the same way you are viewing it.

    However, you should save your source as US-ASCII or UTF-8. Use use utf8; if you do the latter.

      ok here is my problem what i need to do is write the sequence generated from a function to a file which is has encoding as unicode qy{z{y~ is what i get in the file why are the ,, characters missing

        This post doesn't add anything new.

        You have text encoded using cp1252 and you are pretending it's not encoded. Decoding it will do the trick.

Re: unicode in perl
by ikegami (Patriarch) on Jun 27, 2011 at 06:45 UTC
    I don't see the difference between the two strings
Re: unicode in perl
by paramjit (Novice) on Jun 27, 2011 at 06:53 UTC
    i have updated the output