![](/img/trans.png)
[英]Perl read .DAT file with UTF-8 BOM format and write it with UTF-8 format without BOM
[英]Write file in UTF-8 mode using Perl
我需要在Perl中使用UTF-8模式編寫文件。 我該如何創建?
在這種情況下,有人可以幫忙嗎?
我正在嘗試這樣,找到下面的代碼,
use utf8;
use open ':encoding(utf8)';
binmode(FH, ":utf32");
open(FH, ">test11.txt");
print FH "something Çirçös";
它正在創建UTF-8格式的文件。 但是我需要確保此腳本是否正在發生這種情況。 因為如果我在不使用utf8編碼的情況下編寫文件,文件內容也會自動采用UTF-8格式。
你要
use utf8; # Source code is encoded using UTF-8.
open(my $FH, ">:encoding(utf-8)", "test11.txt")
or die $!;
print $FH "something Çirçös";
要么
use utf8; # Source code is encoded using UTF-8.
use open ':encoding(utf-8)'; # Sets the default encoding for handles opened in scope.
open(my $FH, ">", "test11.txt")
or die $!;
print $FH "something Çirçös";
筆記:
utf-8
(不區分大小寫),而不是utf8
(Perl特定的編碼)。 my
)變量。 如果不執行編碼指令,您可能會很幸運並獲得正確的輸出(以及“寬字符”警告)。 不要指望這一點。 您不會總是很幸運。
# Unlucky. $ perl -we'use utf8; print "é"' | od -t x1 0000000 e9 0000001 # Lucky. $ perl -we'use utf8; print "é♡"' | od -t x1 Wide character in print at -e line 1. 0000000 c3 a9 e2 99 a1 0000005 # Correct. $ perl -we'use utf8; binmode STDOUT, ":encoding(utf-8)"; print "é♡"' | od -t x1 0000000 c3 a9 e2 99 a1 0000005
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.