perl use utf8

utf8 Perl编译 来启用/禁用 UTF-8(or UTF-EBCDIC) 在源代码里

简洁:

use utf8;
 no utf8;

 # Convert the internal representation of a Perl scalar to/from UTF-8.

 $num_octets = utf8::upgrade($string);
 $success    = utf8::downgrade($string[, $fail_ok]);

 # Change each character of a Perl scalar to/from a series of
 # characters that represent the UTF-8 bytes of each original character.

 utf8::encode($string);  # "x{100}"  becomes "xc4x80"
 utf8::decode($string);  # "xc4x80" becomes "x{100}"

 # Convert a code point from the platform native character set to
 # Unicode, and vice-versa.
 $unicode = utf8::native_to_unicode(ord('A')); # returns 65 on both
                                               # ASCII and EBCDIC
                                               # platforms
 $native = utf8::unicode_to_native(65);        # returns 65 on ASCII
                                               # platforms; 193 on
                                               # EBCDIC

 $flag = utf8::is_utf8($string); # since Perl 5.8.1
 $flag = utf8::valid($string)

描述:

使用utf8 编译告诉perl解析器 允许UTF-8在程序文本在当前的词法范围。


no utf8 编译告诉Perl 切回到对待文本作为literal 字节在当前的词法范围。

(在EBCDIC 平台,技术是允许UTF-EBCDIC, 不是UTF-8.

但是这个区别是学术的,因此在这个文件术语UTF-8是用于两者)

不要使用这个编译除了告诉Perl你的脚本是用UTF-8写的。

下面的描述是直接使用没有use utf8:


因为它不可能可靠的告诉UTF-8 从本地的8位编码,


你需要一个字节顺序标记在你的源代码的开始,或者use utf8,来指导perl



当UTF-8 变为标准的源代码格式,这个指令会有效的 变成一个 no-op


也可以查询-C切换的影响和它的cousin, PERL_UNICODE 环境变量

启动utf8程序有下面的影响:

在源文本中的字节不是以 ASCII character set 

会被对待一个 literal UTF-8 sequence的一部分。

这包括很多的literals  比如标识符名称,字符串常量,和恒定的正则表达式模式。


注意如果你有non-ASCII, non-UTF-8 字节在你的脚本


use utf8会不高兴。因为你需要有这样的字节在use utf8,

你可以禁用这个程序直到最后的块(或者文件,如果在顶层) 通过使用no utf8

有用的函数;

下面的函数是定义在e utf8:: package,你不需要say use utf8 来使用那些

实际上 你不应该say 除非你真的需要UTF-8源代码

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199641.html