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

CVS リポジトリの参照

Contents of /perldocjp/docs/perl/5.12.1/perlunitut.pod

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


Revision 1.1 - (show annotations) (download)
Fri Jun 15 07:35:16 2012 UTC (11 years, 11 months ago) by argrath
Branch: MAIN
CVS Tags: HEAD
5.12.1/*

1
2 =encoding euc-jp
3
4 =head1 NAME
5
6 =begin original
7
8 perlunitut - Perl Unicode Tutorial
9
10 =end original
11
12 perlunitut - Perl における Unicode のチュートリアル
13
14 =head1 DESCRIPTION
15
16 =begin original
17
18 The days of just flinging strings around are over. It's well established that
19 modern programs need to be capable of communicating funny accented letters, and
20 things like euro symbols. This means that programmers need new habits. It's
21 easy to program Unicode capable software, but it does require discipline to do
22 it right.
23
24 =end original
25
26 文字列を単に放り出す日々は終わりました。
27 最近のプログラムでは変わったアクセントのついた文字や、ユーロのマークの
28 ようなものを通信出来る必要があることが確立しています。
29 これは、プログラマは新しい意味が必要であることを意味しています。
30 Unicode 対応ソフトウェアをプログラムするのは簡単ですが、それを正しく行うには
31 訓練が必要です。
32
33 =begin original
34
35 There's a lot to know about character sets, and text encodings. It's probably
36 best to spend a full day learning all this, but the basics can be learned in
37 minutes.
38
39 =end original
40
41 文字集合とテキストエンコーディングに関しては学ぶべきことがたくさんあります。
42 おそらくこれら全てを学ぶのに丸一日を使うのが最良ですが、基本は数分で
43 学べます。
44
45 =begin original
46
47 These are not the very basics, though. It is assumed that you already
48 know the difference between bytes and characters, and realise (and accept!)
49 that there are many different character sets and encodings, and that your
50 program has to be explicit about them. Recommended reading is "The Absolute
51 Minimum Every Software Developer Absolutely, Positively Must Know About Unicode
52 and Character Sets (No Excuses!)" by Joel Spolsky, at
53 L<http://joelonsoftware.com/articles/Unicode.html>.
54
55 =end original
56
57 しかし、これらは超基本ではありません。
58 あなたがすでにバイトと文字の違いを知っていて、様々な文字集合と
59 エンコーディングがあることを自覚していて(さらに受け入れていて!)、
60 あなたのプログラムがこれらを明示的にする必要がある、と仮定しています。
61 お勧めの読み物は Joel Spolsky による
62 "The Absolute
63 Minimum Every Software Developer Absolutely, Positively Must Know About Unicode
64 and Character Sets (No Excuses!)"
65 L<http://joelonsoftware.com/articles/Unicode.html> です。
66
67 =begin original
68
69 This tutorial speaks in rather absolute terms, and provides only a limited view
70 of the wealth of character string related features that Perl has to offer. For
71 most projects, this information will probably suffice.
72
73 =end original
74
75 このチュートリアルはどちらかといえば独立した用語を使っていますし、Perl が
76 提供している豊富な文字列に関する機能のうち限定された見方のみを扱っています。
77 ほとんどのプロジェクトにとっては、おそらくこの情報で十分です。
78
79 =head2 Definitions
80
81 (定義)
82
83 =begin original
84
85 It's important to set a few things straight first. This is the most important
86 part of this tutorial. This view may conflict with other information that you
87 may have found on the web, but that's mostly because many sources are wrong.
88
89 =end original
90
91 まず、いくつかのことをはっきりさせることが重要です。
92 これはこのチュートリアルで最も重要な部分です。
93 この考え方はあなたが web で見つけたその他の情報と矛盾するかもしれませんが、
94 ほとんどの場合、それは web で見つけた情報の方が間違っているためです。
95
96 =begin original
97
98 You may have to re-read this entire section a few times...
99
100 =end original
101
102 このセクション全体を何回か読み直す必要があるかもしれません…
103
104 =head3 Unicode
105
106 =begin original
107
108 B<Unicode> is a character set with room for lots of characters. The ordinal
109 value of a character is called a B<code point>. (But in practice, the
110 distinction between code point and character is blurred, so the terms often
111 are used interchangeably.)
112
113 =end original
114
115 B<Unicode> は大量の文字を表現できる文字集合です。
116 文字の序数は B<符号位置> と呼ばれます。
117 (しかし実際には、符号位置と文字との区別はあいまいなので、この用語はしばしば
118 同義語として使われます。)
119
120 =begin original
121
122 There are many, many code points, but computers work with bytes, and a byte has
123 room for only 256 values. Unicode has many more characters, so you need a
124 method to make these accessible.
125
126 =end original
127
128 符号位置は本当に大量にありますが、コンピュータはバイト単位で
129 動いていて、バイトは 256 種類の値のみを持ちます。
130 Unicode はもっとたくさんの文字を持っているので、これらにアクセスできるような
131 手法が必要です。
132
133 =begin original
134
135 Unicode is encoded using several competing encodings, of which UTF-8 is the
136 most used. In a Unicode encoding, multiple subsequent bytes can be used to
137 store a single code point, or simply: character.
138
139 =end original
140
141 Unicode はいくつかの競合するエンコーディングを使ってエンコードされていて、
142 UTF-8 がもっともよく使われています。
143 Unicode のエンコーディングでは、複数の引き続くバイト列を一つの符号位置
144 (あるいは単純に「文字」)を保管するのに使います。
145
146 =head3 UTF-8
147
148 =begin original
149
150 B<UTF-8> is a Unicode encoding. Many people think that Unicode and UTF-8 are
151 the same thing, but they're not. There are more Unicode encodings, but much of
152 the world has standardized on UTF-8.
153
154 =end original
155
156 B<UTF-8> は Unicode のエンコーディングです。
157 多くの人々は Unicode と UTF-8 は同じだと考えていますが、違います。
158 Unicode のエンコーディングには他にもありますが、世界のほとんどでは標準として
159 UTF-8 が使われています。
160
161 =begin original
162
163 UTF-8 treats the first 128 codepoints, 0..127, the same as ASCII. They take
164 only one byte per character. All other characters are encoded as two or more
165 (up to six) bytes using a complex scheme. Fortunately, Perl handles this for
166 us, so we don't have to worry about this.
167
168 =end original
169
170 UTF-8 は最初の 128 の符号位置(0..127) を ASCII と同様に扱います。
171 文字毎に 1 バイトしかかかりません。
172 その他の文字は、複雑な仕組みを使って 2 またはそれ以上 (最大 6) のバイトを
173 使います。
174 幸い、Perl がその作業をするので、私たちがそれについて心配する必要は
175 ありません。
176
177 =head3 Text strings (character strings)
178
179 (テキスト文字列(文字の文字列))
180
181 =begin original
182
183 B<Text strings>, or B<character strings> are made of characters. Bytes are
184 irrelevant here, and so are encodings. Each character is just that: the
185 character.
186
187 =end original
188
189 B<テキスト文字列>、または B<文字の文字列> は文字からなります。
190 バイト列はここでは無意味で、エンコーディングも無意味です。
191 各文字は単に: 文字です。
192
193 =begin original
194
195 On a text string, you would do things like:
196
197 =end original
198
199 テキスト文字列では、以下のようにします:
200
201 $text =~ s/foo/bar/;
202 if ($string =~ /^\d+$/) { ... }
203 $text = ucfirst $text;
204 my $character_count = length $text;
205
206 =begin original
207
208 The value of a character (C<ord>, C<chr>) is the corresponding Unicode code
209 point.
210
211 =end original
212
213 文字の値 (C<ord>, C<chr>) は対応する Unicode 符号位置です。
214
215 =head3 Binary strings (byte strings)
216
217 (バイナリ文字列(バイト文字列))
218
219 =begin original
220
221 B<Binary strings>, or B<byte strings> are made of bytes. Here, you don't have
222 characters, just bytes. All communication with the outside world (anything
223 outside of your current Perl process) is done in binary.
224
225 =end original
226
227 B<バイナリ文字列>、または B<バイト文字列> はバイト列からなります。
228 ここでは、文字はなく、単にバイトだけがあります。
229 外側の世界(現在の Perl プロセスの外側のあらゆるもの) との通信はバイナリで
230 行われます。
231
232 =begin original
233
234 On a binary string, you would do things like:
235
236 =end original
237
238 バイナリ文字列では、以下のようにします:
239
240 my (@length_content) = unpack "(V/a)*", $binary;
241 $binary =~ s/\x00\x0F/\xFF\xF0/; # for the brave :)
242 print {$fh} $binary;
243 my $byte_count = length $binary;
244
245 =head3 Encoding
246
247 (エンコード)
248
249 =begin original
250
251 B<Encoding> (as a verb) is the conversion from I<text> to I<binary>. To encode,
252 you have to supply the target encoding, for example C<iso-8859-1> or C<UTF-8>.
253 Some encodings, like the C<iso-8859> ("latin") range, do not support the full
254 Unicode standard; characters that can't be represented are lost in the
255 conversion.
256
257 =end original
258
259 (動詞としての) B<エンコード> は I<テキスト> から I<バイナリ> への変換です。
260 エンコードするには、C<iso-8859-1> や C<UTF-8> のような、エンコード先の
261 エンコーディングが必要です。
262 C<iso-8859> ("latin") のような一部のエンコーディングは、Unicode 標準に
263 完全に対応していません; 表現できない文字は変換時に失われます。
264
265 =head3 Decoding
266
267 (デコード)
268
269 =begin original
270
271 B<Decoding> is the conversion from I<binary> to I<text>. To decode, you have to
272 know what encoding was used during the encoding phase. And most of all, it must
273 be something decodable. It doesn't make much sense to decode a PNG image into a
274 text string.
275
276 =end original
277
278 B<デコード> は I<バイナリ> から I<テキスト> への変換です。
279 デコードするには、エンコード時に使われたエンコーディングが何かを知っている
280 必要があります。
281 そしてそもそも、それはデコード可能でなければなりません。
282 PNG イメージをテキスト文字列にデコードしてもほとんど意味はないです。
283
284 =head3 Internal format
285
286 (内部形式)
287
288 =begin original
289
290 Perl has an B<internal format>, an encoding that it uses to encode text strings
291 so it can store them in memory. All text strings are in this internal format.
292 In fact, text strings are never in any other format!
293
294 =end original
295
296 Perl には B<内部形式> と呼ばれる、テキスト文字列をメモリに格納できるように
297 エンコードを行うために使うエンコーディングがあります。
298 全てのテキスト文字列はこの内部形式になります。
299 実際、テキスト文字列が他の形式になることは決してないのです!
300
301 =begin original
302
303 You shouldn't worry about what this format is, because conversion is
304 automatically done when you decode or encode.
305
306 =end original
307
308 この形式がどのようなものであるかを気にする必要はないはずです; なぜなら
309 変換はデコードやエンコードの際に自動的に行われるからです。
310
311 =head2 Your new toolkit
312
313 (新しいツールキット)
314
315 =begin original
316
317 Add to your standard heading the following line:
318
319 =end original
320
321 あなたの標準ヘッダに以下の行を追加します:
322
323 use Encode qw(encode decode);
324
325 =begin original
326
327 Or, if you're lazy, just:
328
329 =end original
330
331 あるいは、あなたが怠け者なら、単に以下のようにします:
332
333 use Encode;
334
335 =head2 I/O flow (the actual 5 minute tutorial)
336
337 (I/O の流れ(実際の 5 分間チュートリアル))
338
339 =begin original
340
341 The typical input/output flow of a program is:
342
343 =end original
344
345 典型的なプログラムの入出力の流れは以下のものです:
346
347 =begin original
348
349 1. Receive and decode
350 2. Process
351 3. Encode and output
352
353 =end original
354
355 1. 受信してデコードする
356 2. 処理する
357 3. エンコードして出力する
358
359 =begin original
360
361 If your input is binary, and is supposed to remain binary, you shouldn't decode
362 it to a text string, of course. But in all other cases, you should decode it.
363
364 =end original
365
366 もし入力がバイナリで、バイナリのままにしておく必要があるなら、これを
367 テキスト文字列にデコードするべきではありません。
368 しかし、それ以外の全ての場合では、デコードするべきです。
369
370 =begin original
371
372 Decoding can't happen reliably if you don't know how the data was encoded. If
373 you get to choose, it's a good idea to standardize on UTF-8.
374
375 =end original
376
377 もしデータがどのようにエンコードされたか分からないなら、デコードには
378 信頼性がありません。
379 もしあなたが選択するのなら、UTF-8 に標準化するのはいい考えです。
380
381 my $foo = decode('UTF-8', get 'http://example.com/');
382 my $bar = decode('ISO-8859-1', readline STDIN);
383 my $xyzzy = decode('Windows-1251', $cgi->param('foo'));
384
385 =begin original
386
387 Processing happens as you knew before. The only difference is that you're now
388 using characters instead of bytes. That's very useful if you use things like
389 C<substr>, or C<length>.
390
391 =end original
392
393 処理は今まで通りに行います。
394 唯一の違いは、バイトではなく文字を使うことです。
395 これは C<substr> や C<length> のようなものを使うときにとても便利です。
396
397 =begin original
398
399 It's important to realize that there are no bytes in a text string. Of course,
400 Perl has its internal encoding to store the string in memory, but ignore that.
401 If you have to do anything with the number of bytes, it's probably best to move
402 that part to step 3, just after you've encoded the string. Then you know
403 exactly how many bytes it will be in the destination string.
404
405 =end original
406
407 テキスト文字列にはバイト列はないということを自覚することは重要です。
408 もちろん、Perl は文字列をメモリに保管するために内部形式を使いますが、
409 これは無視してください。
410 もしバイト数を扱うような何かをする必要があるなら、おそらくその部分を
411 ステップ 3 の、文字列をエンコードした直後に移すのが最良です。
412 そこで目的の文字列が何バイトになるのかが正確に分かります。
413
414 =begin original
415
416 The syntax for encoding text strings to binary strings is as simple as decoding:
417
418 =end original
419
420 テキスト文字列をバイナリ文字列にするための文法はデコード時と同じぐらい
421 単純です:
422
423 $body = encode('UTF-8', $body);
424
425 =begin original
426
427 If you needed to know the length of the string in bytes, now's the perfect time
428 for that. Because C<$body> is now a byte string, C<length> will report the
429 number of bytes, instead of the number of characters. The number of
430 characters is no longer known, because characters only exist in text strings.
431
432 =end original
433
434 もし文字列のバイト数が必要なら、今がそれをする最良のタイミングです。
435 なぜなら C<$body> はバイト文字列なので、C<length> は文字数ではなく、
436 バイト数を返します。
437 文字というものはテキスト文字列でのみ存在するので、もはや文字数というものは
438 わからなくなっています。
439
440 my $byte_count = length $body;
441
442 =begin original
443
444 And if the protocol you're using supports a way of letting the recipient know
445 which character encoding you used, please help the receiving end by using that
446 feature! For example, E-mail and HTTP support MIME headers, so you can use the
447 C<Content-Type> header. They can also have C<Content-Length> to indicate the
448 number of I<bytes>, which is always a good idea to supply if the number is
449 known.
450
451 =end original
452
453 そしてもしあなたの使っているプロトコルが、あなたが使ったエンコーディングを
454 相手に伝える方法に対応しているなら、その機能を使って受信側を助けてあげて
455 ください!
456 例えば、E-mail と HTTP は MIME ヘッダに対応しているので、
457 C<Content-Type> ヘッダが使えます。
458 I<バイト> 数を示すための C<Content-Length> もあるので、もしバイト数が
459 わかっているなら、これをつけるのは常に良い考えです。
460
461 "Content-Type: text/plain; charset=UTF-8",
462 "Content-Length: $byte_count"
463
464 =head1 SUMMARY
465
466 (まとめ)
467
468 =begin original
469
470 Decode everything you receive, encode everything you send out. (If it's text
471 data.)
472
473 =end original
474
475 (扱っているのがテキストデータなら)受け取ったもの全てを decode して、
476 送り出すもの全てを encode しましょう。
477
478 =head1 Q and A (or FAQ)
479
480 =begin original
481
482 After reading this document, you ought to read L<perlunifaq> too.
483
484 =end original
485
486 この文書を読んだ後、L<perlunifaq> も読むべきです。
487
488 =head1 ACKNOWLEDGEMENTS
489
490 (謝辞)
491
492 Thanks to Johan Vromans from Squirrel Consultancy. His UTF-8 rants during the
493 Amsterdam Perl Mongers meetings got me interested and determined to find out
494 how to use character encodings in Perl in ways that don't break easily.
495
496 Thanks to Gerard Goossen from TTY. His presentation "UTF-8 in the wild" (Dutch
497 Perl Workshop 2006) inspired me to publish my thoughts and write this tutorial.
498
499 Thanks to the people who asked about this kind of stuff in several Perl IRC
500 channels, and have constantly reminded me that a simpler explanation was
501 needed.
502
503 Thanks to the people who reviewed this document for me, before it went public.
504 They are: Benjamin Smith, Jan-Pieter Cornet, Johan Vromans, Lukas Mai, Nathan
505 Gray.
506
507 =head1 AUTHOR
508
509 (著者)
510
511 Juerd Waalboer <#####@juerd.nl>
512
513 =head1 SEE ALSO
514
515 L<perlunifaq>, L<perlunicode>, L<perluniintro>, L<Encode>
516
517 =begin meta
518
519 Translate: Kentaro Shirakta <argrath@ub32.org>
520 Status: completed
521
522 =end meta
523

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