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

Hello! I have a .csv file that contains unicode and when I read it in line by line and output the line to a new output file, the lines with unicoding doesn't get output to a file. Does Perl support unicode? Thanks in advance!

Replies are listed 'Best First'.
Re: Unicode in Perl
by graff (Chancellor) on May 10, 2005 at 04:54 UTC
    Saying that the input "contains unicode" is not enough to understand the problem. Unicode can be conveyed in at least three different ways: UTF-16BE, UTF-16LE and UTF-8. The first two differ only in the byte-order of 16-bit "words" (in the computer science sense of "groupings of two bytes"), and both are substantially different from the third option.

    When you say "lines with unicoding don't get output to a file", do you mean that some lines really are composed entirely of plain ASCII characters (and these get through just fine)? If so, then the data probably is UTF-8, because this encoding was designed so that ASCII characters, which are a small subset of Unicode, can be conveyed in their original single-byte form.

    If you have Perl 5.8.x (hopefully, "x" is greater than 0), then it might be enough for you to check out the usage of the "-C" option when running perl (look it up with "perldoc perlrun") -- "-CS" means treat STDIN, STDOUT and STDERR as utf8-protocol file handles; "-CD" means treat any named files that are opened for input and output as having utf8-protocol.

    You can also selectively apply utf8-protocol to specific file handles using "binmode( HANDLE, ":utf8" );" (in case you need the same script to also handle files that contain non-text binary data). Look that up with "perldoc perlunicode" in 5.8.x.

    If your data are some other form of unicode, you'll probably want to use 5.8.x and the "Encode" module, to convert from the (external) UTF-16 form to (perl-internal) utf8 -- and back to UTF-16 if you want to keep that as your storage format.

    If none of this helps, you'll need to post some code and sample data that demonstrate the problem you're having.

Re: Unicode in Perl
by Tanktalus (Canon) on May 10, 2005 at 03:33 UTC

    What version of perl are you using? Unicode support has drastically changed on the 5.6/5.8 boundaries, and getting a "good" answer may depend on which one you're using.

    If you can use any version, try 5.8 (5.8.6 is the latest at this point, I believe). Its unicode support is way better than 5.6 (kudos to p5p!).

Re: Unicode in Perl
by rupesh (Hermit) on May 10, 2005 at 03:52 UTC