我正在编写一个脚本,它将 UTF-16 编码的文本文件作为输入并输出 UTF-16 编码的文本文件。
use open "encoding(UTF-16)";
open INPUT, "< input.txt"
or die "cannot open > input.txt: $!\n";
open(OUTPUT,"> output.txt");
while(<INPUT>) {
print OUTPUT "$_\n"
}
假设我的程序将 input.txt 中的所有内容写入 output.txt 中。
这在我的 cygwin 环境中工作得很好,它使用“这是为 cygwin-thread-multi-64int 构建的 perl 5,版本 14,subversion 2 (v5.14.2)”
但在我的 Windows 环境中,它使用“这是为 MSWin32-x64-多线程构建的 perl 5,版本 12,subversion 3 (v5.12.3)”,
除了第一行之外,output.txt 中的每一行都以疯狂的符号开头。
例如:
<FIRST LINE OF TEXT>
㈀ Ⰰ ㈀Ⰰ 嘀愀 ㌀ 䌀栀椀愀 䐀⸀⸀⸀ 儀甀愀渀最 䠀ഊ<SECOND LINE OF TEXT>
...
谁能解释一下为什么它在 cygwin 上工作但在 Windows 上不起作用?
编辑:按照建议打印编码层后。
在Windows环境下:
unix
crlf
encoding(UTF-16)
utf8
unix
crlf
encoding(UTF-16)
utf8
在Cygwin环境中:
unix
perlio
encoding(UTF-16)
utf8
unix
perlio
encoding(UTF-16)
utf8
唯一的区别在于 perlio 和 crlf 层之间。