オープンソース・ソフトウェアの開発とダウンロード

CVS リポジトリの参照

Contents of /perldocjp/docs/perl/5.14.1/perluniintro.pod

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download)
Sun Mar 24 13:00:34 2013 UTC (11 years, 1 month ago) by argrath
Branch: MAIN
Changes since 1.2: +141 -165 lines
5.1[46].1/perluniintro

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

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26