| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "PERLCN 1" |
| 58 |
.TH PERLCN 1 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
perlcn \- 简体中文 Perl 指南 |
| 65 |
.SH DESCRIPTION |
| 66 |
.IX Header "DESCRIPTION" |
| 67 |
欢迎来到 Perl 的天地! |
| 68 |
.PP |
| 69 |
从 5.8.0 版开始, Perl 具备了完善的 Unicode (统一码) 支持, |
| 70 |
也连带支持了许多拉丁语系以外的编码方式; CJK (中日韩) 便是其中的一部分. |
| 71 |
Unicode 是国际性的标准, 试图涵盖世界上所有的字符: 西方世界, 东方世界, |
| 72 |
以及两者间的一切 (希腊文, 叙利亚文, 阿拉伯文, 希伯来文, 印度文, |
| 73 |
印地安文, 等等). 它也容纳了多种操作系统与平台 (如 PC 及麦金塔). |
| 74 |
.PP |
| 75 |
Perl 本身以 Unicode 进行操作. 这表示 Perl 内部的字符串数据可用 Unicode |
| 76 |
表示; Perl 的函数与运算符 (例如正则表达式匹配) 也能对 Unicode 进行操作. |
| 77 |
在输入及输出时, 为了处理以 Unicode 之前的编码方式储存的数据, Perl |
| 78 |
提供了 Encode 这个模块, 可以让你轻易地读写使用旧有的编码格式的数据. |
| 79 |
.PP |
| 80 |
Encode 扩展模块支持下列简体中文的编码方式 ('gb2312' 表示 'euc\-cn'): |
| 81 |
.PP |
| 82 |
.Vb 6 |
| 83 |
\& euc\-cn Unix 扩展字符集, 也就是俗称的国标码 |
| 84 |
\& gb2312\-raw 未经处理的 (低比特) GB2312 字符表 |
| 85 |
\& gb12345 未经处理的中国用繁体中文编码 |
| 86 |
\& iso\-ir\-165 GB2312 + GB6345 + GB8565 + 新增字符 |
| 87 |
\& cp936 字码页 936, 也可以用 \*(AqGBK\*(Aq (扩充国标码) 指明 |
| 88 |
\& hz 7 比特逸出式 GB2312 编码 |
| 89 |
.Ve |
| 90 |
.PP |
| 91 |
举例来说, 将 EUC-CN 编码的文件转成 Unicode, 只需输入以下命令: |
| 92 |
.PP |
| 93 |
.Vb 1 |
| 94 |
\& perl \-Mencoding=euc\-cn,STDOUT,utf8 \-pe1 < file.euc\-cn > file.utf8 |
| 95 |
.Ve |
| 96 |
.PP |
| 97 |
Perl 也内附了 "piconv", 一个完全以 Perl 写成的字符转换工具程序, 用法如下: |
| 98 |
.PP |
| 99 |
.Vb 2 |
| 100 |
\& piconv \-f euc\-cn \-t utf8 < file.euc\-cn > file.utf8 |
| 101 |
\& piconv \-f utf8 \-t euc\-cn < file.utf8 > file.euc\-cn |
| 102 |
.Ve |
| 103 |
.PP |
| 104 |
另外, 利用 encoding 模块, 你可以轻易写出以字符为单位的代码, 如下所示: |
| 105 |
.PP |
| 106 |
.Vb 7 |
| 107 |
\& #!/usr/bin/env perl |
| 108 |
\& # 启动 euc\-cn 字串解析; 标准输出入及标准错误都设为 euc\-cn 编码 |
| 109 |
\& use encoding \*(Aqeuc\-cn\*(Aq, STDIN => \*(Aqeuc\-cn\*(Aq, STDOUT => \*(Aqeuc\-cn\*(Aq; |
| 110 |
\& print length("骆驼"); # 2 (双引号表示字符) |
| 111 |
\& print length(\*(Aq骆驼\*(Aq); # 4 (单引号表示字节) |
| 112 |
\& print index("谆谆教诲", "蛔唤"); # \-1 (不包含此子字符串) |
| 113 |
\& print index(\*(Aq谆谆教诲\*(Aq, \*(Aq蛔唤\*(Aq); # 1 (从第二个字节开始) |
| 114 |
.Ve |
| 115 |
.PP |
| 116 |
在最后一列例子里, "谆" 的第二个字节与 "谆" 的第一个字节结合成 EUC-CN |
| 117 |
码的 "蛔"; "谆" 的第二个字节则与 "教" 的第一个字节结合成 "唤". |
| 118 |
这解决了以前 EUC-CN 码匹配处理上常见的问题. |
| 119 |
.SS 额外的中文编码 |
| 120 |
.IX Subsection "额外的中文编码" |
| 121 |
如果需要更多的中文编码, 可以从 CPAN (<https://www.cpan.org/>) 下载 |
| 122 |
Encode::HanExtra 模块. 它目前提供下列编码方式: |
| 123 |
.PP |
| 124 |
.Vb 1 |
| 125 |
\& gb18030 扩充过的国标码, 包含繁体中文 |
| 126 |
.Ve |
| 127 |
.PP |
| 128 |
另外, Encode::HanConvert 模块则提供了简繁转换用的两种编码: |
| 129 |
.PP |
| 130 |
.Vb 2 |
| 131 |
\& big5\-simp Big5 繁体中文与 Unicode 简体中文互转 |
| 132 |
\& gbk\-trad GBK 简体中文与 Unicode 繁体中文互转 |
| 133 |
.Ve |
| 134 |
.PP |
| 135 |
若想在 GBK 与 Big5 之间互转, 请参考该模块内附的 b2g.pl 与 g2b.pl 两个程序, |
| 136 |
或在程序内使用下列写法: |
| 137 |
.PP |
| 138 |
.Vb 3 |
| 139 |
\& use Encode::HanConvert; |
| 140 |
\& $euc_cn = big5_to_gb($big5); # 从 Big5 转为 GBK |
| 141 |
\& $big5 = gb_to_big5($euc_cn); # 从 GBK 转为 Big5 |
| 142 |
.Ve |
| 143 |
.SS 进一步的信息 |
| 144 |
.IX Subsection "进一步的信息" |
| 145 |
请参考 Perl 内附的大量说明文件 (不幸全是用英文写的), 来学习更多关于 |
| 146 |
Perl 的知识, 以及 Unicode 的使用方式. 不过, 外部的资源相当丰富: |
| 147 |
.SS "提供 Perl 资源的网址" |
| 148 |
.IX Subsection "提供 Perl 资源的网址" |
| 149 |
.IP <https://www.perl.org/> 4 |
| 150 |
.IX Item "<https://www.perl.org/>" |
| 151 |
.PP |
| 152 |
Perl 的首页 |
| 153 |
.IP <https://www.perl.com/> 4 |
| 154 |
.IX Item "<https://www.perl.com/>" |
| 155 |
由 Perl 基金会运营的文章辑录 |
| 156 |
.IP <https://www.cpan.org/> 4 |
| 157 |
.IX Item "<https://www.cpan.org/>" |
| 158 |
Perl 综合典藏网 (Comprehensive Perl Archive Network) |
| 159 |
.IP <https://lists.perl.org/> 4 |
| 160 |
.IX Item "<https://lists.perl.org/>" |
| 161 |
Perl 邮递论坛一览 |
| 162 |
.SS "学习 Perl 的网址" |
| 163 |
.IX Subsection "学习 Perl 的网址" |
| 164 |
.IP <http://www.oreilly.com.cn/index.php?func=booklist&cat=68> 4 |
| 165 |
.IX Item "<http://www.oreilly.com.cn/index.php?func=booklist&cat=68>" |
| 166 |
简体中文版的欧莱礼 Perl 书藉 |
| 167 |
.SS "Perl 使用者集会" |
| 168 |
.IX Subsection "Perl 使用者集会" |
| 169 |
.IP <https://www.pm.org/groups/asia.html> 4 |
| 170 |
.IX Item "<https://www.pm.org/groups/asia.html>" |
| 171 |
中国 Perl 推广组一览 |
| 172 |
.SS "Unicode 相关网址" |
| 173 |
.IX Subsection "Unicode 相关网址" |
| 174 |
.IP <https://www.unicode.org/> 4 |
| 175 |
.IX Item "<https://www.unicode.org/>" |
| 176 |
Unicode 学术学会 (Unicode 标准的制定者) |
| 177 |
.IP <https://www.cl.cam.ac.uk/%7Emgk25/unicode.html> 4 |
| 178 |
.IX Item "<https://www.cl.cam.ac.uk/%7Emgk25/unicode.html>" |
| 179 |
Unix/Linux 上的 UTF\-8 及 Unicode 常见问题解答 |
| 180 |
.SH "SEE ALSO" |
| 181 |
.IX Header "SEE ALSO" |
| 182 |
Encode, Encode::CN, encoding, perluniintro, perlunicode |
| 183 |
.SH AUTHORS |
| 184 |
.IX Header "AUTHORS" |
| 185 |
Jarkko Hietaniemi <[email protected]> |
| 186 |
.PP |
| 187 |
Audrey Tang (唐凤) <[email protected]> |
| 188 |
.PP |
| 189 |
Sizhe Zhao <[email protected]> |