http://www.perlmonks.org?node_id=550491


in reply to CR-LF Newlines as 2 distinct characters

The encoding is "utf16le", that is "utf16 little endian", which is the standard representation on (little-endian) Windows x86 machines. Unicode include a byte-order mark character just so you can distinguish utf16be from utf16le.

It tried using binmode to see if I could get the :crlf layer to be applied before the encoding layer, but it doesn't work. This seems like something that should be fixed in perl, since Windows is the most commonly used OS in the world, after all.

Anyway, here's how I would do it:

open PLAYLIST, ">", "playlist.plp" or die "Couldn't open playlist.plp: $!\n"; binmode PLAYLIST, ":raw:encoding(utf16le)" or die "binmode: $!\n"; print PLAYLIST "abcd\r\n"; $\ = "\r\n"; print PLAYLIST "efgh";
You can actually combine the binmode options into the open statement like so:
open PLAYLIST, ">:raw:encoding(utf16le)", "playlist.plp" or die "Couldn't open playlist.plp: $!\n"; print PLAYLIST "abcd\r\n"; $\ = "\r\n"; print PLAYLIST "efgh";