CRLF translation using Unicode in Perl

I am trying to write a Unicode file (UCS-2 Little Endian) in Perl on Windows, for example.

open my $f, ">$fName" or die "can't write $fName\n";
binmode $f, ':raw:encoding(UCS-2LE)';
print $f, "ohai\ni can haz unicodez?\nkthxbye\n";
close $f;

This basically works, except that I no longer receive the automatic translation of LF → CR / LF in the output that I get in plain text files. (Output files have only LF). If I do not specify: raw or add: crlf in the binmode call, then the output file is garbled. I tried reordering the "directives" (that is: coding to: raw) and cannot make it work. The same problem exists for reading.

+2
source share
3 answers

:crlf 0x0A → 0x0D 0x0A (\n --> \r\n) , .

raw, CR?

print $f "ohai\r\ni can haz unicodez?\r\nkthxbye\r\n";

, , :

## never mind - $/ doesn't work
# print $f "ohai$/i can haz unicodez?$/kthxbye$/";

open DUMMY, '>', 'dummy'; print DUMMY "\n"; close DUMMY;
open DUMMY, '<:raw', 'dummy'; $EOL = <DUMMY>; close DUMMY;
unlink 'dummy';

...

print $f "ohai${EOL}i can haz unicodez?${EOL}kthxbye${EOL}";
+2

:

open my $f, ">:encoding(UCS-2LE):crlf", "test.txt";
print $f "ohai\ni can haz unicodez?\nkthxbye\n";
close $f;

UCS-16 LE test.txt

ohai
i can haz unicodez?
kthxbye
+2

, , perl 5.10.1:

Input:

open(my $f_in, '<:raw:perlio:via(File::BOM):crlf', $file);

:

open(my $f_out, '>:raw:perlio:encoding(UTF-16LE):crlf:via(File::BOM)', $file);

BOM, CRLF- / UTF-16LE .

, perlmonks , binmode() open(), ": pop":

binmode $f_out, ':raw:pop:perlio:encoding(UTF-16LE):crlf';

. ": (File:: BOM)".

:

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

http://metacpan.org/pod/File::BOM

+1
source

Source: https://habr.com/ru/post/1755880/


All Articles