我正在尝试用UTF-8中的Perl创建/保存HTML文件,但到目前为止我没有做任何事情.一个以前的答案在这里SO据说用binmode,所以我试过了.这是我的代码:
open (OUT, ">$sectionfilename");
binmode(OUT, ":utf8");
print OUT $section;
close OUT;
Run Code Online (Sandbox Code Playgroud)
当我在像记事本这样的文本编辑器中打开这些文件时,它们仍然是ANSI编码.我究竟做错了什么?
dax*_*xim 14
文本编辑器是检查编码等低级内容的糟糕工具.请改用hexviewer/hexdumper.编写示例的现代方法:
use autodie qw(:all);
open my $out, '>:encoding(UTF-8)', $sectionfilename;
print {$out} $section;
close $out;
Run Code Online (Sandbox Code Playgroud)
autodie启用自动错误检查.
似乎对我有用:
C:\Documents and Settings>cat a.pl
$sectionfilename = "a.txt";
$section = "Hello \x{263A}!\n";
open (OUT, ">$sectionfilename");
binmode(OUT, ":utf8");
print OUT $section;
close OUT;    
C:\Documents and Settings>perl a.pl
C:\Documents and Settings>file a.txt
a.txt: UTF-8 Unicode text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)
但是当我更改要写入的文本时:
$section = "Hello";
Run Code Online (Sandbox Code Playgroud)
并运行:
C:\Documents and Settings>perl a.pl
C:\Documents and Settings>file a.txt
a.txt: ASCII text, with no line terminators
Run Code Online (Sandbox Code Playgroud)