如何从 Perl 输出 UTF-8? [英] How can I output UTF-8 from Perl?
问题描述
我正在尝试使用 utf8
编译指示编写 Perl 脚本,但得到了意想不到的结果.我使用的是 Mac OS X 10.5 (Leopard),并且正在使用 TextMate 进行编辑.我的编辑器和操作系统的所有设置都默认为以 utf-8 格式写入文件.
I am trying to write a Perl script using the utf8
pragma, and I'm getting unexpected results. I'm using Mac OS X 10.5 (Leopard), and I'm editing with TextMate. All of my settings for both my editor and operating system are defaulted to writing files in utf-8 format.
但是,当我将以下内容输入到文本文件中,将其另存为.pl"并执行时,我得到了友好的带问号的菱形"代替非 ASCII 字符.
However, when I enter the following into a text file, save it as a ".pl", and execute it, I get the friendly "diamond with a question mark" in place of the non-ASCII characters.
#!/usr/bin/env perl -w
use strict;
use utf8;
my $str = 'Çirçös';
print( "$str
" );
知道我做错了什么吗?我希望在输出中得到 'Çirçös',但我得到的是 ' ir s'.
Any idea what I'm doing wrong? I expect to get 'Çirçös' in the output, but I get '�ir��s' instead.
推荐答案
use utf8;
不启用 Unicode 输出 - 它使您可以在程序中键入 Unicode.将此添加到程序中,在您的 print()
语句之前:
use utf8;
does not enable Unicode output - it enables you to type Unicode in your program. Add this to the program, before your print()
statement:
binmode(STDOUT, ":utf8");
看看是否有帮助.这应该使 STDOUT
以 UTF-8 而不是普通的 ASCII 输出.
See if that helps. That should make STDOUT
output in UTF-8 instead of ordinary ASCII.
这篇关于如何从 Perl 输出 UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!