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

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.1 - (show annotations) (download)
Sat Jun 23 18:28:31 2012 UTC (11 years, 10 months ago) by argrath
Branch: MAIN
in progress

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

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