Hi Monks, I have this problem I can't seem to solve. I use Perl 5.8 under Windows XP. I want to read a Unicode UTF16 file, do some processing, and write out again Unicode UTF-16.
I can't print out a proper new line. I tried 'pack', 'chr' (using the Unicode codes, not the "\r" "\n" names), and many other options. Using an hexadecimal editor I see I always end up printing out "0D 00 0D 0A 00" and not "0D 00 0A 00". It seems that no matter how I refer to the line feed character using the 000A code, a "0D" byte gets automatically stuck in front of it. Here is my (non-working) snippet:
use strict;
my $infile=shift;
my $outfile=shift;
open IN, "<:encoding(UTF-16LE)", $infile or die;
open OUT, ">:encoding(UTF-16LE)", $outfile or die;
binmode OUT, ':utf8';
while (<IN>) {
chomp;
print OUT "$_";
#also tried:
#print OUT "\x{0D}\x{0A}";
my $r=pack("U", 0x000D);
my $n=pack("U", 0x000A);
print OUT $r;
print OUT $n;
#also tried:
#my $n=chr(0x000D); etc.
}
close(IN);
close(OUT);
Any idea?
Thanks in advance
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.