develooper Front page | perl.perl5.porters | Postings from May 2004

Should an :encoding(ucs2) layer implicitly pop the :crlf layer?

Thread Next
Jan Dubois
May 14, 2004 17:41
Should an :encoding(ucs2) layer implicitly pop the :crlf layer?
Message ID:
I noticed that on Windows you cannot simply write a file in UCS2LE by opening
the output file with:

    open(my $fh, ">:encoding(ucs2le)", $file);
    print $fh, "\r\n";

This will result in a bogus 5 byte file containing:

    0D 00 0D 0A 00

To get the correct result I had to do:

    open(my $fh, ">:raw", $file);
    binmode($fh, ":encoding(ucs2le)");
    print $fh, "\r\n";

which generates the expected 4 bytes:

    0D 00 0A 00

Is this the expected behavior?  If so, then I think this needs to be
documented better, as I expected both cases to create the same result.

I have no idea if for the :utf8 case you would want to keep the :crlf layer:

    open(my $fh, ">:encoding(utf8)", $file);
    print $fh, "\r\n";

I can see how you may expect this to print 3 bytes and not 2.  But inserting a
single byte into a UCS2 stream is always a bug.  The :crlf layer would have to
insert 0D 00 instead of just 0D.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About