原文http://hi.baidu.com/mxp446533129/blog/item/5d9f3812b3e9c414213f2e3b.html
用perl写文本处理程序,或者写服务器端脚本的时候,常常会遇到“Wide character in print” 的警告或者错误。
这是因为在程序中处理中文等宽字符时,perl不能识别要处理的内容。
首先要知道perl只能处理两种编码:ascii码和utf-8。ascii码是很少的,像中文、日文、韩文等字符要想能被perl处理,只能用 utf-8编码方式。字符串在perl内部的存储格式如下图:
当flag是1的时候,perl就会把那个字符串当做utf-8编码的字符来处理;如果是0,perl就不能认知字符串中除了 ascii码之外的字符,这个时候,就会报出“Wide character in print”的警告或者错误。
举个例子,你要在程序中处理‘当历史成为历史’这个字符串,如果你的程序文件是utf-8编码的话,一般情况下直接处理就行了,因为这 时字符串的utf8-flag是打开的。如果你的程序文件是gb2312的话,那么你就需要把那个字符串的utf8-flag打开。但是,一般还会有这样 的问题,因为这个字符串是gb2312编码的,所以你要做两件事情:将字符串的编码转为utf-8和打开utf8-flag。
use Encode;
use strict;
my $str = "当历史成为历史";
Encode::_utf8_on($str);
print $str. "\n";
Encode::_utf8_off($str);
print $str. "\n";
将上面的这段程序存到文件里,试图运行的时候就会报错:Wide character in print at test.pl line 6。这就是因为utf8-flag被关闭,perl不能识别字符串。
当然,在每个处理宽字符的地方加上Encode::_utf8_on函数确实是个解决办法。但是一般来说,在每个地方都加上这样的函 数,既在编写程序的时候麻烦,维护的时候更麻烦。
这里还有一个更好的办法:在程序文件的头部加上以下内容
use utf8;
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
分享到:
相关推荐
Can’t locate ExtUtils/MakeMaker.pm in @INC (@INC contains: inc /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5
自己编写程序,对TCGA的临床信息进行整理和提取,便于做生存分析
l Actual Perl code is typeset in a special monospace font. You'll see this font used in listings and the Input-Output examples, as well as in code snippets. In the explanations of Perl features, ...
今天小编就为大家分享一篇关于解决Can't locate ExtUtils/MakeMaker.pm in @INC报错,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
《An Introduction to Perl Regular Expressions in SAS》,学习正则表达式文档。
Teach Yourself Perl 5 in 21 days.pdf
perl语言作为一种胶水语言已经被业界流传很近,perl的处理文本的能力也是众所周知的,次书,详细描述了多种处理xml文件的包极其大量的范例。
This is a super good perl tutorial. It will teach you perl by so many elementory examples and clear explanations. Worth treasuring.
Teach_Yourself_Perl_in_21_Days 21 days learning and training
Thinking in perl(Perl编程思想)
perl脚本perl脚本perl脚本perl脚本
perl in action 中文 perl in action 中文
Think in perl学习perl的参考书,包括大量的实例
windows Strawberry Perl 5.32最新版本 ,适合调试 perl windows Strawberry Perl 5.32最新版本 ,适合调试 perl windows Strawberry Perl 5.32最新版本 ,适合调试 perl windows Strawberry Perl 5.32最新版本 ...
As in Learning Perl, we designed each chapter to be small enough to read in just an hour or so. Each chapter ends with a series of exercises to help you practice what you've just learned, and the ...
Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0版本,Perl-5.10.0...
coding better, faster, and more reliably in Perl. Focusing on debugging, the most vexing aspect of programming in Perl, this example-rich reference and how-to guide minimizes development, ...
本书向读者展示了如何创建大型,复杂,动态驱动的网站,这些网站看起来不错并且可以使用Perl和Mason轻松维护。
Perl is a high-level programming language with roots in C, sed, awk and shell scripting. Perl is good at handling processes and files, and is especially good at handling text. Perl's hallmarks are ...
In Perl Template Toolkit you'll find detailed coverage of this increasingly popular technology. Written by core members of the technology's development team, the book guides you through the entire ...