1 |
argrath |
1.1 |
|
2 |
|
|
=encoding euc-jp |
3 |
|
|
|
4 |
|
|
=head1 NAME |
5 |
|
|
|
6 |
|
|
=begin original |
7 |
|
|
|
8 |
|
|
perluniintro - Perl Unicode introduction |
9 |
|
|
|
10 |
|
|
=end original |
11 |
|
|
|
12 |
|
|
perluniintro - Perl Unicode の手引き |
13 |
|
|
|
14 |
|
|
=head1 DESCRIPTION |
15 |
|
|
|
16 |
|
|
=begin original |
17 |
|
|
|
18 |
|
|
This document gives a general idea of Unicode and how to use Unicode |
19 |
|
|
in Perl. See L</Further Resources> for references to more in-depth |
20 |
|
|
treatments of Unicode. |
21 |
|
|
|
22 |
|
|
=end original |
23 |
|
|
|
24 |
|
|
このドキュメントは、Unicode の一般的な考えと、 |
25 |
|
|
Perl で Unicode をどのように使うかを書いています。 |
26 |
argrath |
1.2 |
Unicode のより深い扱いへのリファレンスについては L</Further Resources> を |
27 |
|
|
参照してください。 |
28 |
argrath |
1.1 |
|
29 |
|
|
=head2 Unicode |
30 |
|
|
|
31 |
|
|
=begin original |
32 |
|
|
|
33 |
|
|
Unicode is a character set standard which plans to codify all of the |
34 |
|
|
writing systems of the world, plus many other symbols. |
35 |
|
|
|
36 |
|
|
=end original |
37 |
|
|
|
38 |
|
|
Unicode は、文字セットの標準です。 |
39 |
|
|
世界の全ての書記体系と、それに加えて、 |
40 |
|
|
他の多くのシンボルを体系化することを計画しています。 |
41 |
|
|
|
42 |
|
|
=begin original |
43 |
|
|
|
44 |
|
|
Unicode and ISO/IEC 10646 are coordinated standards that unify |
45 |
|
|
almost all other modern character set standards, |
46 |
|
|
covering more than 80 writing systems and hundreds of languages, |
47 |
|
|
including all commercially-important modern languages. All characters |
48 |
|
|
in the largest Chinese, Japanese, and Korean dictionaries are also |
49 |
|
|
encoded. The standards will eventually cover almost all characters in |
50 |
|
|
more than 250 writing systems and thousands of languages. |
51 |
|
|
Unicode 1.0 was released in October 1991, and 6.0 in October 2010. |
52 |
|
|
|
53 |
|
|
=end original |
54 |
|
|
|
55 |
|
|
Unicode と、ISO/IEC 10646 は、よく調整された、符号位置を提供する |
56 |
|
|
標準です。 |
57 |
|
|
Unicode は、ほとんど全ての現代の文字セット標準、80 以上の書記体系と、 |
58 |
|
|
100 以上の言語を網羅します。 |
59 |
|
|
全ての商業的に重要な現代の言語を含みます。 |
60 |
|
|
もっとも大きい中国語、日本語、韓国語、それぞれの辞書の全ての文字もまた、 |
61 |
|
|
符号化されています。 |
62 |
|
|
この標準は、最終的には、250 の書記体系と、1000 以上の言語のほとんどすべての |
63 |
|
|
文字を網羅する予定です。 |
64 |
|
|
Unicode 1.0 は、1991 年 10 月にリリースされ、Unicode 6.0 は、 |
65 |
|
|
2010 年 10 月にリリースされました。 |
66 |
|
|
|
67 |
|
|
=begin original |
68 |
|
|
|
69 |
|
|
A Unicode I<character> is an abstract entity. It is not bound to any |
70 |
|
|
particular integer width, especially not to the C language C<char>. |
71 |
|
|
Unicode is language-neutral and display-neutral: it does not encode the |
72 |
|
|
language of the text, and it does not generally define fonts or other graphical |
73 |
|
|
layout details. Unicode operates on characters and on text built from |
74 |
|
|
those characters. |
75 |
|
|
|
76 |
|
|
=end original |
77 |
|
|
|
78 |
|
|
Unicode の I<文字> は、抽象的な存在です。 |
79 |
|
|
Unicode の文字は、どんな特定の整数幅にも、特に、C 言語の C<char> にも |
80 |
|
|
束縛されません。 |
81 |
|
|
Unicode は、言語中立で、表示中立です: |
82 |
|
|
Unicode は、テキストの言語をエンコードしませんし、 |
83 |
|
|
一般的にはフォントや他のグラフィカルなレイアウトの詳細を定義しません。 |
84 |
|
|
Unicode は、文字と、それらの文字からなるテキストを操作します。 |
85 |
|
|
|
86 |
|
|
=begin original |
87 |
|
|
|
88 |
|
|
Unicode defines characters like C<LATIN CAPITAL LETTER A> or C<GREEK |
89 |
|
|
SMALL LETTER ALPHA> and unique numbers for the characters, in this |
90 |
|
|
case 0x0041 and 0x03B1, respectively. These unique numbers are called |
91 |
|
|
I<code points>. A code point is essentially the position of the |
92 |
|
|
character within the set of all possible Unicode characters, and thus in |
93 |
|
|
Perl, the term I<ordinal> is often used interchangeably with it. |
94 |
|
|
|
95 |
|
|
=end original |
96 |
|
|
|
97 |
|
|
Unicode は、C<LATIN CAPITAL LETTER A> や C<GREEK SMALL LETTER ALPHA> のような |
98 |
|
|
文字と、その文字について固有の番号を定義します。 |
99 |
|
|
この場合は、それぞれ、0x0041 と、0x03B1 になります。 |
100 |
|
|
このような固有の番号は、I<符号位置> と呼ばれます。 |
101 |
|
|
A code point is essentially the position of the |
102 |
|
|
character within the set of all possible Unicode characters, and thus in |
103 |
|
|
Perl, the term I<ordinal> is often used interchangeably with it. |
104 |
|
|
(TBT) |
105 |
|
|
|
106 |
|
|
=begin original |
107 |
|
|
|
108 |
|
|
The Unicode standard prefers using hexadecimal notation for the code |
109 |
|
|
points. If numbers like C<0x0041> are unfamiliar to you, take a peek |
110 |
|
|
at a later section, L</"Hexadecimal Notation">. The Unicode standard |
111 |
|
|
uses the notation C<U+0041 LATIN CAPITAL LETTER A>, to give the |
112 |
|
|
hexadecimal code point and the normative name of the character. |
113 |
|
|
|
114 |
|
|
=end original |
115 |
|
|
|
116 |
|
|
Unicode 標準は、符号位置に 16 進記法を使うのを好みます。 |
117 |
|
|
C<0x0041> のような番号に馴染みがなければ、後のセクション、 |
118 |
|
|
L</"Hexadecimal Notation"> を覗いて見て下さい。 |
119 |
|
|
Unicode 標準は、C<U+0041 LATIN CAPITAL LETTER A> という表記を使って、 |
120 |
|
|
16 進法の符号位置と標準的な文字の名前を書きます。 |
121 |
|
|
|
122 |
|
|
=begin original |
123 |
|
|
|
124 |
|
|
Unicode also defines various I<properties> for the characters, like |
125 |
|
|
"uppercase" or "lowercase", "decimal digit", or "punctuation"; |
126 |
|
|
these properties are independent of the names of the characters. |
127 |
|
|
Furthermore, various operations on the characters like uppercasing, |
128 |
|
|
lowercasing, and collating (sorting) are defined. |
129 |
|
|
|
130 |
|
|
=end original |
131 |
|
|
|
132 |
|
|
Unicode はまた、様々な文字の I<性質> を定義します。 |
133 |
|
|
"大文字"、"小文字"、"10進数字"、"句読点" など; |
134 |
|
|
これらの性質は、文字の名前と独立です。 |
135 |
|
|
更に、様々な文字に対する操作、大文字化や小文字化や並び替えが |
136 |
|
|
定義されています。 |
137 |
|
|
|
138 |
|
|
=begin original |
139 |
|
|
|
140 |
|
|
A Unicode I<logical> "character" can actually consist of more than one internal |
141 |
|
|
I<actual> "character" or code point. For Western languages, this is adequately |
142 |
|
|
modelled by a I<base character> (like C<LATIN CAPITAL LETTER A>) followed |
143 |
|
|
by one or more I<modifiers> (like C<COMBINING ACUTE ACCENT>). This sequence of |
144 |
|
|
base character and modifiers is called a I<combining character |
145 |
|
|
sequence>. Some non-western languages require more complicated |
146 |
|
|
models, so Unicode created the I<grapheme cluster> concept, which was |
147 |
|
|
later further refined into the I<extended grapheme cluster>. For |
148 |
|
|
example, a Korean Hangul syllable is considered a single logical |
149 |
|
|
character, but most often consists of three actual |
150 |
|
|
Unicode characters: a leading consonant followed by an interior vowel followed |
151 |
|
|
by a trailing consonant. |
152 |
|
|
|
153 |
|
|
=end original |
154 |
|
|
|
155 |
|
|
Unicode I<論理> 「文字」は、実際には一つ以上の I<実際> の「文字」または |
156 |
|
|
符号位置から構成されます。 |
157 |
|
|
西洋の言語では、これは (C<LATIN CAPITAL LETTER A> のような)、I<基底文字> |
158 |
|
|
(base character) に続いて、一つ以上の(C<COMBINING ACUTE ACCENT> のような) |
159 |
|
|
I<修飾字> (modifiers) によってモデル化されています。、 |
160 |
|
|
この基底文字と修飾字の並びは、I<結合文字の並び> |
161 |
|
|
(combining character sequence) と呼ばれます。 |
162 |
|
|
一部の非西洋言語ではより複雑なモデルが必要なので、Unicode は |
163 |
|
|
I<書記素クラスタ> (grapheme cluster) という概念を作成し、後に |
164 |
|
|
I<拡張書記素クラスタ> (extended grapheme cluster) という形に洗練させました。 |
165 |
|
|
例えば、ハングル音節文字は一つの論理文字として考えられますが、とても |
166 |
|
|
しばしば三つの実際の Unocde 文字から構成されています: |
167 |
|
|
先頭子音に引き続いて内部母音、それに引き続いて末尾子音です。 |
168 |
|
|
|
169 |
|
|
=begin original |
170 |
|
|
|
171 |
|
|
Whether to call these extended grapheme clusters "characters" depends on your |
172 |
|
|
point of view. If you are a programmer, you probably would tend towards seeing |
173 |
|
|
each element in the sequences as one unit, or "character". However from |
174 |
|
|
the user's point of view, the whole sequence could be seen as one |
175 |
|
|
"character" since that's probably what it looks like in the context of the |
176 |
|
|
user's language. In this document, we take the programmer's point of |
177 |
|
|
view: one "character" is one Unicode code point. |
178 |
|
|
|
179 |
|
|
=end original |
180 |
|
|
|
181 |
|
|
これらの拡張書記素クラスタを「複数の文字」と呼ぶかどうかは、 |
182 |
|
|
考え方によります。 |
183 |
|
|
プログラマならば、おそらく、この順番のそれぞれの要素を、 |
184 |
|
|
1 つの単位か "文字" として、見ようとするでしょう。 |
185 |
|
|
全ての順番を一つの"文字"として見ることができます。 |
186 |
|
|
しかし、ユーザの考え方からは、おそらくユーザの言語の文脈でみえるような |
187 |
|
|
ものでしょう。 |
188 |
|
|
In this document, we take the programmer's point of |
189 |
|
|
view: one "character" is one Unicode code point. |
190 |
|
|
(TBT) |
191 |
|
|
|
192 |
|
|
=begin original |
193 |
|
|
|
194 |
|
|
For some combinations of base character and modifiers, there are |
195 |
|
|
I<precomposed> characters. There is a single character equivalent, for |
196 |
|
|
example, to the sequence C<LATIN CAPITAL LETTER A> followed by |
197 |
|
|
C<COMBINING ACUTE ACCENT>. It is called C<LATIN CAPITAL LETTER A WITH |
198 |
|
|
ACUTE>. These precomposed characters are, however, only available for |
199 |
|
|
some combinations, and are mainly meant to support round-trip |
200 |
|
|
conversions between Unicode and legacy standards (like ISO 8859). Using |
201 |
|
|
sequences, as Unicode does, allows for needing fewer basic building blocks |
202 |
|
|
(code points) to express many more potential grapheme clusters. To |
203 |
|
|
support conversion between equivalent forms, various I<normalization |
204 |
|
|
forms> are also defined. Thus, C<LATIN CAPITAL LETTER A WITH ACUTE> is |
205 |
|
|
in I<Normalization Form Composed>, (abbreviated NFC), and the sequence |
206 |
|
|
C<LATIN CAPITAL LETTER A> followed by C<COMBINING ACUTE ACCENT> |
207 |
|
|
represents the same character in I<Normalization Form Decomposed> (NFD). |
208 |
|
|
|
209 |
|
|
=end original |
210 |
|
|
|
211 |
|
|
For some combinations of base character and modifiers, there are |
212 |
|
|
I<precomposed> characters. There is a single character equivalent, for |
213 |
|
|
example, to the sequence C<LATIN CAPITAL LETTER A> followed by |
214 |
|
|
C<COMBINING ACUTE ACCENT>. It is called C<LATIN CAPITAL LETTER A WITH |
215 |
|
|
ACUTE>. These precomposed characters are, however, only available for |
216 |
|
|
some combinations, and are mainly meant to support round-trip |
217 |
|
|
conversions between Unicode and legacy standards (like ISO 8859). Using |
218 |
|
|
sequences, as Unicode does, allows for needing fewer basic building blocks |
219 |
|
|
(code points) to express many more potential grapheme clusters. To |
220 |
|
|
support conversion between equivalent forms, various I<normalization |
221 |
|
|
forms> are also defined. Thus, C<LATIN CAPITAL LETTER A WITH ACUTE> is |
222 |
|
|
in I<Normalization Form Composed>, (abbreviated NFC), and the sequence |
223 |
|
|
C<LATIN CAPITAL LETTER A> followed by C<COMBINING ACUTE ACCENT> |
224 |
|
|
represents the same character in I<Normalization Form Decomposed> (NFD). |
225 |
|
|
(TBT) |
226 |
|
|
|
227 |
|
|
=begin original |
228 |
|
|
|
229 |
|
|
Because of backward compatibility with legacy encodings, the "a unique |
230 |
|
|
number for every character" idea breaks down a bit: instead, there is |
231 |
|
|
"at least one number for every character". The same character could |
232 |
|
|
be represented differently in several legacy encodings. The |
233 |
|
|
converse is not also true: some code points do not have an assigned |
234 |
|
|
character. Firstly, there are unallocated code points within |
235 |
|
|
otherwise used blocks. Secondly, there are special Unicode control |
236 |
|
|
characters that do not represent true characters. |
237 |
|
|
|
238 |
|
|
=end original |
239 |
|
|
|
240 |
|
|
レガシーエンコーディングとの後方互換性のために、 |
241 |
|
|
"全ての文字に固有の番号" という考えは、少々壊れています: |
242 |
|
|
その代わりに、"少なくとも全ての文字に 1 つの番号" があります。 |
243 |
|
|
同じ文字が、いくつかのレガシーエンコーディングの中で、違うように |
244 |
|
|
表現されていました。 |
245 |
|
|
逆は真でもなく: 符号位置には、文字が割り当てられていないものも |
246 |
|
|
あります。 |
247 |
|
|
1 番目に、使われているブロック内にもかかわらず、割り当てられていない |
248 |
|
|
符号位置があります。 |
249 |
|
|
2 番目に、特別な Unicode のコントロール文字があり、それらは、本物の文字を |
250 |
|
|
表現しません。 |
251 |
|
|
|
252 |
|
|
=begin original |
253 |
|
|
|
254 |
|
|
A common myth about Unicode is that it is "16-bit", that is, |
255 |
|
|
Unicode is only represented as C<0x10000> (or 65536) characters from |
256 |
|
|
C<0x0000> to C<0xFFFF>. B<This is untrue.> Since Unicode 2.0 (July |
257 |
|
|
1996), Unicode has been defined all the way up to 21 bits (C<0x10FFFF>), |
258 |
|
|
and since Unicode 3.1 (March 2001), characters have been defined |
259 |
|
|
beyond C<0xFFFF>. The first C<0x10000> characters are called the |
260 |
|
|
I<Plane 0>, or the I<Basic Multilingual Plane> (BMP). With Unicode |
261 |
|
|
3.1, 17 (yes, seventeen) planes in all were defined--but they are |
262 |
|
|
nowhere near full of defined characters, yet. |
263 |
|
|
|
264 |
|
|
=end original |
265 |
|
|
|
266 |
|
|
Unicode について、よく知られている神話は、Unicode が、「16 ビット」である、 |
267 |
|
|
というものです。 |
268 |
|
|
Unicode は、C<0x0000> から、C<0xFFFF> までで、C<0x10000> (か、65536)の |
269 |
|
|
文字を表現するだけ、というものです。 |
270 |
|
|
B<これは真実ではありません>。 |
271 |
|
|
Unicode 2.0(1996 年 7 月)から、Unicode は、21ビット(C<0x10FFFF>)まで、 |
272 |
|
|
様々に定義されています。 |
273 |
|
|
Unicode 3.1(2001 年 3 月)から、文字は、C<0xFFFF> を超えて、定義されました。 |
274 |
|
|
最初の C<0x10000> 文字は、 |
275 |
|
|
I<Plane 0>、または、I<Basime Multilingual Plane>(BMP)と、と呼ばれました。 |
276 |
|
|
Unicode 3.1 で、全部で 17(そう、17)の Plane が定義されました -- ですが、 |
277 |
|
|
まだ、定義された全文字のどこにも、まだ近くにありません。 |
278 |
|
|
|
279 |
|
|
=begin original |
280 |
|
|
|
281 |
|
|
Another myth is about Unicode blocks--that they have something to |
282 |
|
|
do with languages--that each block would define the characters used |
283 |
|
|
by a language or a set of languages. B<This is also untrue.> |
284 |
|
|
The division into blocks exists, but it is almost completely |
285 |
|
|
accidental--an artifact of how the characters have been and |
286 |
|
|
still are allocated. Instead, there is a concept called I<scripts>, which is |
287 |
|
|
more useful: there is C<Latin> script, C<Greek> script, and so on. Scripts |
288 |
|
|
usually span varied parts of several blocks. For more information about |
289 |
|
|
scripts, see L<perlunicode/Scripts>. |
290 |
|
|
|
291 |
|
|
=end original |
292 |
|
|
|
293 |
|
|
もう 1 つの神話は Unicode ブロックに関するものです -- これが何か言語を |
294 |
|
|
取り扱うことがある -- |
295 |
|
|
それぞれのブロックは言語か言語のセットで使われている文字を |
296 |
|
|
定義するということです。 |
297 |
|
|
B<これもまた真実ではありません>。 |
298 |
|
|
ブロックに分割されたものはありますが、それは、ほとんど完全に、 |
299 |
|
|
予想外のものです。 |
300 |
|
|
代わりに、I<用字> (scripts) と呼ばれる、より有益なコンセプトがあります: |
301 |
|
|
C<Latin> 用字と、C<Greek> 用字と、その他のものがあります。 |
302 |
|
|
用字は、ふつう、いくつかのブロックの変えられた部分を測っています。 |
303 |
|
|
詳しくは、L<perlunicode/Scripts> を見て下さい。 |
304 |
|
|
|
305 |
|
|
=begin original |
306 |
|
|
|
307 |
|
|
The Unicode code points are just abstract numbers. To input and |
308 |
|
|
output these abstract numbers, the numbers must be I<encoded> or |
309 |
|
|
I<serialised> somehow. Unicode defines several I<character encoding |
310 |
|
|
forms>, of which I<UTF-8> is perhaps the most popular. UTF-8 is a |
311 |
|
|
variable length encoding that encodes Unicode characters as 1 to 6 |
312 |
|
|
bytes. Other encodings |
313 |
|
|
include UTF-16 and UTF-32 and their big- and little-endian variants |
314 |
|
|
(UTF-8 is byte-order independent) The ISO/IEC 10646 defines the UCS-2 |
315 |
|
|
and UCS-4 encoding forms. |
316 |
|
|
|
317 |
|
|
=end original |
318 |
|
|
|
319 |
|
|
Unicode の符号位置は、抽象的な数字です。 |
320 |
|
|
この抽象的な数字を入出力するために、数字は、I<エンコードされる> 必要が |
321 |
|
|
あるか、I<シリアライズされる> か、どうか、しなければなりません。 |
322 |
|
|
Unicode は、複数の I<character encoding forms> を定義していますが、 |
323 |
|
|
その中で、I<UTF-8> は、たぶん、もっとも有名です。 |
324 |
|
|
UTF-8 は、可変長のエンコーディングで、Unicode 文字を 1 から 6 バイト。 |
325 |
|
|
他のエンコーディングは、UTF-16 と UTF-32 と、それらの大小のエンディアンの |
326 |
|
|
変形(UTF-8 は、バイトオーダーと独立です)を含みます。 |
327 |
|
|
ISO/IEC 10646 は、UCS-2 と UCS-4 の encoding forms を定義しています。 |
328 |
|
|
|
329 |
|
|
=begin original |
330 |
|
|
|
331 |
|
|
For more information about encodings--for instance, to learn what |
332 |
|
|
I<surrogates> and I<byte order marks> (BOMs) are--see L<perlunicode>. |
333 |
|
|
|
334 |
|
|
=end original |
335 |
|
|
|
336 |
|
|
エンコーディングについて -- たとえば、I<surrogates> と |
337 |
|
|
I<byte order marks>(BOMs) -- もっと知りたければ L<perlunicode> を |
338 |
|
|
見て下さい。 |
339 |
|
|
|
340 |
|
|
=head2 Perl's Unicode Support |
341 |
|
|
|
342 |
|
|
(Perl の Unicode サポート) |
343 |
|
|
|
344 |
|
|
=begin original |
345 |
|
|
|
346 |
|
|
Starting from Perl 5.6.0, Perl has had the capacity to handle Unicode |
347 |
|
|
natively. Perl 5.8.0, however, is the first recommended release for |
348 |
|
|
serious Unicode work. The maintenance release 5.6.1 fixed many of the |
349 |
|
|
problems of the initial Unicode implementation, but for example |
350 |
|
|
regular expressions still do not work with Unicode in 5.6.1. |
351 |
|
|
Perl 5.14.0 is the first release where Unicode support is |
352 |
|
|
(almost) seamlessly integrable without some gotchas (the exception being |
353 |
|
|
some differences in L<quotemeta|perlfunc/quotemeta>). To enable this |
354 |
|
|
seamless support, you should C<use feature 'unicode_strings'> (which is |
355 |
|
|
automatically selected if you C<use 5.012> or higher). See L<feature>. |
356 |
|
|
(5.14 also fixes a number of bugs and departures from the Unicode |
357 |
|
|
standard.) |
358 |
|
|
|
359 |
|
|
=end original |
360 |
|
|
|
361 |
|
|
Perl 5.6.0 から、Perl は、Unicode をネイティブに扱う能力を持っていました。 |
362 |
|
|
しかし、Perl 5.8.0 の最初の RC リリースは、重大な Unicode の仕事です。 |
363 |
|
|
メンテナンスリリースの 5.6.1 は、最初の Unicode 実装の多くの問題を |
364 |
|
|
修正しました。 |
365 |
|
|
ですが、たとえば、5.6.1 で、Unicode での正規表現はまだ働きません。 |
366 |
|
|
Perl 5.14.0 is the first release where Unicode support is |
367 |
|
|
(almost) seamlessly integrable without some gotchas (the exception being |
368 |
|
|
some differences in L<quotemeta|perlfunc/quotemeta>). To enable this |
369 |
|
|
seamless support, you should C<use feature 'unicode_strings'> (which is |
370 |
|
|
automatically selected if you C<use 5.012> or higher). See L<feature>. |
371 |
|
|
(5.14 also fixes a number of bugs and departures from the Unicode |
372 |
|
|
standard.) |
373 |
|
|
(TBT) |
374 |
|
|
|
375 |
|
|
=begin original |
376 |
|
|
|
377 |
|
|
Before Perl 5.8.0, the use of C<use utf8> was used to declare |
378 |
|
|
that operations in the current block or file would be Unicode-aware. |
379 |
|
|
This model was found to be wrong, or at least clumsy: the "Unicodeness" |
380 |
|
|
is now carried with the data, instead of being attached to the |
381 |
|
|
operations. |
382 |
|
|
Starting with Perl 5.8.0, only one case remains where an explicit C<use |
383 |
|
|
utf8> is needed: if your Perl script itself is encoded in UTF-8, you can |
384 |
|
|
use UTF-8 in your identifier names, and in string and regular expression |
385 |
|
|
literals, by saying C<use utf8>. This is not the default because |
386 |
|
|
scripts with legacy 8-bit data in them would break. See L<utf8>. |
387 |
|
|
|
388 |
|
|
=end original |
389 |
|
|
|
390 |
|
|
Perl 5.8.0 以前では、から、C<use utf8> の使用は、現在のブロックやファイルの |
391 |
|
|
操作が、Unicode であると明示するのを宣言するために使われました。 |
392 |
|
|
このモデルは間違いが見つけられるか、少なくとも、不格好です: |
393 |
|
|
操作に添付するかわりに、"Unicodeness" は、今や、データに持ち込まれています。 |
394 |
|
|
Perl 5.8.0 から、唯一残されている、明示的に C<use utf8> をする必要がある場所は: |
395 |
|
|
Perl スクリプト自身が UTF-8 でエンコードされていれば、 |
396 |
|
|
識別子の名前、文字の中と、正規表現のリテラルに、UTF-8 を使うことができます。 |
397 |
|
|
これは、デフォルトではありません。 |
398 |
|
|
なぜなら、レガシーの 8-bit データのスクリプトが壊れるからです。 |
399 |
|
|
L<utf8> を参照してください。 |
400 |
|
|
|
401 |
|
|
=head2 Perl's Unicode Model |
402 |
|
|
|
403 |
|
|
(Perl の Unicode モデル) |
404 |
|
|
|
405 |
|
|
=begin original |
406 |
|
|
|
407 |
|
|
Perl supports both pre-5.6 strings of eight-bit native bytes, and |
408 |
|
|
strings of Unicode characters. The general principle is that Perl tries |
409 |
|
|
to keep its data as eight-bit bytes for as long as possible, but as soon |
410 |
|
|
as Unicodeness cannot be avoided, the data is transparently upgraded |
411 |
|
|
to Unicode. Prior to Perl 5.14, the upgrade was not completely |
412 |
|
|
transparent (see L<perlunicode/The "Unicode Bug">), and for backwards |
413 |
|
|
compatibility, full transparency is not gained unless C<use feature |
414 |
|
|
'unicode_strings'> (see L<feature>) or C<use 5.012> (or higher) is |
415 |
|
|
selected. |
416 |
|
|
|
417 |
|
|
=end original |
418 |
|
|
|
419 |
|
|
Perl は、Perl 5.6 より前での 8 ビットネイティブのバイト列の文字列と、 |
420 |
|
|
Unicode 文字の文字列の両方をサポートします。 |
421 |
|
|
一般的な方針は、Perl は、可能な限り長く 8 ビットバイト列としてデータを |
422 |
|
|
保とうとします。 |
423 |
|
|
ですが、Unicode 性が避けられなくなった時点で、 |
424 |
|
|
データは透過的に Unicode に昇格されます。 |
425 |
|
|
Prior to Perl 5.14, the upgrade was not completely |
426 |
|
|
transparent (L<perlunicode/The "Unicode Bug"> を |
427 |
|
|
参照してください), and for backwards |
428 |
|
|
compatibility, full transparency is not gained unless C<use feature |
429 |
|
|
'unicode_strings'> (see L<feature>) or C<use 5.012> (or higher) is |
430 |
|
|
selected. |
431 |
|
|
(TBT) |
432 |
|
|
|
433 |
|
|
=begin original |
434 |
|
|
|
435 |
|
|
Internally, Perl currently uses either whatever the native eight-bit |
436 |
|
|
character set of the platform (for example Latin-1) is, defaulting to |
437 |
|
|
UTF-8, to encode Unicode strings. Specifically, if all code points in |
438 |
|
|
the string are C<0xFF> or less, Perl uses the native eight-bit |
439 |
|
|
character set. Otherwise, it uses UTF-8. |
440 |
|
|
|
441 |
|
|
=end original |
442 |
|
|
|
443 |
|
|
内部的には、Perl は、プラットフォームの 8 ビット文字セット |
444 |
|
|
(例えば、Latin-1)は、デフォルトは UTF-8 ですが、Unicode 文字列に |
445 |
|
|
エンコードします。 |
446 |
|
|
特に、文字列中の、全ての符号位置は、C<0xFF> 以下であれば、 |
447 |
|
|
Perl は、ネイティブの 8 ビットの文字セットを使います。 |
448 |
|
|
そうでなければ、UTF-8 を使います。 |
449 |
|
|
|
450 |
|
|
=begin original |
451 |
|
|
|
452 |
|
|
A user of Perl does not normally need to know nor care how Perl |
453 |
|
|
happens to encode its internal strings, but it becomes relevant when |
454 |
|
|
outputting Unicode strings to a stream without a PerlIO layer (one with |
455 |
|
|
the "default" encoding). In such a case, the raw bytes used internally |
456 |
|
|
(the native character set or UTF-8, as appropriate for each string) |
457 |
|
|
will be used, and a "Wide character" warning will be issued if those |
458 |
|
|
strings contain a character beyond 0x00FF. |
459 |
|
|
|
460 |
|
|
=end original |
461 |
|
|
|
462 |
|
|
Perl のユーザは普通は、Perl がその内部文字列をたまたま、どのように |
463 |
|
|
エンコードするかを、知る必要も、気にする必要もありませんが、Unicode 文字列を |
464 |
|
|
PerlIO 層なしに (「デフォルトの」エンコーディングで) ストリームに |
465 |
|
|
出力しようとすると、関係するようになります。 |
466 |
|
|
このようなケースでは、内部的に使われている生のバイト列(それぞれの |
467 |
|
|
文字列の必要に応じて、ネイティブの文字セットか、UTF-8)が使われます。 |
468 |
|
|
それらの文字列に、0x00FF を超える文字があれば、"Wide character" の警告が |
469 |
|
|
出されます。 |
470 |
|
|
|
471 |
|
|
=begin original |
472 |
|
|
|
473 |
|
|
For example, |
474 |
|
|
|
475 |
|
|
=end original |
476 |
|
|
|
477 |
|
|
たとえば、 |
478 |
|
|
|
479 |
|
|
perl -e 'print "\x{DF}\n", "\x{0100}\x{DF}\n"' |
480 |
|
|
|
481 |
|
|
=begin original |
482 |
|
|
|
483 |
|
|
produces a fairly useless mixture of native bytes and UTF-8, as well |
484 |
|
|
as a warning: |
485 |
|
|
|
486 |
|
|
=end original |
487 |
|
|
|
488 |
|
|
ネイティブのバイト列と UTF-8 の、まったく役に立たない混合もまた、 |
489 |
|
|
同様に警告を出します: |
490 |
|
|
|
491 |
|
|
Wide character in print at ... |
492 |
|
|
|
493 |
|
|
=begin original |
494 |
|
|
|
495 |
|
|
To output UTF-8, use the C<:encoding> or C<:utf8> output layer. Prepending |
496 |
|
|
|
497 |
|
|
=end original |
498 |
|
|
|
499 |
|
|
UTF-8 を出力するために、C<:encoding> か C<:utf8> 出力層を使います。 |
500 |
|
|
先頭にこれを追加することで: |
501 |
|
|
|
502 |
|
|
binmode(STDOUT, ":utf8"); |
503 |
|
|
|
504 |
|
|
=begin original |
505 |
|
|
|
506 |
|
|
to this sample program ensures that the output is completely UTF-8, |
507 |
|
|
and removes the program's warning. |
508 |
|
|
|
509 |
|
|
=end original |
510 |
|
|
|
511 |
|
|
このサンプルプログラムが、出力が完全に UTF-8 であることを保証し、 |
512 |
|
|
プログラムの警告を削除します。 |
513 |
|
|
|
514 |
|
|
=begin original |
515 |
|
|
|
516 |
|
|
You can enable automatic UTF-8-ification of your standard file |
517 |
|
|
handles, default C<open()> layer, and C<@ARGV> by using either |
518 |
|
|
the C<-C> command line switch or the C<PERL_UNICODE> environment |
519 |
|
|
variable, see L<perlrun> for the documentation of the C<-C> switch. |
520 |
|
|
|
521 |
|
|
=end original |
522 |
|
|
|
523 |
|
|
C<-C> コマンドラインスイッチか、C<PERL_UNICODE> 環境変数のどちらか一方を |
524 |
|
|
使うことで、標準ファイルハンドルと、デフォルトの C<open()> 層と、 |
525 |
|
|
C<@ARGV> の UTF-8 化を自動的に有効に出来ます; |
526 |
|
|
L<perlrun> の C<-C> スイッチのドキュメントを見て下さい。 |
527 |
|
|
|
528 |
|
|
=begin original |
529 |
|
|
|
530 |
|
|
Note that this means that Perl expects other software to work the same |
531 |
|
|
way: |
532 |
|
|
if Perl has been led to believe that STDIN should be UTF-8, but then |
533 |
|
|
STDIN coming in from another command is not UTF-8, Perl will likely |
534 |
|
|
complain about the malformed UTF-8. |
535 |
|
|
|
536 |
|
|
=end original |
537 |
|
|
|
538 |
|
|
これは、Perl が他のソフトウェアも同じように動作することを |
539 |
|
|
想定するということを意味することに注意してください: |
540 |
|
|
Perl が STDIN が UTF-8 であるべきと信じるように主導されていて、 |
541 |
|
|
他のコマンドからくる STDIN が UTF-8 でなければ、 |
542 |
|
|
Perl はおそらく不正な UTF-8 であると文句を言います。 |
543 |
|
|
|
544 |
|
|
=begin original |
545 |
|
|
|
546 |
|
|
All features that combine Unicode and I/O also require using the new |
547 |
|
|
PerlIO feature. Almost all Perl 5.8 platforms do use PerlIO, though: |
548 |
|
|
you can see whether yours is by running "perl -V" and looking for |
549 |
|
|
C<useperlio=define>. |
550 |
|
|
|
551 |
|
|
=end original |
552 |
|
|
|
553 |
|
|
Unicode と、I/O の結合の特徴もまた、新しい PerlIO の特徴を使うことを |
554 |
|
|
必要とします。 |
555 |
|
|
ほとんど全ての Perl5.8 プラットフォームは、PerlIO を使います。 |
556 |
|
|
ですが: |
557 |
|
|
"perl -V" を動かして、C<useperlio=define> を見れば、PerlIO を |
558 |
|
|
使っているかどうか、わかります。 |
559 |
|
|
|
560 |
|
|
=head2 Unicode and EBCDIC |
561 |
|
|
|
562 |
|
|
(Unicode と EBCDIC) |
563 |
|
|
|
564 |
|
|
=begin original |
565 |
|
|
|
566 |
|
|
Perl 5.8.0 also supports Unicode on EBCDIC platforms. There, |
567 |
|
|
Unicode support is somewhat more complex to implement since |
568 |
|
|
additional conversions are needed at every step. |
569 |
|
|
|
570 |
|
|
=end original |
571 |
|
|
|
572 |
|
|
Perl 5.8.0 は、EBCDIC プラットフォームでもまた、Unicode をサポートします。 |
573 |
|
|
ここでは、各ステップで追加の変換が必要になるので、Unicode サポートは |
574 |
|
|
実装が若干より複雑になります。 |
575 |
|
|
|
576 |
|
|
=begin original |
577 |
|
|
|
578 |
|
|
Later Perl releases have added code that will not work on EBCDIC platforms, and |
579 |
|
|
no one has complained, so the divergence has continued. If you want to run |
580 |
|
|
Perl on an EBCDIC platform, send email to perlbug@perl.org |
581 |
|
|
|
582 |
|
|
=end original |
583 |
|
|
|
584 |
|
|
後の Perl リリースには EBCDIC プラットフォームで動作しないコードが追加され、 |
585 |
|
|
誰も文句を言わなかったので、相違は続いています。 |
586 |
|
|
もし Perl を EBCDIC プラットフォームで実行したいなら、 |
587 |
|
|
perlbug@perl.org にメールを送ってください。 |
588 |
|
|
|
589 |
|
|
=begin original |
590 |
|
|
|
591 |
|
|
On EBCDIC platforms, the internal Unicode encoding form is UTF-EBCDIC |
592 |
|
|
instead of UTF-8. The difference is that as UTF-8 is "ASCII-safe" in |
593 |
|
|
that ASCII characters encode to UTF-8 as-is, while UTF-EBCDIC is |
594 |
|
|
"EBCDIC-safe". |
595 |
|
|
|
596 |
|
|
=end original |
597 |
|
|
|
598 |
|
|
EBCDIC プラットフォームでは、内部 Unicode エンコーディング形式は |
599 |
|
|
UTF-8 ではなく UTF-EBCDIC です。 |
600 |
|
|
この違いは、ASCII 文字はそのまま UTF-8 にエンコードされるという点において |
601 |
|
|
"ASCII セーフ" ですが、一方 UTF-EBCDIC は "EBCDIC セーフ" です。 |
602 |
|
|
|
603 |
|
|
=head2 Creating Unicode |
604 |
|
|
|
605 |
|
|
(Unicode の作成) |
606 |
|
|
|
607 |
|
|
=begin original |
608 |
|
|
|
609 |
|
|
To create Unicode characters in literals for code points above C<0xFF>, |
610 |
|
|
use the C<\x{...}> notation in double-quoted strings: |
611 |
|
|
|
612 |
|
|
=end original |
613 |
|
|
|
614 |
|
|
C<0xFF> を超える符号位置のリテラルで Unicode 文字を作るためには、 |
615 |
|
|
C<\x{...}> 記法をダブルクォートされた文字列の中で使います。 |
616 |
|
|
|
617 |
|
|
my $smiley = "\x{263a}"; |
618 |
|
|
|
619 |
|
|
=begin original |
620 |
|
|
|
621 |
|
|
Similarly, it can be used in regular expression literals |
622 |
|
|
|
623 |
|
|
=end original |
624 |
|
|
|
625 |
|
|
同様に、正規表現の中でも使えます。 |
626 |
|
|
|
627 |
|
|
$smiley =~ /\x{263a}/; |
628 |
|
|
|
629 |
|
|
=begin original |
630 |
|
|
|
631 |
|
|
At run-time you can use C<chr()>: |
632 |
|
|
|
633 |
|
|
=end original |
634 |
|
|
|
635 |
|
|
C<chr()> を使って、実行時に: |
636 |
|
|
|
637 |
|
|
my $hebrew_alef = chr(0x05d0); |
638 |
|
|
|
639 |
|
|
=begin original |
640 |
|
|
|
641 |
|
|
See L</"Further Resources"> for how to find all these numeric codes. |
642 |
|
|
|
643 |
|
|
=end original |
644 |
|
|
|
645 |
|
|
全てのこれらの数字のコードを見つける方法は、L</"Further Resources"> を |
646 |
|
|
参照してください。 |
647 |
|
|
|
648 |
|
|
=begin original |
649 |
|
|
|
650 |
|
|
Naturally, C<ord()> will do the reverse: it turns a character into |
651 |
|
|
a code point. |
652 |
|
|
|
653 |
|
|
=end original |
654 |
|
|
|
655 |
|
|
当然、C<ord()> は、逆を行います: |
656 |
|
|
文字を符号位置に変えます。 |
657 |
|
|
|
658 |
|
|
=begin original |
659 |
|
|
|
660 |
|
|
Note that C<\x..> (no C<{}> and only two hexadecimal digits), C<\x{...}>, |
661 |
|
|
and C<chr(...)> for arguments less than C<0x100> (decimal 256) |
662 |
|
|
generate an eight-bit character for backward compatibility with older |
663 |
|
|
Perls. For arguments of C<0x100> or more, Unicode characters are |
664 |
|
|
always produced. If you want to force the production of Unicode |
665 |
|
|
characters regardless of the numeric value, use C<pack("U", ...)> |
666 |
|
|
instead of C<\x..>, C<\x{...}>, or C<chr()>. |
667 |
|
|
|
668 |
|
|
=end original |
669 |
|
|
|
670 |
|
|
C<0x100>(10 進の 256)未満の引数の、C<\x..> (C<{}> なしで、 |
671 |
|
|
2 つの 16 進数の数字のみです)と、C<\x{...}> と、C<chr(...)> は、古い |
672 |
|
|
Perl との互換性のために、8 ビットの文字列を生成します。 |
673 |
|
|
C<x100> かそれ以上の引数では、常に Unicode 文字が生成されます。 |
674 |
|
|
何が何でも、数字の値の Unicode 文字を、強制的に生成したければ、 |
675 |
|
|
C<\x..>、C<\x{...}>、C<chr()> の代わりに、C<pack("U", ...)> を使って下さい。 |
676 |
|
|
|
677 |
|
|
=begin original |
678 |
|
|
|
679 |
|
|
You can also use the C<charnames> pragma to invoke characters |
680 |
|
|
by name in double-quoted strings: |
681 |
|
|
|
682 |
|
|
=end original |
683 |
|
|
|
684 |
|
|
ダブルクォートされた文字列内で、名前で文字を呼び出すために、 |
685 |
|
|
C<charnames> プラグマを使うこともできます: |
686 |
|
|
|
687 |
|
|
use charnames ':full'; |
688 |
|
|
my $arabic_alef = "\N{ARABIC LETTER ALEF}"; |
689 |
|
|
|
690 |
|
|
=begin original |
691 |
|
|
|
692 |
|
|
And, as mentioned above, you can also C<pack()> numbers into Unicode |
693 |
|
|
characters: |
694 |
|
|
|
695 |
|
|
=end original |
696 |
|
|
|
697 |
|
|
そして、上述のように、数字を Unicode 文字に、C<pack()> できます: |
698 |
|
|
|
699 |
|
|
my $georgian_an = pack("U", 0x10a0); |
700 |
|
|
|
701 |
|
|
=begin original |
702 |
|
|
|
703 |
|
|
Note that both C<\x{...}> and C<\N{...}> are compile-time string |
704 |
|
|
constants: you cannot use variables in them. if you want similar |
705 |
|
|
run-time functionality, use C<chr()> and C<charnames::string_vianame()>. |
706 |
|
|
|
707 |
|
|
=end original |
708 |
|
|
|
709 |
|
|
C<\x{...}> と、C<\N{...}> の両方は、コンパイル時の文字列定数です: |
710 |
|
|
その中に変数を使うことはできません。 |
711 |
|
|
類似のことを実行時にしたいなら、c<chr()> と、C<charnames::string_vianame()> を |
712 |
|
|
使ってください。 |
713 |
|
|
|
714 |
|
|
=begin original |
715 |
|
|
|
716 |
|
|
If you want to force the result to Unicode characters, use the special |
717 |
|
|
C<"U0"> prefix. It consumes no arguments but causes the following bytes |
718 |
|
|
to be interpreted as the UTF-8 encoding of Unicode characters: |
719 |
|
|
|
720 |
|
|
=end original |
721 |
|
|
|
722 |
|
|
結果を Unicode 文字に強制したいなら、特別な、C<"U0"> 接頭辞を使って下さい。 |
723 |
|
|
これは引数を消費しませんが、引き続くバイト列を Unicode 文字の |
724 |
|
|
UTF-8 エンコーディングとして解釈させます: |
725 |
|
|
|
726 |
|
|
my $chars = pack("U0W*", 0x80, 0x42); |
727 |
|
|
|
728 |
|
|
=begin original |
729 |
|
|
|
730 |
|
|
Likewise, you can stop such UTF-8 interpretation by using the special |
731 |
|
|
C<"C0"> prefix. |
732 |
|
|
|
733 |
|
|
=end original |
734 |
|
|
|
735 |
|
|
同様に、特殊な C<"C0"> 接頭辞を使うことによって、このような UTF-8 の解釈を |
736 |
|
|
停止させることができます。 |
737 |
|
|
|
738 |
|
|
=head2 Handling Unicode |
739 |
|
|
|
740 |
|
|
(Unicode を扱う) |
741 |
|
|
|
742 |
|
|
=begin original |
743 |
|
|
|
744 |
|
|
Handling Unicode is for the most part transparent: just use the |
745 |
|
|
strings as usual. Functions like C<index()>, C<length()>, and |
746 |
|
|
C<substr()> will work on the Unicode characters; regular expressions |
747 |
|
|
will work on the Unicode characters (see L<perlunicode> and L<perlretut>). |
748 |
|
|
|
749 |
|
|
=end original |
750 |
|
|
|
751 |
|
|
Unicode を扱うことは、多くの部分にとって、透過的です: |
752 |
|
|
文字列をいつものように使うだけです。 |
753 |
|
|
C<index()>、C<length()>、C<substr()> のような関数は Unicode 文字列で |
754 |
|
|
動きます; |
755 |
|
|
正規表現も Unicode 文字列で動きます(L<perlunicode> と L<perlretut> を |
756 |
|
|
参照してください)。 |
757 |
|
|
|
758 |
|
|
=begin original |
759 |
|
|
|
760 |
|
|
Note that Perl considers grapheme clusters to be separate characters, so for |
761 |
|
|
example |
762 |
|
|
|
763 |
|
|
=end original |
764 |
|
|
|
765 |
|
|
Perl は書記素クラスタを別々の文字と考えていることに注意して下さい。 |
766 |
|
|
ですので、例えば |
767 |
|
|
|
768 |
|
|
use charnames ':full'; |
769 |
|
|
print length("\N{LATIN CAPITAL LETTER A}\N{COMBINING ACUTE ACCENT}"), "\n"; |
770 |
|
|
|
771 |
|
|
=begin original |
772 |
|
|
|
773 |
|
|
will print 2, not 1. The only exception is that regular expressions |
774 |
|
|
have C<\X> for matching an extended grapheme cluster. (Thus C<\X> in a |
775 |
|
|
regular expression would match the entire sequence of both the example |
776 |
|
|
characters.) |
777 |
|
|
|
778 |
|
|
=end original |
779 |
|
|
|
780 |
|
|
これは 1 ではなく、2 を表示します。 |
781 |
|
|
唯一の例外は、正規表現に拡張書記素クラスタにマッチするために、C<\X> が |
782 |
|
|
ある場合です。 |
783 |
|
|
(Thus C<\X> in a |
784 |
|
|
regular expression would match the entire sequence of both the example |
785 |
|
|
characters.) |
786 |
|
|
(TBT) |
787 |
|
|
|
788 |
|
|
=begin original |
789 |
|
|
|
790 |
|
|
Life is not quite so transparent, however, when working with legacy |
791 |
|
|
encodings, I/O, and certain special cases: |
792 |
|
|
|
793 |
|
|
=end original |
794 |
|
|
|
795 |
|
|
しかし、レガシーエンコーディング、I/O、そしてある種の特殊な状況では、 |
796 |
|
|
人生はそれほど透過的ではありません: |
797 |
|
|
|
798 |
|
|
=head2 Legacy Encodings |
799 |
|
|
|
800 |
|
|
(レガシーエンコーディング) |
801 |
|
|
|
802 |
|
|
=begin original |
803 |
|
|
|
804 |
|
|
When you combine legacy data and Unicode, the legacy data needs |
805 |
|
|
to be upgraded to Unicode. Normally the legacy data is assumed to be |
806 |
|
|
ISO 8859-1 (or EBCDIC, if applicable). |
807 |
|
|
|
808 |
|
|
=end original |
809 |
|
|
|
810 |
|
|
レガシーデータと Unicode とを組み合わせる時は、 |
811 |
|
|
レガシーデータを Unicode に昇格しなければなりません。 |
812 |
|
|
通常、レガシーデータは ISO 8859-1 (か、必要なら EBCDIC)が仮定されます。 |
813 |
|
|
|
814 |
|
|
=begin original |
815 |
|
|
|
816 |
|
|
The C<Encode> module knows about many encodings and has interfaces |
817 |
|
|
for doing conversions between those encodings: |
818 |
|
|
|
819 |
|
|
=end original |
820 |
|
|
|
821 |
|
|
C<Encode> モジュールは多くのエンコーディングを知っており、 |
822 |
|
|
それらのエンコーディングの間の変換をするインターフェースを持っています。 |
823 |
|
|
|
824 |
|
|
use Encode 'decode'; |
825 |
|
|
$data = decode("iso-8859-3", $data); # convert from legacy to utf-8 |
826 |
|
|
|
827 |
|
|
=head2 Unicode I/O |
828 |
|
|
|
829 |
|
|
=begin original |
830 |
|
|
|
831 |
|
|
Normally, writing out Unicode data |
832 |
|
|
|
833 |
|
|
=end original |
834 |
|
|
|
835 |
|
|
通常、Unicode データを書き出すことは、 |
836 |
|
|
|
837 |
|
|
print FH $some_string_with_unicode, "\n"; |
838 |
|
|
|
839 |
|
|
=begin original |
840 |
|
|
|
841 |
|
|
produces raw bytes that Perl happens to use to internally encode the |
842 |
|
|
Unicode string. Perl's internal encoding depends on the system as |
843 |
|
|
well as what characters happen to be in the string at the time. If |
844 |
|
|
any of the characters are at code points C<0x100> or above, you will get |
845 |
|
|
a warning. To ensure that the output is explicitly rendered in the |
846 |
|
|
encoding you desire--and to avoid the warning--open the stream with |
847 |
|
|
the desired encoding. Some examples: |
848 |
|
|
|
849 |
|
|
=end original |
850 |
|
|
|
851 |
|
|
Unicode 文字列を内部的にエンコードするのにたまたまつかわれている、 |
852 |
|
|
生のバイト列を生成することです。 |
853 |
|
|
Perl の内部エンコーディングは、そのときに、文字列にたまたまある文字と |
854 |
|
|
同じように、システム依存です。 |
855 |
|
|
どの文字も、符号位置が C<0x100> 以上なら、警告がでます。 |
856 |
|
|
出力が明示的に、望んでいるエンコーディングにレンダリングされていることを |
857 |
|
|
保証し、警告を避けるために、望んでいるエンコーディングでストリームを |
858 |
|
|
open してください。 |
859 |
|
|
いくつか例示します: |
860 |
|
|
|
861 |
|
|
open FH, ">:utf8", "file"; |
862 |
|
|
|
863 |
|
|
open FH, ">:encoding(ucs2)", "file"; |
864 |
|
|
open FH, ">:encoding(UTF-8)", "file"; |
865 |
|
|
open FH, ">:encoding(shift_jis)", "file"; |
866 |
|
|
|
867 |
|
|
=begin original |
868 |
|
|
|
869 |
|
|
and on already open streams, use C<binmode()>: |
870 |
|
|
|
871 |
|
|
=end original |
872 |
|
|
|
873 |
|
|
すでに開かれているストリームに関しては、C<binmode()> を使います: |
874 |
|
|
|
875 |
|
|
binmode(STDOUT, ":utf8"); |
876 |
|
|
|
877 |
|
|
binmode(STDOUT, ":encoding(ucs2)"); |
878 |
|
|
binmode(STDOUT, ":encoding(UTF-8)"); |
879 |
|
|
binmode(STDOUT, ":encoding(shift_jis)"); |
880 |
|
|
|
881 |
|
|
=begin original |
882 |
|
|
|
883 |
|
|
The matching of encoding names is loose: case does not matter, and |
884 |
|
|
many encodings have several aliases. Note that the C<:utf8> layer |
885 |
|
|
must always be specified exactly like that; it is I<not> subject to |
886 |
|
|
the loose matching of encoding names. Also note that currently C<:utf8> is unsafe for |
887 |
|
|
input, because it accepts the data without validating that it is indeed valid |
888 |
|
|
UTF-8; you should instead use C<:encoding(utf-8)> (with or without a |
889 |
|
|
hyphen). |
890 |
|
|
|
891 |
|
|
=end original |
892 |
|
|
|
893 |
|
|
エンコーディング名のマッチングはルーズです: 大文字小文字は重要ではなく、 |
894 |
|
|
多くのエンコーディングでは、いくつかの別名があります。 |
895 |
|
|
C<:utf8> 層は、常に、きっちりとそのように指定される必要があります。 |
896 |
|
|
このことは、エンコーディング名のルーズなマッチングの対象では I<ありません>。 |
897 |
|
|
また、今のところデータが妥当な UTF-8 であるかどうかを検証せずに受け入れるので、 |
898 |
|
|
入力に対しては C<:utf8> は安全ではないことにも注意してください; |
899 |
argrath |
1.2 |
代わりに C<:encoding(utf-8)> (ハイフンありかなしかどちらか) を使うべきです。 |
900 |
argrath |
1.1 |
|
901 |
|
|
=begin original |
902 |
|
|
|
903 |
|
|
See L<PerlIO> for the C<:utf8> layer, L<PerlIO::encoding> and |
904 |
|
|
L<Encode::PerlIO> for the C<:encoding()> layer, and |
905 |
|
|
L<Encode::Supported> for many encodings supported by the C<Encode> |
906 |
|
|
module. |
907 |
|
|
|
908 |
|
|
=end original |
909 |
|
|
|
910 |
|
|
C<:utf8> 層に関しては、L<PerlIO> を参照してください; |
911 |
|
|
C<:encoding()> に関しては、L<Encode::PerlIO> を参照してください; |
912 |
|
|
C<Encode> モジュールでサポートされている、多くのエンコーディングに関しては、 |
913 |
|
|
L<Encode::Supported> を参照してください。 |
914 |
|
|
|
915 |
|
|
=begin original |
916 |
|
|
|
917 |
|
|
Reading in a file that you know happens to be encoded in one of the |
918 |
|
|
Unicode or legacy encodings does not magically turn the data into |
919 |
|
|
Unicode in Perl's eyes. To do that, specify the appropriate |
920 |
|
|
layer when opening files |
921 |
|
|
|
922 |
|
|
=end original |
923 |
|
|
|
924 |
|
|
Unicode かレガシーのエンコーディングどちらかでたまたまエンコードされている |
925 |
|
|
ファイルを読み込んでも、Perl の目で、魔法のように、データが Unicode に |
926 |
|
|
変わったりしません。 |
927 |
|
|
そうするためには、ファイルを開くときに、適切な層を指定します。 |
928 |
|
|
|
929 |
|
|
open(my $fh,'<:encoding(utf8)', 'anything'); |
930 |
|
|
my $line_of_unicode = <$fh>; |
931 |
|
|
|
932 |
|
|
open(my $fh,'<:encoding(Big5)', 'anything'); |
933 |
|
|
my $line_of_unicode = <$fh>; |
934 |
|
|
|
935 |
|
|
=begin original |
936 |
|
|
|
937 |
|
|
The I/O layers can also be specified more flexibly with |
938 |
|
|
the C<open> pragma. See L<open>, or look at the following example. |
939 |
|
|
|
940 |
|
|
=end original |
941 |
|
|
|
942 |
|
|
I/O 層は、もっと柔軟に、C<open> プラグマでもまた、指定することが出来ます。 |
943 |
|
|
L<open> を参照するか、次の例を見て下さい。 |
944 |
|
|
|
945 |
|
|
use open ':encoding(utf8)'; # input/output default encoding will be |
946 |
|
|
# UTF-8 |
947 |
|
|
open X, ">file"; |
948 |
|
|
print X chr(0x100), "\n"; |
949 |
|
|
close X; |
950 |
|
|
open Y, "<file"; |
951 |
|
|
printf "%#x\n", ord(<Y>); # this should print 0x100 |
952 |
|
|
close Y; |
953 |
|
|
|
954 |
|
|
=begin original |
955 |
|
|
|
956 |
|
|
With the C<open> pragma you can use the C<:locale> layer |
957 |
|
|
|
958 |
|
|
=end original |
959 |
|
|
|
960 |
|
|
C<open> プラグマで、C<:local> 層も使えます: |
961 |
|
|
|
962 |
|
|
BEGIN { $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R' } |
963 |
|
|
# the :locale will probe the locale environment variables like |
964 |
|
|
# LC_ALL |
965 |
|
|
use open OUT => ':locale'; # russki parusski |
966 |
|
|
open(O, ">koi8"); |
967 |
|
|
print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1 |
968 |
|
|
close O; |
969 |
|
|
open(I, "<koi8"); |
970 |
|
|
printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1 |
971 |
|
|
close I; |
972 |
|
|
|
973 |
|
|
=begin original |
974 |
|
|
|
975 |
|
|
These methods install a transparent filter on the I/O stream that |
976 |
|
|
converts data from the specified encoding when it is read in from the |
977 |
|
|
stream. The result is always Unicode. |
978 |
|
|
|
979 |
|
|
=end original |
980 |
|
|
|
981 |
|
|
ストリームからファイルが読まれるときに、特定のエンコーディングから、 |
982 |
|
|
データを変換する I/O ストリーム上の透過的な層です。 |
983 |
|
|
その結果は常に Unicode です。 |
984 |
|
|
|
985 |
|
|
=begin original |
986 |
|
|
|
987 |
|
|
The L<open> pragma affects all the C<open()> calls after the pragma by |
988 |
|
|
setting default layers. If you want to affect only certain |
989 |
|
|
streams, use explicit layers directly in the C<open()> call. |
990 |
|
|
|
991 |
|
|
=end original |
992 |
|
|
|
993 |
|
|
L<open> プラグマは、デフォルトの層を設定することで、プラグマの後の |
994 |
|
|
全ての C<open()> 呼び出しに影響します。 |
995 |
|
|
あるストリームだけに影響を限定したいなら、C<open()> 呼び出しで明示的な |
996 |
|
|
層を使って下さい。 |
997 |
|
|
|
998 |
|
|
=begin original |
999 |
|
|
|
1000 |
|
|
You can switch encodings on an already opened stream by using |
1001 |
|
|
C<binmode()>; see L<perlfunc/binmode>. |
1002 |
|
|
|
1003 |
|
|
=end original |
1004 |
|
|
|
1005 |
|
|
C<binmode()> を使って、すでに開かれているストリームのエンコーディングを |
1006 |
|
|
変えることが出来ます; L<perlfunc/binmode> を参照してください。 |
1007 |
|
|
|
1008 |
|
|
=begin original |
1009 |
|
|
|
1010 |
|
|
The C<:locale> does not currently (as of Perl 5.8.0) work with |
1011 |
|
|
C<open()> and C<binmode()>, only with the C<open> pragma. The |
1012 |
|
|
C<:utf8> and C<:encoding(...)> methods do work with all of C<open()>, |
1013 |
|
|
C<binmode()>, and the C<open> pragma. |
1014 |
|
|
|
1015 |
|
|
=end original |
1016 |
|
|
|
1017 |
|
|
C<:locale> は、現在(Perl 5.8.0 の時点で)、C<open()> と C<binmode()> では |
1018 |
|
|
動きません。 |
1019 |
|
|
C<open> プラグマでのみ動きます。 |
1020 |
|
|
C<:utf8> と C<:encoding(...)> メソッドは、全ての C<open()>、C<binmode()>、 |
1021 |
|
|
C<open> プラグマで動きます。 |
1022 |
|
|
|
1023 |
|
|
=begin original |
1024 |
|
|
|
1025 |
|
|
Similarly, you may use these I/O layers on output streams to |
1026 |
|
|
automatically convert Unicode to the specified encoding when it is |
1027 |
|
|
written to the stream. For example, the following snippet copies the |
1028 |
|
|
contents of the file "text.jis" (encoded as ISO-2022-JP, aka JIS) to |
1029 |
|
|
the file "text.utf8", encoded as UTF-8: |
1030 |
|
|
|
1031 |
|
|
=end original |
1032 |
|
|
|
1033 |
|
|
似たようなものに、ストリームに書き出すときに、自動的に Unicode を |
1034 |
|
|
特定のエンコーディングに変換する、出力ストリームの I/O 層を使うかも |
1035 |
|
|
知れません。 |
1036 |
|
|
例えば、次のようなコードの断片は、(ISO-2022-JP, またの名を JIS として |
1037 |
|
|
エンコードされている)" text.jis" ファイルの内容を、UTF-8 として |
1038 |
|
|
エンコードされる "text.utf8" ファイルにコピーします。 |
1039 |
|
|
|
1040 |
|
|
open(my $nihongo, '<:encoding(iso-2022-jp)', 'text.jis'); |
1041 |
|
|
open(my $unicode, '>:utf8', 'text.utf8'); |
1042 |
|
|
while (<$nihongo>) { print $unicode $_ } |
1043 |
|
|
|
1044 |
|
|
=begin original |
1045 |
|
|
|
1046 |
|
|
The naming of encodings, both by the C<open()> and by the C<open> |
1047 |
|
|
pragma allows for flexible names: C<koi8-r> and C<KOI8R> will both be |
1048 |
|
|
understood. |
1049 |
|
|
|
1050 |
|
|
=end original |
1051 |
|
|
|
1052 |
|
|
C<open()> と、C<open> プラグマの両方で使われているエンコーディングの |
1053 |
|
|
名前は、フレキシブルな名前でも使えます: C<koi8-r>と、C<KOI8R> は、 |
1054 |
|
|
両方とも理解されます。 |
1055 |
|
|
|
1056 |
|
|
=begin original |
1057 |
|
|
|
1058 |
|
|
Common encodings recognized by ISO, MIME, IANA, and various other |
1059 |
|
|
standardisation organisations are recognised; for a more detailed |
1060 |
|
|
list see L<Encode::Supported>. |
1061 |
|
|
|
1062 |
|
|
=end original |
1063 |
|
|
|
1064 |
|
|
ISO、MIME、IANA、様々な他の標準化機関が認識している、一般的な |
1065 |
|
|
エンコーディングが認識されます; より詳細なリストは、 |
1066 |
|
|
L<Encode::Supported> を参照してください。 |
1067 |
|
|
|
1068 |
|
|
=begin original |
1069 |
|
|
|
1070 |
|
|
C<read()> reads characters and returns the number of characters. |
1071 |
|
|
C<seek()> and C<tell()> operate on byte counts, as do C<sysread()> |
1072 |
|
|
and C<sysseek()>. |
1073 |
|
|
|
1074 |
|
|
=end original |
1075 |
|
|
|
1076 |
|
|
C<read()> は、文字を読み、文字の数を返します。 |
1077 |
|
|
C<seek()> と C<tell()> は、バイトカウントに関して操作します。 |
1078 |
|
|
C<sysread> と C<sysseek()> も同様です。 |
1079 |
|
|
|
1080 |
|
|
=begin original |
1081 |
|
|
|
1082 |
|
|
Notice that because of the default behaviour of not doing any |
1083 |
|
|
conversion upon input if there is no default layer, |
1084 |
|
|
it is easy to mistakenly write code that keeps on expanding a file |
1085 |
|
|
by repeatedly encoding the data: |
1086 |
|
|
|
1087 |
|
|
=end original |
1088 |
|
|
|
1089 |
|
|
デフォルト層がなければ、入力になんの変換もしないのがデフォルトの |
1090 |
|
|
振る舞いなので、繰り返しデータをエンコーディングすることで、 |
1091 |
|
|
ファイルを展開し続けるコードを誤って書きやすいです。 |
1092 |
|
|
|
1093 |
|
|
# BAD CODE WARNING |
1094 |
|
|
open F, "file"; |
1095 |
|
|
local $/; ## read in the whole file of 8-bit characters |
1096 |
|
|
$t = <F>; |
1097 |
|
|
close F; |
1098 |
|
|
open F, ">:encoding(utf8)", "file"; |
1099 |
|
|
print F $t; ## convert to UTF-8 on output |
1100 |
|
|
close F; |
1101 |
|
|
|
1102 |
|
|
=begin original |
1103 |
|
|
|
1104 |
|
|
If you run this code twice, the contents of the F<file> will be twice |
1105 |
|
|
UTF-8 encoded. A C<use open ':encoding(utf8)'> would have avoided the |
1106 |
|
|
bug, or explicitly opening also the F<file> for input as UTF-8. |
1107 |
|
|
|
1108 |
|
|
=end original |
1109 |
|
|
|
1110 |
|
|
このコードを 2 回実行すると、F<file> の内容は、UTF-8 に、2 回 |
1111 |
|
|
エンコードされます。 |
1112 |
|
|
C<use open 'encoding(utf8)'> は、バグを避けるでしょう; |
1113 |
|
|
もしくは、UTF-8 として入力するために、F<File> を、明示的に開くことです。 |
1114 |
|
|
|
1115 |
|
|
=begin original |
1116 |
|
|
|
1117 |
|
|
B<NOTE>: the C<:utf8> and C<:encoding> features work only if your |
1118 |
|
|
Perl has been built with the new PerlIO feature (which is the default |
1119 |
|
|
on most systems). |
1120 |
|
|
|
1121 |
|
|
=end original |
1122 |
|
|
|
1123 |
|
|
B<注意>: C<:utf8> と C<:encoding> の機能は、Perl が新しい PerlIO の機能で |
1124 |
|
|
ビルドされている場合(ほとんどのシステムで、それがデフォルトです)にのみ |
1125 |
|
|
動作します。 |
1126 |
|
|
|
1127 |
|
|
=head2 Displaying Unicode As Text |
1128 |
|
|
|
1129 |
|
|
(Unicode をテキストとして表示する) |
1130 |
|
|
|
1131 |
|
|
=begin original |
1132 |
|
|
|
1133 |
|
|
Sometimes you might want to display Perl scalars containing Unicode as |
1134 |
|
|
simple ASCII (or EBCDIC) text. The following subroutine converts |
1135 |
|
|
its argument so that Unicode characters with code points greater than |
1136 |
|
|
255 are displayed as C<\x{...}>, control characters (like C<\n>) are |
1137 |
|
|
displayed as C<\x..>, and the rest of the characters as themselves: |
1138 |
|
|
|
1139 |
|
|
=end original |
1140 |
|
|
|
1141 |
|
|
Unicode を含んでいる Perl のスカラを、単純な ASCII(か、EBCDIC)の |
1142 |
|
|
テキストとして、表示したいかもしれません。 |
1143 |
|
|
下のサブルーチンは、引数を変換して、255 より大きい符号位置の |
1144 |
|
|
Unicode 文字を、C<\x{...}> として表示し、(C<\n> のような)制御文字は、 |
1145 |
|
|
C<\x..> として表示します。 |
1146 |
|
|
残りの文字は、そのまま表示します: |
1147 |
|
|
|
1148 |
|
|
sub nice_string { |
1149 |
|
|
join("", |
1150 |
|
|
map { $_ > 255 ? # if wide character... |
1151 |
|
|
sprintf("\\x{%04X}", $_) : # \x{...} |
1152 |
|
|
chr($_) =~ /[[:cntrl:]]/ ? # else if control character ... |
1153 |
|
|
sprintf("\\x%02X", $_) : # \x.. |
1154 |
|
|
quotemeta(chr($_)) # else quoted or as themselves |
1155 |
|
|
} unpack("W*", $_[0])); # unpack Unicode characters |
1156 |
|
|
} |
1157 |
|
|
|
1158 |
|
|
=begin original |
1159 |
|
|
|
1160 |
|
|
For example, |
1161 |
|
|
|
1162 |
|
|
=end original |
1163 |
|
|
|
1164 |
|
|
たとえば、 |
1165 |
|
|
|
1166 |
|
|
nice_string("foo\x{100}bar\n") |
1167 |
|
|
|
1168 |
|
|
=begin original |
1169 |
|
|
|
1170 |
|
|
returns the string |
1171 |
|
|
|
1172 |
|
|
=end original |
1173 |
|
|
|
1174 |
|
|
は、次のような文字列になり: |
1175 |
|
|
|
1176 |
|
|
'foo\x{0100}bar\x0A' |
1177 |
|
|
|
1178 |
|
|
=begin original |
1179 |
|
|
|
1180 |
|
|
which is ready to be printed. |
1181 |
|
|
|
1182 |
|
|
=end original |
1183 |
|
|
|
1184 |
|
|
表示可能になります。 |
1185 |
|
|
|
1186 |
|
|
=head2 Special Cases |
1187 |
|
|
|
1188 |
|
|
(特殊な状況) |
1189 |
|
|
|
1190 |
|
|
=over 4 |
1191 |
|
|
|
1192 |
|
|
=item * |
1193 |
|
|
|
1194 |
|
|
=begin original |
1195 |
|
|
|
1196 |
|
|
Bit Complement Operator ~ And vec() |
1197 |
|
|
|
1198 |
|
|
=end original |
1199 |
|
|
|
1200 |
|
|
演算子 ~ と、vec() についてのちょっとした補足 |
1201 |
|
|
|
1202 |
|
|
=begin original |
1203 |
|
|
|
1204 |
|
|
The bit complement operator C<~> may produce surprising results if |
1205 |
|
|
used on strings containing characters with ordinal values above |
1206 |
|
|
255. In such a case, the results are consistent with the internal |
1207 |
|
|
encoding of the characters, but not with much else. So don't do |
1208 |
|
|
that. Similarly for C<vec()>: you will be operating on the |
1209 |
|
|
internally-encoded bit patterns of the Unicode characters, not on |
1210 |
|
|
the code point values, which is very probably not what you want. |
1211 |
|
|
|
1212 |
|
|
=end original |
1213 |
|
|
|
1214 |
|
|
演算子 C<~> は、255 を超える序数の値の文字を含んでいる文字列で、 |
1215 |
|
|
使われると、驚くべき結果になるでしょう。 |
1216 |
|
|
そのようなケースでは、その結果は文字の内部的なエンコーディングで一貫性が |
1217 |
|
|
あります。 |
1218 |
|
|
しかし、他の多くでは違います。 |
1219 |
|
|
ですので、そのようなことはしないでください。 |
1220 |
|
|
C<vec()> も同様です: 符号位置の値ではなく、内部的にエンコードされた、 |
1221 |
|
|
Unicode 文字列のビットパターンを操作することになります; |
1222 |
|
|
おそらく、それは望んだことではないでしょう。 |
1223 |
|
|
|
1224 |
|
|
=item * |
1225 |
|
|
|
1226 |
|
|
=begin original |
1227 |
|
|
|
1228 |
|
|
Peeking At Perl's Internal Encoding |
1229 |
|
|
|
1230 |
|
|
=end original |
1231 |
|
|
|
1232 |
|
|
Perl の内部エンコーディングをのぞき見る |
1233 |
|
|
|
1234 |
|
|
=begin original |
1235 |
|
|
|
1236 |
|
|
Normal users of Perl should never care how Perl encodes any particular |
1237 |
|
|
Unicode string (because the normal ways to get at the contents of a |
1238 |
|
|
string with Unicode--via input and output--should always be via |
1239 |
|
|
explicitly-defined I/O layers). But if you must, there are two |
1240 |
|
|
ways of looking behind the scenes. |
1241 |
|
|
|
1242 |
|
|
=end original |
1243 |
|
|
|
1244 |
|
|
Perl がどのように、特定の Unicode 文字列をエンコードしているかを、 |
1245 |
|
|
Perl の普通のユーザは気にするべきではありません |
1246 |
|
|
(Unicode で文字列の内容に達する普通の方法 -- 入出力によって -- は、常に |
1247 |
|
|
明示的に定義された I/O 層を経由すべきだからです)。 |
1248 |
|
|
ですが、もし必要なら、隠れている裏側を見る 2 つの方法があります。 |
1249 |
|
|
|
1250 |
|
|
=begin original |
1251 |
|
|
|
1252 |
|
|
One way of peeking inside the internal encoding of Unicode characters |
1253 |
|
|
is to use C<unpack("C*", ...> to get the bytes of whatever the string |
1254 |
|
|
encoding happens to be, or C<unpack("U0..", ...)> to get the bytes of the |
1255 |
|
|
UTF-8 encoding: |
1256 |
|
|
|
1257 |
|
|
=end original |
1258 |
|
|
|
1259 |
|
|
Unicode 文字の内部エンコーディングの内側を覗き見る方法の 1 つは、 |
1260 |
|
|
エンコーディングが何であろうとも文字列のバイト列を得るために |
1261 |
|
|
C<unpack("C*", ...> を使うか、UTF-8 エンコーディングのバイト列を得るために |
1262 |
|
|
C<unpack("U0..", ...)> を使うことです: |
1263 |
|
|
|
1264 |
|
|
# this prints c4 80 for the UTF-8 bytes 0xc4 0x80 |
1265 |
|
|
print join(" ", unpack("U0(H2)*", pack("U", 0x100))), "\n"; |
1266 |
|
|
|
1267 |
|
|
=begin original |
1268 |
|
|
|
1269 |
|
|
Yet another way would be to use the Devel::Peek module: |
1270 |
|
|
|
1271 |
|
|
=end original |
1272 |
|
|
|
1273 |
|
|
もう一つの方法は、Dvel::Peek モジュールを使うことです: |
1274 |
|
|
|
1275 |
|
|
perl -MDevel::Peek -e 'Dump(chr(0x100))' |
1276 |
|
|
|
1277 |
|
|
=begin original |
1278 |
|
|
|
1279 |
|
|
That shows the C<UTF8> flag in FLAGS and both the UTF-8 bytes |
1280 |
|
|
and Unicode characters in C<PV>. See also later in this document |
1281 |
|
|
the discussion about the C<utf8::is_utf8()> function. |
1282 |
|
|
|
1283 |
|
|
=end original |
1284 |
|
|
|
1285 |
|
|
これは、FLAGS の C<UTF8> フラグと、UTF-8 バイトと、C<PV> の中の |
1286 |
|
|
Unicode 文字の両方を見せます。 |
1287 |
|
|
このドキュメントの後にある、C<utf8::is_utf8()> 機能についての議論も |
1288 |
|
|
参照してください。 |
1289 |
|
|
|
1290 |
|
|
=back |
1291 |
|
|
|
1292 |
|
|
=head2 Advanced Topics |
1293 |
|
|
|
1294 |
|
|
(発展した話題) |
1295 |
|
|
|
1296 |
|
|
=over 4 |
1297 |
|
|
|
1298 |
|
|
=item * |
1299 |
|
|
|
1300 |
|
|
=begin original |
1301 |
|
|
|
1302 |
|
|
String Equivalence |
1303 |
|
|
|
1304 |
|
|
=end original |
1305 |
|
|
|
1306 |
|
|
文字列の等価性 |
1307 |
|
|
|
1308 |
|
|
=begin original |
1309 |
|
|
|
1310 |
|
|
The question of string equivalence turns somewhat complicated |
1311 |
|
|
in Unicode: what do you mean by "equal"? |
1312 |
|
|
|
1313 |
|
|
=end original |
1314 |
|
|
|
1315 |
|
|
文字列の等価性の疑問は、Unicode でいくぶん複雑になります。 |
1316 |
|
|
"等価" で、何を意味していますか? |
1317 |
|
|
|
1318 |
|
|
=begin original |
1319 |
|
|
|
1320 |
|
|
(Is C<LATIN CAPITAL LETTER A WITH ACUTE> equal to |
1321 |
|
|
C<LATIN CAPITAL LETTER A>?) |
1322 |
|
|
|
1323 |
|
|
=end original |
1324 |
|
|
|
1325 |
|
|
(IS C<LATIN CAPITAL LETTER A WITH ACCUTE> と |
1326 |
|
|
C<LATIN CAPITAL LETTER A>は、等しいでしょうか?) |
1327 |
|
|
|
1328 |
|
|
=begin original |
1329 |
|
|
|
1330 |
|
|
The short answer is that by default Perl compares equivalence (C<eq>, |
1331 |
|
|
C<ne>) based only on code points of the characters. In the above |
1332 |
|
|
case, the answer is no (because 0x00C1 != 0x0041). But sometimes, any |
1333 |
|
|
CAPITAL LETTER A's should be considered equal, or even A's of any case. |
1334 |
|
|
|
1335 |
|
|
=end original |
1336 |
|
|
|
1337 |
|
|
短く答えれば次のようになります。 |
1338 |
|
|
デフォルトでは、Perl は(C<eq> と、C<ne> で)等価性を |
1339 |
|
|
比較しますが、これらは、文字の符号位置でのみに基づいています。 |
1340 |
|
|
上のケースでは、答えはいいえです(0x00C1 != 0x0041 ですから)。 |
1341 |
|
|
しかし、どんな CAPITAL LETTER A も等しいと考えるべき時や、大文字小文字に |
1342 |
|
|
関わらずどんな A も等しいと考えるべき時もあります。 |
1343 |
|
|
|
1344 |
|
|
=begin original |
1345 |
|
|
|
1346 |
|
|
The long answer is that you need to consider character normalization |
1347 |
|
|
and casing issues: see L<Unicode::Normalize>, Unicode Technical Report #15, |
1348 |
|
|
L<Unicode Normalization Forms|http://www.unicode.org/unicode/reports/tr15> and |
1349 |
|
|
sections on case mapping in the L<Unicode Standard|http://www.unicode.org>. |
1350 |
|
|
|
1351 |
|
|
=end original |
1352 |
|
|
|
1353 |
|
|
長く答えるなら、文字の正規化と大文字小文字の問題を考える必要があります: |
1354 |
|
|
L<Unicode::Normalize>、Unicode テクニカルレポート #15 |
1355 |
|
|
L<Unicode Normalization Forms|http://www.unicode.org/unicode/reports/tr15> と |
1356 |
|
|
L<Unicode Standard|http://www.unicode.org> の大文字小文字マッピングの |
1357 |
|
|
章を参照してください。 |
1358 |
|
|
|
1359 |
|
|
=begin original |
1360 |
|
|
|
1361 |
|
|
As of Perl 5.8.0, the "Full" case-folding of I<Case |
1362 |
|
|
Mappings/SpecialCasing> is implemented, but bugs remain in C<qr//i> with them, |
1363 |
|
|
mostly fixed by 5.14. |
1364 |
|
|
|
1365 |
|
|
=end original |
1366 |
|
|
|
1367 |
|
|
Perl 5.8.0 から、I<Case Mappings/SpecialCasing> の、 |
1368 |
|
|
「完全な」大文字小文字の畳み込みが実装されていますが、C<qr//i> にバグが |
1369 |
|
|
残っています; ほとんどは 5.14 で修正されました。 |
1370 |
|
|
|
1371 |
|
|
=item * |
1372 |
|
|
|
1373 |
|
|
=begin original |
1374 |
|
|
|
1375 |
|
|
String Collation |
1376 |
|
|
|
1377 |
|
|
=end original |
1378 |
|
|
|
1379 |
|
|
文字列の照合(Collation) |
1380 |
|
|
|
1381 |
|
|
=begin original |
1382 |
|
|
|
1383 |
|
|
People like to see their strings nicely sorted--or as Unicode |
1384 |
|
|
parlance goes, collated. But again, what do you mean by collate? |
1385 |
|
|
|
1386 |
|
|
=end original |
1387 |
|
|
|
1388 |
|
|
文字列がうまくソートされている(あるいは Unicode の業界用語を使えば、 |
1389 |
|
|
照合されている(collated))のを好みます。 |
1390 |
|
|
ですが、再び、照合で何を意味していますか? |
1391 |
|
|
|
1392 |
|
|
=begin original |
1393 |
|
|
|
1394 |
|
|
(Does C<LATIN CAPITAL LETTER A WITH ACUTE> come before or after |
1395 |
|
|
C<LATIN CAPITAL LETTER A WITH GRAVE>?) |
1396 |
|
|
|
1397 |
|
|
=end original |
1398 |
|
|
|
1399 |
|
|
(C<LATIN CAPITAL LETTER A WITH ACUTE> は、 |
1400 |
|
|
C<LATIN CAPITAL LETTER A WITH GRAVE> より、 |
1401 |
|
|
前でしょうか後でしょうか?) |
1402 |
|
|
|
1403 |
|
|
=begin original |
1404 |
|
|
|
1405 |
|
|
The short answer is that by default, Perl compares strings (C<lt>, |
1406 |
|
|
C<le>, C<cmp>, C<ge>, C<gt>) based only on the code points of the |
1407 |
|
|
characters. In the above case, the answer is "after", since |
1408 |
|
|
C<0x00C1> > C<0x00C0>. |
1409 |
|
|
|
1410 |
|
|
=end original |
1411 |
|
|
|
1412 |
|
|
短く答えれば次のようになります。 |
1413 |
|
|
Perl は、文字列を、(C<lt>、C<le>、C<cmp>、C<ge>、C<gt>)で比較しますが、 |
1414 |
|
|
これらは、文字の符号位置でのみに基づいています。 |
1415 |
|
|
上のケースでは、答えは、C<0x00C1> > C<0x00C0> ですので、"後"になります。 |
1416 |
|
|
|
1417 |
|
|
=begin original |
1418 |
|
|
|
1419 |
|
|
The long answer is that "it depends", and a good answer cannot be |
1420 |
|
|
given without knowing (at the very least) the language context. |
1421 |
|
|
See L<Unicode::Collate>, and I<Unicode Collation Algorithm> |
1422 |
|
|
L<http://www.unicode.org/unicode/reports/tr10/> |
1423 |
|
|
|
1424 |
|
|
=end original |
1425 |
|
|
|
1426 |
|
|
長く答えるなら、「場合によります」; |
1427 |
|
|
良い答えは、(とても少なくとも)言語のコンテキストを知らずには、 |
1428 |
|
|
与えられません。 |
1429 |
|
|
L<Unicode::Collate>, I<Unicode Collation Algorithm> |
1430 |
|
|
L<http://www.unicode.org/unicode/reports/tr10/> を参照してください。 |
1431 |
|
|
|
1432 |
|
|
=back |
1433 |
|
|
|
1434 |
|
|
=head2 Miscellaneous |
1435 |
|
|
|
1436 |
|
|
(その他) |
1437 |
|
|
|
1438 |
|
|
=over 4 |
1439 |
|
|
|
1440 |
|
|
=item * |
1441 |
|
|
|
1442 |
|
|
=begin original |
1443 |
|
|
|
1444 |
|
|
Character Ranges and Classes |
1445 |
|
|
|
1446 |
|
|
=end original |
1447 |
|
|
|
1448 |
|
|
文字の範囲とクラス |
1449 |
|
|
|
1450 |
|
|
=begin original |
1451 |
|
|
|
1452 |
|
|
Character ranges in regular expression bracketed character classes ( e.g., |
1453 |
|
|
C</[a-z]/>) and in the C<tr///> (also known as C<y///>) operator are not |
1454 |
|
|
magically Unicode-aware. What this means is that C<[A-Za-z]> will not |
1455 |
|
|
magically start to mean "all alphabetic letters" (not that it does mean that |
1456 |
|
|
even for 8-bit characters; for those, if you are using locales (L<perllocale>), |
1457 |
|
|
use C</[[:alpha:]]/>; and if not, use the 8-bit-aware property C<\p{alpha}>). |
1458 |
|
|
|
1459 |
|
|
=end original |
1460 |
|
|
|
1461 |
|
|
正規表現の大かっこ文字クラス(C</[a-z]/>)内と、C<tr///>(C<y///> としても |
1462 |
|
|
知られる)演算子内では、自動的には Unicode 対応にはなりません。 |
1463 |
|
|
これが意味することは、C<[A-Za-z]> は、自動的に "全てのアルファベット文字"を |
1464 |
|
|
意味するようにはなりません(8 ビットの文字であることすらも意味しません; |
1465 |
argrath |
1.2 |
そのためには、ロケール (L<perllocale>) を使っているなら、C</[[:alpha:]]/> を |
1466 |
|
|
使ってください; そうでなければ、8 ビットを認識する特性 C<\p{alpha}> を |
1467 |
|
|
使ってください)。 |
1468 |
argrath |
1.1 |
|
1469 |
|
|
=begin original |
1470 |
|
|
|
1471 |
|
|
All the properties that begin with C<\p> (and its inverse C<\P>) are actually |
1472 |
|
|
character classes that are Unicode-aware. There are dozens of them, see |
1473 |
|
|
L<perluniprops>. |
1474 |
|
|
|
1475 |
|
|
=end original |
1476 |
|
|
|
1477 |
|
|
All the properties that begin with C<\p> (and its inverse C<\P>) are actually |
1478 |
|
|
character classes that are Unicode-aware. There are dozens of them, see |
1479 |
|
|
L<perluniprops>. |
1480 |
|
|
(TBT) |
1481 |
|
|
|
1482 |
|
|
=begin original |
1483 |
|
|
|
1484 |
|
|
You can use Unicode code points as the end points of character ranges, and the |
1485 |
|
|
range will include all Unicode code points that lie between those end points. |
1486 |
|
|
|
1487 |
|
|
=end original |
1488 |
|
|
|
1489 |
argrath |
1.2 |
文字の範囲の端の位置として、Unicode の符号位置を使うことができ、幅はこれらの |
1490 |
|
|
両端の位置の間の Unicode 符号位置全てを含みます。 |
1491 |
argrath |
1.1 |
|
1492 |
|
|
=item * |
1493 |
|
|
|
1494 |
|
|
=begin original |
1495 |
|
|
|
1496 |
|
|
String-To-Number Conversions |
1497 |
|
|
|
1498 |
|
|
=end original |
1499 |
|
|
|
1500 |
|
|
文字列から数字への変換 |
1501 |
|
|
|
1502 |
|
|
=begin original |
1503 |
|
|
|
1504 |
|
|
Unicode does define several other decimal--and numeric--characters |
1505 |
|
|
besides the familiar 0 to 9, such as the Arabic and Indic digits. |
1506 |
|
|
Perl does not support string-to-number conversion for digits other |
1507 |
|
|
than ASCII 0 to 9 (and ASCII a to f for hexadecimal). |
1508 |
|
|
To get safe conversions from any Unicode string, use |
1509 |
|
|
L<Unicode::UCD/num()>. |
1510 |
|
|
|
1511 |
|
|
=end original |
1512 |
|
|
|
1513 |
|
|
Unicode は、なじみのある、アラビアやインドの数字のような 0 から 9 に加えて、 |
1514 |
|
|
複数の他の 10 進数--と数字の--文字を、定義します。 |
1515 |
|
|
Perl は、ASCII の 0 から 9(と、16 進法の ASCII の a から f)以外の数字の、 |
1516 |
|
|
文字列から数への変換をサポートしません。 |
1517 |
argrath |
1.2 |
任意の Unicode 文字列からの安全な変換を行うには、L<Unicode::UCD/num()> を |
1518 |
|
|
使ってください。 |
1519 |
argrath |
1.1 |
|
1520 |
|
|
=back |
1521 |
|
|
|
1522 |
|
|
=head2 Questions With Answers |
1523 |
|
|
|
1524 |
|
|
(質問と回答) |
1525 |
|
|
|
1526 |
|
|
=over 4 |
1527 |
|
|
|
1528 |
|
|
=item * |
1529 |
|
|
|
1530 |
|
|
=begin original |
1531 |
|
|
|
1532 |
|
|
Will My Old Scripts Break? |
1533 |
|
|
|
1534 |
|
|
=end original |
1535 |
|
|
|
1536 |
|
|
古いスクリプトは壊れるでしょうか? |
1537 |
|
|
|
1538 |
|
|
=begin original |
1539 |
|
|
|
1540 |
|
|
Very probably not. Unless you are generating Unicode characters |
1541 |
|
|
somehow, old behaviour should be preserved. About the only behaviour |
1542 |
|
|
that has changed and which could start generating Unicode is the old |
1543 |
|
|
behaviour of C<chr()> where supplying an argument more than 255 |
1544 |
|
|
produced a character modulo 255. C<chr(300)>, for example, was equal |
1545 |
|
|
to C<chr(45)> or "-" (in ASCII), now it is LATIN CAPITAL LETTER I WITH |
1546 |
|
|
BREVE. |
1547 |
|
|
|
1548 |
|
|
=end original |
1549 |
|
|
|
1550 |
|
|
たぶん、壊れません。 |
1551 |
|
|
どうにかして、Unicode 文字を生成していなければ、古い挙動は保護されます。 |
1552 |
|
|
振る舞いが変更され、Unicode を生成し始める唯一のものは、古い C<chr()> は |
1553 |
|
|
引数として 255 を超える値が指定されると、255 を法とした文字が生成される |
1554 |
|
|
というものです。 |
1555 |
|
|
例えば、C<chr(300)> は、C<chr(45)> あるいは (ASCII の)"-" と同じでした。 |
1556 |
|
|
現在ではそれは、LATIN CAPITAL LETTER I WITH BREVE です。 |
1557 |
|
|
|
1558 |
|
|
=item * |
1559 |
|
|
|
1560 |
|
|
=begin original |
1561 |
|
|
|
1562 |
|
|
How Do I Make My Scripts Work With Unicode? |
1563 |
|
|
|
1564 |
|
|
=end original |
1565 |
|
|
|
1566 |
|
|
私のスクリプトを Unicode で動かすには? |
1567 |
|
|
|
1568 |
|
|
=begin original |
1569 |
|
|
|
1570 |
|
|
Very little work should be needed since nothing changes until you |
1571 |
|
|
generate Unicode data. The most important thing is getting input as |
1572 |
|
|
Unicode; for that, see the earlier I/O discussion. |
1573 |
|
|
To get full seamless Unicode support, add |
1574 |
|
|
C<use feature 'unicode_strings'> (or C<use 5.012> or higher) to your |
1575 |
|
|
script. |
1576 |
|
|
|
1577 |
|
|
=end original |
1578 |
|
|
|
1579 |
|
|
Unicode データを生成するまで、ほとんど何もする必要はありません。 |
1580 |
|
|
もっとも重要なものは、Unicode として、入力を得ることです; |
1581 |
|
|
なので、以前の I/O の議論を見て下さい。 |
1582 |
|
|
To get full seamless Unicode support, add |
1583 |
|
|
C<use feature 'unicode_strings'> (or C<use 5.012> or higher) to your |
1584 |
|
|
script. |
1585 |
|
|
(TBT) |
1586 |
|
|
|
1587 |
|
|
=item * |
1588 |
|
|
|
1589 |
|
|
=begin original |
1590 |
|
|
|
1591 |
|
|
How Do I Know Whether My String Is In Unicode? |
1592 |
|
|
|
1593 |
|
|
=end original |
1594 |
|
|
|
1595 |
|
|
Unicode の文字列かどうかを知るには? |
1596 |
|
|
|
1597 |
|
|
=begin original |
1598 |
|
|
|
1599 |
|
|
You shouldn't have to care. But you may if your Perl is before 5.14.0 |
1600 |
|
|
or you haven't specified C<use feature 'unicode_strings'> or C<use |
1601 |
|
|
5.012> (or higher) because otherwise the semantics of the code points |
1602 |
|
|
in the range 128 to 255 are different depending on |
1603 |
|
|
whether the string they are contained within is in Unicode or not. |
1604 |
|
|
(See L<perlunicode/When Unicode Does Not Happen>.) |
1605 |
|
|
|
1606 |
|
|
=end original |
1607 |
|
|
|
1608 |
|
|
気にする必要はないはずです。 |
1609 |
|
|
しかし、if your Perl is before 5.14.0 |
1610 |
|
|
or you haven't specified C<use feature 'unicode_strings'> or C<use |
1611 |
|
|
5.012> (or higher) |
1612 |
|
|
今のところ範囲 128 から 256 の符号位置の意味論は、文字列が Unicode で |
1613 |
|
|
保管されているかどうかによって異なるため、知りたいかもしれません。 |
1614 |
|
|
(L<perlunicode/When Unicode Does Not Happen> を参照してください。) |
1615 |
|
|
(TBT) |
1616 |
|
|
|
1617 |
|
|
=begin original |
1618 |
|
|
|
1619 |
|
|
To determine if a string is in Unicode, use: |
1620 |
|
|
|
1621 |
|
|
=end original |
1622 |
|
|
|
1623 |
|
|
文字列が Unicode かどうかを決定するには、以下を使います: |
1624 |
|
|
|
1625 |
|
|
print utf8::is_utf8($string) ? 1 : 0, "\n"; |
1626 |
|
|
|
1627 |
|
|
=begin original |
1628 |
|
|
|
1629 |
|
|
But note that this doesn't mean that any of the characters in the |
1630 |
|
|
string are necessary UTF-8 encoded, or that any of the characters have |
1631 |
|
|
code points greater than 0xFF (255) or even 0x80 (128), or that the |
1632 |
|
|
string has any characters at all. All the C<is_utf8()> does is to |
1633 |
|
|
return the value of the internal "utf8ness" flag attached to the |
1634 |
|
|
C<$string>. If the flag is off, the bytes in the scalar are interpreted |
1635 |
|
|
as a single byte encoding. If the flag is on, the bytes in the scalar |
1636 |
|
|
are interpreted as the (variable-length, potentially multi-byte) UTF-8 encoded |
1637 |
|
|
code points of the characters. Bytes added to a UTF-8 encoded string are |
1638 |
|
|
automatically upgraded to UTF-8. If mixed non-UTF-8 and UTF-8 scalars |
1639 |
|
|
are merged (double-quoted interpolation, explicit concatenation, or |
1640 |
|
|
printf/sprintf parameter substitution), the result will be UTF-8 encoded |
1641 |
|
|
as if copies of the byte strings were upgraded to UTF-8: for example, |
1642 |
|
|
|
1643 |
|
|
=end original |
1644 |
|
|
|
1645 |
|
|
しかし、これは、文字列の文字のすべてが UTF-8 でエンコードされているとか、 |
1646 |
|
|
文字のすべてが 0xFF(255)(あるいは 0x80(128)) より大きい符号位置を |
1647 |
|
|
持つとか、文字列に文字が含まれているかとかいうことを |
1648 |
|
|
意味するわけではないことに注意してください。 |
1649 |
|
|
C<is_utf8()> がすることの全ては、C<$string> につけられている内部の |
1650 |
|
|
"utf8ness" フラグの値を返すことです。 |
1651 |
|
|
フラグが無効であれば、スカラ内のバイト列は、シングルバイト |
1652 |
|
|
エンコーディングとして解釈されます。 |
1653 |
|
|
フラグが有効であれば、スカラ内のバイト列は、(可変長で、おそらくマルチバイトの) |
1654 |
|
|
UTF-8 でエンコードされた文字の符号位置として解釈されます。 |
1655 |
|
|
(ダブルクオートされた語句、明示的な連結、printf/sprintf パラメタ |
1656 |
|
|
置換によって)非 UTF-8 と、UTF-8 のスカラがマージされた場合、この結果は、 |
1657 |
|
|
バイト文字列のコピーが UTF-8 で昇格されたかのように、 |
1658 |
|
|
UTF-8 でエンコードされています: 例えば、 |
1659 |
|
|
|
1660 |
|
|
$a = "ab\x80c"; |
1661 |
|
|
$b = "\x{100}"; |
1662 |
|
|
print "$a = $b\n"; |
1663 |
|
|
|
1664 |
|
|
=begin original |
1665 |
|
|
|
1666 |
|
|
the output string will be UTF-8-encoded C<ab\x80c = \x{100}\n>, but |
1667 |
|
|
C<$a> will stay byte-encoded. |
1668 |
|
|
|
1669 |
|
|
=end original |
1670 |
|
|
|
1671 |
|
|
出力する文字列は UTF-8 エンコードされた C<ab\x80c = \x{100}\n> になりますが、 |
1672 |
|
|
C<$a> は、バイトエンコードされたままです。 |
1673 |
|
|
|
1674 |
|
|
=begin original |
1675 |
|
|
|
1676 |
|
|
Sometimes you might really need to know the byte length of a string |
1677 |
|
|
instead of the character length. For that use either the |
1678 |
|
|
C<Encode::encode_utf8()> function or the C<bytes> pragma |
1679 |
|
|
and the C<length()> function: |
1680 |
|
|
|
1681 |
|
|
=end original |
1682 |
|
|
|
1683 |
|
|
文字列の長さではなく、文字列のバイト長を知る必要があるかもしれません。 |
1684 |
|
|
そのためには、C<Encode::encode_utf8()> 関数か、C<bytes> プラグマと |
1685 |
|
|
C<length()> 関数を使うだけです: |
1686 |
|
|
|
1687 |
|
|
my $unicode = chr(0x100); |
1688 |
|
|
print length($unicode), "\n"; # will print 1 |
1689 |
|
|
require Encode; |
1690 |
|
|
print length(Encode::encode_utf8($unicode)), "\n"; # will print 2 |
1691 |
|
|
use bytes; |
1692 |
|
|
print length($unicode), "\n"; # will also print 2 |
1693 |
|
|
# (the 0xC4 0x80 of the UTF-8) |
1694 |
|
|
no bytes; |
1695 |
|
|
|
1696 |
|
|
=item * |
1697 |
|
|
|
1698 |
|
|
=begin original |
1699 |
|
|
|
1700 |
|
|
How Do I Find Out What Encoding a File Has? |
1701 |
|
|
|
1702 |
|
|
=end original |
1703 |
|
|
|
1704 |
argrath |
1.2 |
ファイルが使っているエンコーディングを見つけるには? |
1705 |
argrath |
1.1 |
|
1706 |
|
|
=begin original |
1707 |
|
|
|
1708 |
|
|
You might try L<Encode::Guess>, but it has a number of limitations. |
1709 |
|
|
|
1710 |
|
|
=end original |
1711 |
|
|
|
1712 |
argrath |
1.2 |
L<Encode::Guess> を試してみることができますが、いくつかの制限があります。 |
1713 |
argrath |
1.1 |
|
1714 |
|
|
=item * |
1715 |
|
|
|
1716 |
|
|
=begin original |
1717 |
|
|
|
1718 |
|
|
How Do I Detect Data That's Not Valid In a Particular Encoding? |
1719 |
|
|
|
1720 |
|
|
=end original |
1721 |
|
|
|
1722 |
|
|
あるエンコーディングで、データが妥当でないことを検出するには? |
1723 |
|
|
|
1724 |
|
|
=begin original |
1725 |
|
|
|
1726 |
|
|
Use the C<Encode> package to try converting it. |
1727 |
|
|
For example, |
1728 |
|
|
|
1729 |
|
|
=end original |
1730 |
|
|
|
1731 |
|
|
C<Encode> パッケージを使って、それを変換してみてください。 |
1732 |
|
|
例えば、 |
1733 |
|
|
|
1734 |
|
|
use Encode 'decode_utf8'; |
1735 |
|
|
|
1736 |
|
|
if (eval { decode_utf8($string, Encode::FB_CROAK); 1 }) { |
1737 |
|
|
# $string is valid utf8 |
1738 |
|
|
} else { |
1739 |
|
|
# $string is not valid utf8 |
1740 |
|
|
} |
1741 |
|
|
|
1742 |
|
|
=begin original |
1743 |
|
|
|
1744 |
|
|
Or use C<unpack> to try decoding it: |
1745 |
|
|
|
1746 |
|
|
=end original |
1747 |
|
|
|
1748 |
|
|
または C<unpack> を使ってデコードしてみてください: |
1749 |
|
|
|
1750 |
|
|
use warnings; |
1751 |
|
|
@chars = unpack("C0U*", $string_of_bytes_that_I_think_is_utf8); |
1752 |
|
|
|
1753 |
|
|
=begin original |
1754 |
|
|
|
1755 |
|
|
If invalid, a C<Malformed UTF-8 character> warning is produced. The "C0" means |
1756 |
|
|
"process the string character per character". Without that, the |
1757 |
|
|
C<unpack("U*", ...)> would work in C<U0> mode (the default if the format |
1758 |
|
|
string starts with C<U>) and it would return the bytes making up the UTF-8 |
1759 |
|
|
encoding of the target string, something that will always work. |
1760 |
|
|
|
1761 |
|
|
=end original |
1762 |
|
|
|
1763 |
|
|
妥当でなければ、C<Malformed UTF-8 character> の警告が出ます。 |
1764 |
|
|
"C0" は、「文字列を文字単位で処理する」ことを意味します。 |
1765 |
|
|
それ以外では、C<unpack("U*", ...)> は (フォーマット文字列が C<U> で |
1766 |
|
|
始まっている場合のデフォルトである)C<U0> モードで動作し、ターゲット文字列の |
1767 |
|
|
UTF08 エンコーディングでのバイト数を返し、このようなことは常に動作します。 |
1768 |
|
|
|
1769 |
|
|
=item * |
1770 |
|
|
|
1771 |
|
|
=begin original |
1772 |
|
|
|
1773 |
|
|
How Do I Convert Binary Data Into a Particular Encoding, Or Vice Versa? |
1774 |
|
|
|
1775 |
|
|
=end original |
1776 |
|
|
|
1777 |
|
|
バイナリデータを特定のエンコーディングに変換するには? また、その逆は? |
1778 |
|
|
|
1779 |
|
|
=begin original |
1780 |
|
|
|
1781 |
|
|
This probably isn't as useful as you might think. |
1782 |
|
|
Normally, you shouldn't need to. |
1783 |
|
|
|
1784 |
|
|
=end original |
1785 |
|
|
|
1786 |
|
|
たぶん、思うようには有益ではないでしょう。 |
1787 |
|
|
ふつう、必要とするべきではありません。 |
1788 |
|
|
|
1789 |
|
|
=begin original |
1790 |
|
|
|
1791 |
|
|
In one sense, what you are asking doesn't make much sense: encodings |
1792 |
|
|
are for characters, and binary data are not "characters", so converting |
1793 |
|
|
"data" into some encoding isn't meaningful unless you know in what |
1794 |
|
|
character set and encoding the binary data is in, in which case it's |
1795 |
|
|
not just binary data, now is it? |
1796 |
|
|
|
1797 |
|
|
=end original |
1798 |
|
|
|
1799 |
|
|
ある意味では、あなたが尋ねようとしていることはほとんど意味はありません: |
1800 |
|
|
エンコーディングは文字のためのもので、バイナリデータは「文字」ではないので、 |
1801 |
|
|
「データ」を何かのエンコーディングに変換するということは、 |
1802 |
|
|
バイナリデータの文字セットとエンコーディングが分かっていない限り無意味です; |
1803 |
|
|
そして分かっているならそれは単なるバイナリデータではないですよね? |
1804 |
|
|
|
1805 |
|
|
=begin original |
1806 |
|
|
|
1807 |
|
|
If you have a raw sequence of bytes that you know should be |
1808 |
|
|
interpreted via a particular encoding, you can use C<Encode>: |
1809 |
|
|
|
1810 |
|
|
=end original |
1811 |
|
|
|
1812 |
|
|
どのエンコーディングで解釈するべきか分かっている生のバイト列がある場合、 |
1813 |
|
|
C<Encode> が使えます: |
1814 |
|
|
|
1815 |
|
|
use Encode 'from_to'; |
1816 |
|
|
from_to($data, "iso-8859-1", "utf-8"); # from latin-1 to utf-8 |
1817 |
|
|
|
1818 |
|
|
=begin original |
1819 |
|
|
|
1820 |
|
|
The call to C<from_to()> changes the bytes in C<$data>, but nothing |
1821 |
|
|
material about the nature of the string has changed as far as Perl is |
1822 |
|
|
concerned. Both before and after the call, the string C<$data> |
1823 |
|
|
contains just a bunch of 8-bit bytes. As far as Perl is concerned, |
1824 |
|
|
the encoding of the string remains as "system-native 8-bit bytes". |
1825 |
|
|
|
1826 |
|
|
=end original |
1827 |
|
|
|
1828 |
|
|
C<from_to()> の呼び出しは C<$data> のバイト列を変更しますが、Perl が |
1829 |
|
|
関知する限りにおいては文字列の性質は何も変わりません。 |
1830 |
|
|
呼び出しの前後の両方において、文字列 C<$data> は単に 8 ビットのバイトの |
1831 |
|
|
塊です。 |
1832 |
|
|
Perl が関知するかぎりにおいては、文字列のエンコーディングは |
1833 |
|
|
「システムにネイティブな 8 ビットのバイト列」のままです。 |
1834 |
|
|
|
1835 |
|
|
=begin original |
1836 |
|
|
|
1837 |
|
|
You might relate this to a fictional 'Translate' module: |
1838 |
|
|
|
1839 |
|
|
=end original |
1840 |
|
|
|
1841 |
|
|
これを架空の 'Translate' モジュールと関連付けることもできます: |
1842 |
|
|
|
1843 |
|
|
use Translate; |
1844 |
|
|
my $phrase = "Yes"; |
1845 |
|
|
Translate::from_to($phrase, 'english', 'deutsch'); |
1846 |
|
|
## phrase now contains "Ja" |
1847 |
|
|
|
1848 |
|
|
=begin original |
1849 |
|
|
|
1850 |
|
|
The contents of the string changes, but not the nature of the string. |
1851 |
|
|
Perl doesn't know any more after the call than before that the |
1852 |
|
|
contents of the string indicates the affirmative. |
1853 |
|
|
|
1854 |
|
|
=end original |
1855 |
|
|
|
1856 |
|
|
文字列の内容は変わりますが、文字列の性質は変わりません。 |
1857 |
|
|
Perl は文字列の内容が肯定を意味するという呼び出し前の状態よりも多く、 |
1858 |
|
|
呼び出しによって何かを知ることはありません。 |
1859 |
|
|
|
1860 |
|
|
=begin original |
1861 |
|
|
|
1862 |
|
|
Back to converting data. If you have (or want) data in your system's |
1863 |
|
|
native 8-bit encoding (e.g. Latin-1, EBCDIC, etc.), you can use |
1864 |
|
|
pack/unpack to convert to/from Unicode. |
1865 |
|
|
|
1866 |
|
|
=end original |
1867 |
|
|
|
1868 |
|
|
データの変換に戻ります。 |
1869 |
|
|
もしシステムのネイティブな 8 ビットエンコーディング(例えば、Latin-1, |
1870 |
|
|
EBCDIC など)のデータを持っている(あるいは欲しい)場合は、Unicode との変換に |
1871 |
|
|
pack/unpack を使えます。 |
1872 |
|
|
|
1873 |
|
|
$native_string = pack("W*", unpack("U*", $Unicode_string)); |
1874 |
|
|
$Unicode_string = pack("U*", unpack("W*", $native_string)); |
1875 |
|
|
|
1876 |
|
|
=begin original |
1877 |
|
|
|
1878 |
|
|
If you have a sequence of bytes you B<know> is valid UTF-8, |
1879 |
|
|
but Perl doesn't know it yet, you can make Perl a believer, too: |
1880 |
|
|
|
1881 |
|
|
=end original |
1882 |
|
|
|
1883 |
|
|
あなたはバイト列が妥当な UTF-8 であると B<分かっている> けれども、Perl が |
1884 |
|
|
知らない場合、Perl に信じさせることもできます: |
1885 |
|
|
|
1886 |
|
|
use Encode 'decode_utf8'; |
1887 |
|
|
$Unicode = decode_utf8($bytes); |
1888 |
|
|
|
1889 |
|
|
=begin original |
1890 |
|
|
|
1891 |
|
|
or: |
1892 |
|
|
|
1893 |
|
|
=end original |
1894 |
|
|
|
1895 |
|
|
または: |
1896 |
|
|
|
1897 |
|
|
$Unicode = pack("U0a*", $bytes); |
1898 |
|
|
|
1899 |
|
|
=begin original |
1900 |
|
|
|
1901 |
|
|
You can find the bytes that make up a UTF-8 sequence with |
1902 |
|
|
|
1903 |
|
|
=end original |
1904 |
|
|
|
1905 |
|
|
UTF-8 シーケンスを作るバイト列は以下のようにして見つけられます |
1906 |
|
|
|
1907 |
|
|
@bytes = unpack("C*", $Unicode_string) |
1908 |
|
|
|
1909 |
|
|
=begin original |
1910 |
|
|
|
1911 |
|
|
and you can create well-formed Unicode with |
1912 |
|
|
|
1913 |
|
|
=end original |
1914 |
|
|
|
1915 |
|
|
整形された Unicode は以下のようにして作成できます |
1916 |
|
|
|
1917 |
|
|
$Unicode_string = pack("U*", 0xff, ...) |
1918 |
|
|
|
1919 |
|
|
=item * |
1920 |
|
|
|
1921 |
|
|
=begin original |
1922 |
|
|
|
1923 |
|
|
How Do I Display Unicode? How Do I Input Unicode? |
1924 |
|
|
|
1925 |
|
|
=end original |
1926 |
|
|
|
1927 |
|
|
Unicode を表示するには? Unicode を入力するには? |
1928 |
|
|
|
1929 |
|
|
=begin original |
1930 |
|
|
|
1931 |
|
|
See L<http://www.alanwood.net/unicode/> and |
1932 |
|
|
L<http://www.cl.cam.ac.uk/~mgk25/unicode.html> |
1933 |
|
|
|
1934 |
|
|
=end original |
1935 |
|
|
|
1936 |
|
|
L<http://www.alanwood.net/unicode/> と |
1937 |
|
|
L<http://www.cl.cam.ac.uk/~mgk25/unicode.html> を参照してください。 |
1938 |
|
|
|
1939 |
|
|
=item * |
1940 |
|
|
|
1941 |
|
|
=begin original |
1942 |
|
|
|
1943 |
|
|
How Does Unicode Work With Traditional Locales? |
1944 |
|
|
|
1945 |
|
|
=end original |
1946 |
|
|
|
1947 |
|
|
伝統的なロケールと Unicode は、どのように動きますか? |
1948 |
|
|
|
1949 |
|
|
=begin original |
1950 |
|
|
|
1951 |
|
|
Perl tries to keep the two separated. Code points that are above 255 |
1952 |
|
|
are treated as Unicode; those below 256, generally as locale. This |
1953 |
|
|
works reasonably well except in some case-insensitive regular expression |
1954 |
|
|
pattern matches that in Unicode would cross the 255/256 boundary. These |
1955 |
|
|
are disallowed. |
1956 |
|
|
Also, the C<\p{}> and C<\N{}> constructs silently assume Unicode values |
1957 |
|
|
even for code points below 256. |
1958 |
|
|
See also L<perlrun> for the |
1959 |
|
|
description of the C<-C> switch and its environment counterpart, |
1960 |
|
|
C<$ENV{PERL_UNICODE}> to see how to enable various Unicode features, |
1961 |
|
|
for example by using locale settings. |
1962 |
|
|
|
1963 |
|
|
=end original |
1964 |
|
|
|
1965 |
|
|
Perl tries to keep the two separated. Code points that are above 255 |
1966 |
|
|
are treated as Unicode; those below 256, generally as locale. This |
1967 |
|
|
works reasonably well except in some case-insensitive regular expression |
1968 |
|
|
pattern matches that in Unicode would cross the 255/256 boundary. These |
1969 |
|
|
are disallowed. |
1970 |
|
|
Also, the C<\p{}> and C<\N{}> constructs silently assume Unicode values |
1971 |
|
|
even for code points below 256. |
1972 |
|
|
C<-C> スイッチと、それに対応する環境変数である |
1973 |
|
|
C<$ENV{PERL_UNICODE}> の説明のために、L<perlrun> も参照してください; |
1974 |
|
|
様々な Unicode の機能が、どのように可能になっているのか、たとえば、 |
1975 |
|
|
ロケールの設定によって、が、わかります。 |
1976 |
|
|
(TBT) |
1977 |
|
|
|
1978 |
|
|
=back |
1979 |
|
|
|
1980 |
|
|
=head2 Hexadecimal Notation |
1981 |
|
|
|
1982 |
|
|
(16 進記法) |
1983 |
|
|
|
1984 |
|
|
=begin original |
1985 |
|
|
|
1986 |
|
|
The Unicode standard prefers using hexadecimal notation because |
1987 |
|
|
that more clearly shows the division of Unicode into blocks of 256 characters. |
1988 |
|
|
Hexadecimal is also simply shorter than decimal. You can use decimal |
1989 |
|
|
notation, too, but learning to use hexadecimal just makes life easier |
1990 |
|
|
with the Unicode standard. The C<U+HHHH> notation uses hexadecimal, |
1991 |
|
|
for example. |
1992 |
|
|
|
1993 |
|
|
=end original |
1994 |
|
|
|
1995 |
|
|
Unicode 標準は、16 進記法を使うのを好みます。 |
1996 |
|
|
それは、256 の文字のブロックに Unicode を分割しているのが、 |
1997 |
|
|
他の表記よりわかりやすいからです。 |
1998 |
|
|
10 進記法を使うことも出来ますが、16 進記法を使うことを学べば、 |
1999 |
|
|
Unicode 標準との暮らしが楽になります。 |
2000 |
|
|
例えば、C<U+HHHH> 表記は、16 進記法を使います。 |
2001 |
|
|
|
2002 |
|
|
=begin original |
2003 |
|
|
|
2004 |
|
|
The C<0x> prefix means a hexadecimal number, the digits are 0-9 I<and> |
2005 |
|
|
a-f (or A-F, case doesn't matter). Each hexadecimal digit represents |
2006 |
|
|
four bits, or half a byte. C<print 0x..., "\n"> will show a |
2007 |
|
|
hexadecimal number in decimal, and C<printf "%x\n", $decimal> will |
2008 |
|
|
show a decimal number in hexadecimal. If you have just the |
2009 |
|
|
"hex digits" of a hexadecimal number, you can use the C<hex()> function. |
2010 |
|
|
|
2011 |
|
|
=end original |
2012 |
|
|
|
2013 |
|
|
C<0x> の接頭辞は、16 進の数字を意味しています。 |
2014 |
|
|
数字は、0-9 I<および> a-f(か、A-F、大文字小文字は問いません)です。 |
2015 |
|
|
それぞれの 16 進の数字は 4 ビット、1/2 バイトを表します。 |
2016 |
|
|
C<print 0x..., "\n"> は 16 進数を 10 進で見せます。 |
2017 |
|
|
C<printf "%x\n", $decimal> は 10 進数を 16 進で見せます。 |
2018 |
|
|
"hex digits" の 16 進の数字があるなら、C<hex()> 関数を使うことが出来ます。 |
2019 |
|
|
|
2020 |
|
|
print 0x0009, "\n"; # 9 |
2021 |
|
|
print 0x000a, "\n"; # 10 |
2022 |
|
|
print 0x000f, "\n"; # 15 |
2023 |
|
|
print 0x0010, "\n"; # 16 |
2024 |
|
|
print 0x0011, "\n"; # 17 |
2025 |
|
|
print 0x0100, "\n"; # 256 |
2026 |
|
|
|
2027 |
|
|
print 0x0041, "\n"; # 65 |
2028 |
|
|
|
2029 |
|
|
printf "%x\n", 65; # 41 |
2030 |
|
|
printf "%#x\n", 65; # 0x41 |
2031 |
|
|
|
2032 |
|
|
print hex("41"), "\n"; # 65 |
2033 |
|
|
|
2034 |
|
|
=head2 Further Resources |
2035 |
|
|
|
2036 |
|
|
(更なるリソース) |
2037 |
|
|
|
2038 |
|
|
=over 4 |
2039 |
|
|
|
2040 |
|
|
=item * |
2041 |
|
|
|
2042 |
|
|
Unicode Consortium |
2043 |
|
|
|
2044 |
|
|
L<http://www.unicode.org/> |
2045 |
|
|
|
2046 |
|
|
=item * |
2047 |
|
|
|
2048 |
|
|
Unicode FAQ |
2049 |
|
|
|
2050 |
|
|
L<http://www.unicode.org/unicode/faq/> |
2051 |
|
|
|
2052 |
|
|
=item * |
2053 |
|
|
|
2054 |
|
|
Unicode Glossary |
2055 |
|
|
|
2056 |
|
|
L<http://www.unicode.org/glossary/> |
2057 |
|
|
|
2058 |
|
|
=item * |
2059 |
|
|
|
2060 |
|
|
=begin original |
2061 |
|
|
|
2062 |
|
|
Unicode Recommended Reading List |
2063 |
|
|
|
2064 |
|
|
=end original |
2065 |
|
|
|
2066 |
argrath |
1.2 |
Unicode 関連の推奨参照リスト |
2067 |
argrath |
1.1 |
|
2068 |
|
|
=begin original |
2069 |
|
|
|
2070 |
|
|
The Unicode Consortium has a list of articles and books, some of which |
2071 |
|
|
give a much more in depth treatment of Unicode: |
2072 |
|
|
L<http://unicode.org/resources/readinglist.html> |
2073 |
|
|
|
2074 |
|
|
=end original |
2075 |
|
|
|
2076 |
argrath |
1.2 |
The Unicode Consortium には記事と本の一覧があり、has a list of articles and books, some of which |
2077 |
argrath |
1.1 |
give a much more in depth treatment of Unicode: |
2078 |
|
|
L<http://unicode.org/resources/readinglist.html> |
2079 |
|
|
(TBT) |
2080 |
|
|
|
2081 |
|
|
=item * |
2082 |
|
|
|
2083 |
|
|
Unicode Useful Resources |
2084 |
|
|
|
2085 |
|
|
L<http://www.unicode.org/unicode/onlinedat/resources.html> |
2086 |
|
|
|
2087 |
|
|
=item * |
2088 |
|
|
|
2089 |
|
|
Unicode and Multilingual Support in HTML, Fonts, Web Browsers and Other Applications |
2090 |
|
|
|
2091 |
|
|
L<http://www.alanwood.net/unicode/> |
2092 |
|
|
|
2093 |
|
|
=item * |
2094 |
|
|
|
2095 |
|
|
UTF-8 and Unicode FAQ for Unix/Linux |
2096 |
|
|
|
2097 |
|
|
L<http://www.cl.cam.ac.uk/~mgk25/unicode.html> |
2098 |
|
|
|
2099 |
|
|
=item * |
2100 |
|
|
|
2101 |
|
|
Legacy Character Sets |
2102 |
|
|
|
2103 |
|
|
L<http://www.czyborra.com/> |
2104 |
|
|
L<http://www.eki.ee/letter/> |
2105 |
|
|
|
2106 |
|
|
=item * |
2107 |
|
|
|
2108 |
|
|
=begin original |
2109 |
|
|
|
2110 |
|
|
You can explore various information from the Unicode data files using |
2111 |
|
|
the C<Unicode::UCD> module. |
2112 |
|
|
|
2113 |
|
|
=end original |
2114 |
|
|
|
2115 |
|
|
C<Unicode::UCD> モジュールを使って、Unicode データファイルから |
2116 |
|
|
様々な情報を調べることができます。 |
2117 |
|
|
|
2118 |
|
|
=back |
2119 |
|
|
|
2120 |
|
|
=head1 UNICODE IN OLDER PERLS |
2121 |
|
|
|
2122 |
|
|
(古い Perl での Unicode) |
2123 |
|
|
|
2124 |
|
|
=begin original |
2125 |
|
|
|
2126 |
|
|
If you cannot upgrade your Perl to 5.8.0 or later, you can still |
2127 |
|
|
do some Unicode processing by using the modules C<Unicode::String>, |
2128 |
|
|
C<Unicode::Map8>, and C<Unicode::Map>, available from CPAN. |
2129 |
|
|
If you have the GNU recode installed, you can also use the |
2130 |
|
|
Perl front-end C<Convert::Recode> for character conversions. |
2131 |
|
|
|
2132 |
|
|
=end original |
2133 |
|
|
|
2134 |
|
|
Perl 5.8.0 以降にアップグレード出来なくても、まだ、CPAN から利用できる、 |
2135 |
|
|
C<Unicode::String>、C<Unicode::Mmap8>、C<bUnicode::Map> を使って、 |
2136 |
|
|
いくつかの Unicode 処理ができます。 |
2137 |
|
|
GNU recode がインストールされているなら、それの Perl フロントエンドである |
2138 |
|
|
C<Convert::Recode> を文字変換のために使えます。 |
2139 |
|
|
|
2140 |
|
|
=begin original |
2141 |
|
|
|
2142 |
|
|
The following are fast conversions from ISO 8859-1 (Latin-1) bytes |
2143 |
|
|
to UTF-8 bytes and back, the code works even with older Perl 5 versions. |
2144 |
|
|
|
2145 |
|
|
=end original |
2146 |
|
|
|
2147 |
|
|
下記のものは、ISO 8859-1 (Latin-1) バイト列から UTF-8 バイト列に |
2148 |
|
|
(あるいはその逆に)素早く変換するものです。 |
2149 |
|
|
このコードは古い Perl 5 でも動きます。 |
2150 |
|
|
|
2151 |
|
|
# ISO 8859-1 to UTF-8 |
2152 |
|
|
s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg; |
2153 |
|
|
|
2154 |
|
|
# UTF-8 to ISO 8859-1 |
2155 |
|
|
s/([\xC2\xC3])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg; |
2156 |
|
|
|
2157 |
|
|
=head1 SEE ALSO |
2158 |
|
|
|
2159 |
|
|
L<perlunitut>, L<perlunicode>, L<Encode>, L<open>, L<utf8>, L<bytes>, |
2160 |
|
|
L<perlretut>, L<perlrun>, L<Unicode::Collate>, L<Unicode::Normalize>, |
2161 |
|
|
L<Unicode::UCD> |
2162 |
|
|
|
2163 |
|
|
=head1 ACKNOWLEDGMENTS |
2164 |
|
|
|
2165 |
|
|
(謝辞) |
2166 |
|
|
|
2167 |
|
|
Thanks to the kind readers of the perl5-porters@perl.org, |
2168 |
|
|
perl-unicode@perl.org, linux-utf8@nl.linux.org, and unicore@unicode.org |
2169 |
|
|
mailing lists for their valuable feedback. |
2170 |
|
|
|
2171 |
|
|
=head1 AUTHOR, COPYRIGHT, AND LICENSE |
2172 |
|
|
|
2173 |
|
|
(著者、著作権、ライセンス) |
2174 |
|
|
|
2175 |
|
|
Copyright 2001-2011 Jarkko Hietaniemi E<lt>jhi@iki.fiE<gt> |
2176 |
|
|
|
2177 |
|
|
This document may be distributed under the same terms as Perl itself. |
2178 |
|
|
|
2179 |
|
|
=begin meta |
2180 |
|
|
|
2181 |
|
|
Translate: ktats (5.8.1) |
2182 |
|
|
Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-) |
2183 |
|
|
Status: in progress |
2184 |
|
|
|
2185 |
|
|
=end meta |
2186 |
|
|
|