如何从Perl输出UTF-8? [英] How can I output UTF-8 from Perl?

查看:103
本文介绍了如何从Perl输出UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用"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\n" );

知道我在做什么错吗?我希望在输出中得到'Ç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 output -它使您可以在程序中键入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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆