Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: PerlIO: crlf layer on Windows interfering with UCS-2 unicode

by ikegami (Pope)
on Jun 03, 2008 at 10:53 UTC ( #689841=note: print w/replies, xml ) Need Help??

in reply to PerlIO: crlf layer on Windows interfering with UCS-2 unicode

Surprisingly, the trick only works for open, not binmode.

use strict; use warnings; sub dump_layers(*) { my @layers = PerlIO::get_layers($_[0]); print STDERR "@layers\n"; } my $file = 'temp'; { open(my $fh, '>:raw:encoding(ucs-2le):crlf:utf8', $file) or die; dump_layers($fh); } { open(my $fh, '<:raw:encoding(ucs-2le):crlf:utf8', $file) or die; dump_layers($fh); } unlink $file; binmode STDOUT, ':raw:encoding(ucs-2le):crlf:utf8' or die; dump_layers STDOUT; binmode STDIN, ':raw:encoding(ucs-2le):crlf:utf8' or die; dump_layers STDIN;
unix encoding(UCS-2LE) utf8 crlf utf8 unix encoding(UCS-2LE) utf8 crlf utf8 unix crlf encoding(UCS-2LE) utf8 unix crlf encoding(UCS-2LE) utf8

The only solution I've found follows, but I don't like it.

binmode $fh, ':raw:pop:encoding(ucs-2le):crlf:utf8'; # ^^^

The issue is that :raw disables the existing :crlf layer (but doesn't remove it) when using binmode. Then, the later :crlf reactivates the earlier :crlf layer instead of adding a new layer, messing everything up.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://689841]
[stevieb]: I am thankful that I fell into Perl years ago in the manner I did. I don't believe there is another language that people care about so much, that they're willing to give everything. Langs come and go, but in Perl, it seems, things remain consistent.
[stevieb]: The people back then (~2000) are the same people now, but with much more experience. I'm appreciative that I fell into the Perl rabbit hole.
[stevieb]: Coding circles around Python devs, showing how unit tests should be done, enabling easy access to everything, learning basic C to wrap for direct hardware access... I am thankful.
[stevieb]: I'm in an appreciative mood. Thank you fellow Monks, for all I know, and for the knowledge which I can pass on

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-02-25 01:22 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (364 votes). Check out past polls.