我正在尝试使用 perl 在 Windows 中打印土耳其语中的 ş,ı,ö,ç 等字符,但我做不到。我的主要目的是在 Windows 中使用特殊字符创建文件夹。
这是我的代码:
use Text::Iconv;
use strict;
use warnings;
$conve = Text::Iconv->new("windows-1254","UTF-16");
$converted = $conve->convert("ş");
print $converted;
system("mkdir $converted");
我在第 7 行收到格式错误的 utf-8 字符(字节 0xfe)aa.pl
最佳答案
将以下内容另存为 UTF-8:
use utf8;
use strict;
use warnings;
use open ":std", ":encoding(cp1254)"; # Set encoding for STD*
use Encode qw( encode );
my $file_name = "ş";
print "$file_name\n";
system(encode('cp1254', qq{mkdir "$file_name"}));
use utf8
告诉 Perl 源代码是 UTF-8。
use open ":std", ":encoding(cp1254)";
导致发送到 STDOUT 和 STDERR 的文本使用 cp1254 进行编码,并导致从 STDIN 读取的文本从 cp1254 解码.
它不会影响发送到系统调用(例如system
)的内容,因此您需要对它们进行显式编码。
关于Perl 在 Windows 中使用外来字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556829/