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