How can I embede chinese characters in my Perl source?
In my script I need to "qw" some chinese character int开发者_如何学编程o a string . when I run the script , perl conplains that there is unrecognized character in the script . Although I know it must related to encode related stuff , but I don't know how to solve it . so turn to you for help .
thanks in advance .
Embedding literal strings in Perl source code is easy. Use the utf8
pragma. (Do not use the encoding
pragma, it is highly problematic.)
use utf8;
my $perl_string = '你好,张HY';
Your text editor must save this file as UTF-8, too.
Then, if you want to output Perl strings, you must encode them first, see perlunitut and perlunifaq.
use Encode qw(encode);
use Encode::HanExtra;
# for example, printing to STDOUT
print encode('UTF-8', $perl_string); # in a Linux environment
print encode('GB18030', $perl_string); # in a Windows environment
I also want to advise you that since the year 2006 support for the national standard GB18030 is mandatory for all software products in the PRC - you have to install Encode::HanExtra from CPAN.
精彩评论