perlunifaq > 5.28.0 との差分

perlunifaq 5.28.0 と 5.10.1 の差分

11
22=encoding euc-jp
33
44=head1 NAME
55
66=begin original
77
88perlunifaq - Perl Unicode FAQ
99
1010=end original
1111
1212perlunifaq - Perl Unicode FAQ
1313
1414=head1 Q and A
1515
1616=begin original
1717
1818This is a list of questions and answers about Unicode in Perl, intended to be
1919read after L<perlunitut>.
2020
2121=end original
2222
2323これは、L<perlunitut> の後で読むことを想定した、Perl での Unicode に関する
2424質問と答えの一覧です。
2525
2626=head2 perlunitut isn't really a Unicode tutorial, is it?
2727
2828(perlunitut は実際には Unicode チュートリアルじゃないんじゃないの?)
2929
3030=begin original
3131
3232No, and this isn't really a Unicode FAQ.
3333
3434=end original
3535
3636はい、違います; そしてこれは実際には Unicode FAQ ではありません。
3737
3838=begin original
3939
40Perl has an abstracted interface for all supported character encodings, so this
40Perl has an abstracted interface for all supported character encodings, so they
4141is actually a generic C<Encode> tutorial and C<Encode> FAQ. But many people
4242think that Unicode is special and magical, and I didn't want to disappoint
4343them, so I decided to call the document a Unicode tutorial.
4444
4545=end original
4646
4747Perl は対応している全ての文字エンコーディングへの抽象インターフェースを
4848持っているので、実際には汎用の C<Encode> チュートリアルと
4949C<Encode> FAQ です。
5050しかし、多くの人々が、Unicode は特別でマジカルなものだと考えていて、
5151私は彼らを失望させたくなかったので、そのドキュメントを
5252Unicode チュートリアルと呼ぶことに決めました。
5353
5454=head2 What character encodings does Perl support?
5555
5656(Perl が対応している文字エンコーディングは何?)
5757
5858=begin original
5959
6060To find out which character encodings your Perl supports, run:
6161
6262=end original
6363
6464Perl がどの文字エンコーディングに対応しているかを見つけるには、以下を
6565実行してください:
6666
6767 perl -MEncode -le "print for Encode->encodings(':all')"
6868
6969=head2 Which version of perl should I use?
7070
7171(どのバージョンの perl を使うべき?)
7272
7373=begin original
7474
7575Well, if you can, upgrade to the most recent, but certainly C<5.8.1> or newer.
76The tutorial and FAQ assume the latest release.
76The tutorial and FAQ are based on the status quo as of C<5.8.8>.
7777
7878=end original
7979
8080うーん、もし可能なら、最新にアップグレードしてください; 但し、確実に
8181C<5.8.1> 以降にはしてください。
82チュートリアルと FAQ は最新リリース仮定しています。
82チュートリアルと FAQ は C<5.8.8> の状態基にしています。
8383
8484=begin original
8585
8686You should also check your modules, and upgrade them if necessary. For example,
8787HTML::Entities requires version >= 1.32 to function correctly, even though the
8888changelog is silent about this.
8989
9090=end original
9191
9292モジュールもチェックして、もし必要ならアップグレードするべきです。
9393例えば HTML::Entities は、changelog は何も触れていませんが、正しく
9494動作するためにはバージョン >= 1.32 が必要です。
9595
9696=head2 What about binary data, like images?
9797
9898(イメージのようなバイナリデータはどうするの?)
9999
100100=begin original
101101
102102Well, apart from a bare C<binmode $fh>, you shouldn't treat them specially.
103103(The binmode is needed because otherwise Perl may convert line endings on Win32
104104systems.)
105105
106106=end original
107107
108108うーん、生の C<binmode $fh> を別として、特別に扱う必要はないはずです。
109109(Win32 システムで Perl が行端を変更しないようにするために、binmode が
110110必要です。)
111111
112112=begin original
113113
114114Be careful, though, to never combine text strings with binary strings. If you
115115need text in a binary stream, encode your text strings first using the
116116appropriate encoding, then join them with binary strings. See also: "What if I
117117don't encode?".
118118
119119=end original
120120
121121但し、決してテキスト文字列とバイナリ文字列を結合しないように
122122注意してください。
123123もしバイナリストリームにテキストが必要なら、まずテキスト文字列を適切な
124124エンコーディングを使ってエンコードして、それをバイナリ文字列と
125125結合してください。
126126L<"What if I don't encode?"> も参照してください。
127127
128128=head2 When should I decode or encode?
129129
130130(デコードやエンコードはいつ行うべき?)
131131
132132=begin original
133133
134134Whenever you're communicating text with anything that is external to your perl
135135process, like a database, a text file, a socket, or another program. Even if
136136the thing you're communicating with is also written in Perl.
137137
138138=end original
139139
140140データベース、テキストファイル、ソケット、他のプログラムといった、自分の
141141perl プロセスの外側にある何かとテキストを通信するときはいつでも、です。
142142通信の相手が Perl で書かれている場合も同じです。
143143
144144=head2 What if I don't decode?
145145
146146(デコードしないとどうなるの?)
147147
148148=begin original
149149
150150Whenever your encoded, binary string is used together with a text string, Perl
151151will assume that your binary string was encoded with ISO-8859-1, also known as
152152latin-1. If it wasn't latin-1, then your data is unpleasantly converted. For
153153example, if it was UTF-8, the individual bytes of multibyte characters are seen
154154as separate characters, and then again converted to UTF-8. Such double encoding
155155can be compared to double HTML encoding (C<&amp;gt;>), or double URI encoding
156156(C<%253E>).
157157
158158=end original
159159
160160エンコードされたバイナリ文字列をテキスト文字列と一緒に使ったときはいつでも、
161161Perl はバイナリ文字列が ISO-8859-1 またの名を latin-1 と仮定します。
162162もしこれが latin-1 でなかった場合、データは不愉快な形に変換されます。
163163例えば、もしデータが UTF-8 だった場合、マルチバイト文字のそれぞれのバイトが
164164文字として扱われ、それから再び UTF-8 に変換されます。
165165このような二重エンコードは二重 HTML エンコーディング (C<&amp;gt;>) や
166166二重 URI エンコーディング (C<%253E>) と比較できます。
167167
168168=begin original
169169
170170This silent implicit decoding is known as "upgrading". That may sound
171171positive, but it's best to avoid it.
172172
173173=end original
174174
175175この、暗黙のうちに行われるデコードは「昇格」("upgrading")と呼ばれます。
176176これは前向きなことに聞こえるかもしれませんが、避けるのが最良です。
177177
178178=head2 What if I don't encode?
179179
180180(エンコードしないとどうなるの?)
181181
182182=begin original
183183
184184Your text string will be sent using the bytes in Perl's internal format. In
185185some cases, Perl will warn you that you're doing something wrong, with a
186186friendly warning:
187187
188188=end original
189189
190190テキスト文字列は Perl の内部形式のバイト列を使って送信されます。
191191いくつかの場合では、Perl は何かが間違っていることを、親切なメッセージで
192192警告します:
193193
194194 Wide character in print at example.pl line 2.
195195
196196=begin original
197197
198198Because the internal format is often UTF-8, these bugs are hard to spot,
199199because UTF-8 is usually the encoding you wanted! But don't be lazy, and don't
200200use the fact that Perl's internal format is UTF-8 to your advantage. Encode
201201explicitly to avoid weird bugs, and to show to maintenance programmers that you
202202thought this through.
203203
204204=end original
205205
206206内部形式はしばしば UTF-8 なので、このバグは発見しにくいです; なぜなら
207207あなたがほしいのは普通 UTF-8 だからです!
208208しかし、手を抜かないでください; そして Perl の内部形式が UTF-8 であることを
209209利用しようとしないでください。
210210奇妙なバグを防ぐため、そして保守プログラマに対してあなたが何を考えたかを
211211示すために、明示的にエンコードしてください。
212212
213213=head2 Is there a way to automatically decode or encode?
214214
215215(自動的にデコードやエンコードする方法はある?)
216216
217217=begin original
218218
219219If all data that comes from a certain handle is encoded in exactly the same
220220way, you can tell the PerlIO system to automatically decode everything, with
221221the C<encoding> layer. If you do this, you can't accidentally forget to decode
222222or encode anymore, on things that use the layered handle.
223223
224224=end original
225225
226226もし、あるハンドルから来る全てのデータが正確に同じ方法で
227227エンコードされているなら、C<encoding> 層を使って、 PerlIO システムに自動的に
228228全てをデコードするように伝えることができます。
229229これを行えば、この層のハンドルを使っている限り、うっかりデコードや
230230エンコードを忘れることはありません。
231231
232232=begin original
233233
234234You can provide this layer when C<open>ing the file:
235235
236236=end original
237237
238238ファイルを C<open> するときにこの層を指定することができます:
239239
240 open my $fh, '>:encoding(UTF-8)', $filename; # auto encoding on write
240 open my $fh, '>:encoding(UTF-8)', $filename; # auto encoding on write
241 open my $fh, '<:encoding(UTF-8)', $filename; # auto decoding on read
241 open my $fh, '<:encoding(UTF-8)', $filename; # auto decoding on read
242242
243243=begin original
244244
245245Or if you already have an open filehandle:
246246
247247=end original
248248
249249あるいは既にオープンしているファイルハンドルがあるなら:
250250
251 binmode $fh, ':encoding(UTF-8)';
251 binmode $fh, ':encoding(UTF-8)';
252252
253253=begin original
254254
255255Some database drivers for DBI can also automatically encode and decode, but
256256that is sometimes limited to the UTF-8 encoding.
257257
258258=end original
259259
260260DBI のデータベースドライバのいくつかも、エンコードとデコードを自動的に
261261行いますが、ときどきこれは UTF-8 エンコーディングに制限されています。
262262
263263=head2 What if I don't know which encoding was used?
264264
265265(どのエンコーディングが使われているかわからないときは?)
266266
267267=begin original
268268
269269Do whatever you can to find out, and if you have to: guess. (Don't forget to
270270document your guess with a comment.)
271271
272272=end original
273273
274274なんとかして見つけるか、もし必要なら、推測してください。
275275(どう推測したかをコメントとして文書化するのを忘れないでください。)
276276
277277=begin original
278278
279279You could open the document in a web browser, and change the character set or
280280character encoding until you can visually confirm that all characters look the
281281way they should.
282282
283283=end original
284284
285285ドキュメントを web ブラウザで開いて、全ての文字があるべき形であることを
286286視覚的に確認できるまで文字集合や文字エンコーディングを変更する方法も
287287あります。
288288
289289=begin original
290290
291291There is no way to reliably detect the encoding automatically, so if people
292292keep sending you data without charset indication, you may have to educate them.
293293
294294=end original
295295
296296エンコーディングを自動的に検出するための信頼性のある方法はないので、
297297もし人々があなたに文字集合の指示なしにデータを送り続けるなら、彼らを
298298教育する必要があるかもしれません。
299299
300300=head2 Can I use Unicode in my Perl sources?
301301
302302(Perl のソースコードに Unicode は使える?)
303303
304304=begin original
305305
306306Yes, you can! If your sources are UTF-8 encoded, you can indicate that with the
307307C<use utf8> pragma.
308308
309309=end original
310310
311311はい、できます!
312312ソースコードが UTF-8 でエンコードされているなら、C<use utf8> プラグマを
313313使ってそれを示すことができます。
314314
315315 use utf8;
316316
317317=begin original
318318
319319This doesn't do anything to your input, or to your output. It only influences
320320the way your sources are read. You can use Unicode in string literals, in
321321identifiers (but they still have to be "word characters" according to C<\w>),
322322and even in custom delimiters.
323323
324324=end original
325325
326326これは入出力に対しては何も行いません。
327327ソースを読み込む方法のみに影響を与えます。
328328文字列リテラル、識別子(しかし C<\w> に従った「単語文字」である必要が
329329あります)、そして独自デリミタにすら Unicode が使えます。
330330
331331=head2 Data::Dumper doesn't restore the UTF8 flag; is it broken?
332332
333333(Data::Dumper は UTF8 フラグを復元しません; これは壊れてるの?)
334334
335335=begin original
336336
337337No, Data::Dumper's Unicode abilities are as they should be. There have been
338338some complaints that it should restore the UTF8 flag when the data is read
339339again with C<eval>. However, you should really not look at the flag, and
340340nothing indicates that Data::Dumper should break this rule.
341341
342342=end original
343343
344344いいえ、Data::Dumper の Unicode 能力は、あるべき形であります。
345345C<eval> で再びデータを読み込むとき、UTF8 フラグを復元するべきだという
346346苦情が来ることがあります。
347347しかし、実際にはフラグを見るべきではないですし、Data::Dumper がこの規則を
348348破っていることを示すものは何もありません。
349349
350350=begin original
351351
352352Here's what happens: when Perl reads in a string literal, it sticks to 8 bit
353353encoding as long as it can. (But perhaps originally it was internally encoded
354354as UTF-8, when you dumped it.) When it has to give that up because other
355355characters are added to the text string, it silently upgrades the string to
356356UTF-8.
357357
358358=end original
359359
360360起きているのは以下のようなことです: Perl が文字列リテラルを読み込むとき、
361361可能な限り長く 8 ビットエンコーディングにこだわります。
362362(しかしおそらく、これをダンプしたときには内部では UTF-8 でエンコード
363363されていました。)
364364それ以外の文字をテキスト文字列に追加するためにこれを諦めなければならない
365365とき、Perl は暗黙のうちに文字列を UTF-8 に昇格させます。
366366
367367=begin original
368368
369369If you properly encode your strings for output, none of this is of your
370370concern, and you can just C<eval> dumped data as always.
371371
372372=end original
373373
374374出力用の文字列を適切にエンコードしていれば、これについてあなたは何も
375375心配することはなく、いつも通りにダンプしたデータを C<eval> できます。
376376
377377=head2 Why do regex character classes sometimes match only in the ASCII range?
378378
379379(なぜ正規表現文字クラスは時々 ASCII の範囲にしかマッチしないの?)
380380
381=head2 Why do some characters not uppercase or lowercase correctly?
382
383(なぜいくつかの文字は正しく大文字や小文字にならないの?)
384
381385=begin original
382386
383Starting in Perl 5.14 (and partially in Perl 5.12), just put a
387It seemed like a good idea at the time, to keep the semantics the same for
384C<use feature 'unicode_strings'> near the beginning of your program.
388standard strings, when Perl got Unicode support. While it might be repaired
385Within its lexical scope you shouldn't have this problem. It also is
389in the future, we now have to deal with the fact that Perl treats equal
386automatically enabled under C<use feature ':5.12'> or C<use v5.12> or
390strings differently, depending on the internal state.
387using C<-E> on the command line for Perl 5.12 or higher.
388391
389392=end original
390393
391Perl 5.14 から (そして部分的に Perl 5.12 から、) 単プログラムの先頭付近に
394Perl Unicode 対応なった時点では、これは標準文字列と同じ意味論を
392C<use feature 'unicode_strings'> を書てください。
395維持するのにいい考えだと思われました
393のレキシカルスコープ内でこの問題は発生しないはずす。
396一方、将来修正されるかもない、Perl が同じ文字列を内部状態に
394これはまた C<use feature ':5.12'> または C<use v5.12> が有効か、Perl 5.12
397よって異なる扱いをするという事実に対応する必要が出てき
395以降でコマンドラインで C<-E> を使っていると自動的に有効になります。
396398
397399=begin original
398400
399The rationale for requiring this is to not break older programs that
401Affected are C<uc>, C<lc>, C<ucfirst>, C<lcfirst>, C<\U>, C<\L>, C<\u>, C<\l>,
400rely on the way things worked before Unicode came along. Those older
402C<\d>, C<\s>, C<\w>, C<\D>, C<\S>, C<\W>, C</.../i>, C<(?i:...)>,
401programs knew only about the ASCII character set, and so may not work
403C</[[:posix:]]/>, and C<quotemeta> (though this last should not cause any real
402properly for additional characters. When a string is encoded in UTF-8,
404problems).
403Perl assumes that the program is prepared to deal with Unicode, but when
404the string isn't, Perl assumes that only ASCII
405is wanted, and so those characters that are not ASCII
406characters aren't recognized as to what they would be in Unicode.
407C<use feature 'unicode_strings'> tells Perl to treat all characters as
408Unicode, whether the string is encoded in UTF-8 or not, thus avoiding
409the problem.
410405
411406=end original
412407
413これが必要な理論的根拠、Unicode がやってくる前に動作する方法に
408影響を受けるの C<uc>, C<lc>, C<ucfirst>, C<lcfirst>, C<\U>, C<\L>, C<\u>, C<\l>,
414依存している古いプログラムを壊さないことです。
409C<\d>, C<\s>, C<\w>, C<\D>, C<\S>, C<\W>, C</.../i>, C<(?i:...)>,
415このような古いプログラムは ASCII 文字集合みを知っているで、追加
410C</[[:posix:]]/>, C<quotemeta> です (しかし最後は実際には何問題も
416文字については正しく動作しないかも知れません
411起こさないはずです)
417Perl はプログラムが Unicode を扱えるように準備されていると仮定しますが、
418文字列がそうでなかった場合、Perl は (EBCDIC プラットフォームでなければ)
419ASCII のみが求められていると仮定するので、非 ASCII 文字は Unicode に
420するべきものとして認識しません。
421C<use feature 'unicode_strings'> は Perl に、文字が UTF-8 で
422エンコードされているかどうかにかかわらず全ての文字を Unicode として
423扱うように知らせて、この問題を回避します。
424412
425413=begin original
426414
427However, on earlier Perls, or if you pass strings to subroutines outside
415To force Unicode semantics, you can upgrade the internal representation to
428the feature's scope, you can force Unicode rules by changing the
416by doing C<utf8::upgrade($string)>. This can be used
429encoding to UTF-8 by doing C<utf8::upgrade($string)>. This can be used
430417safely on any string, as it checks and does not change strings that have
431418already been upgraded.
432419
433420=end original
434421
435しかし、以前の Perl であっ、この機能のスコープの外側のサブルーチンに
422Unicode の意味論を強制するめにC<utf8::upgrade($string)> とするとで
436文字列渡した場合、C<utf8::upgrade($string)> とすることエンコーディングを
423内部表現昇格きます。
437UTF-8 にすることで Unicode の規則を強制できます。
438424これは既に昇格している文字列は変更しないので、どのような文字列に対しても
439425安全に用いることができます。
440426
441427=begin original
442428
443429For a more detailed discussion, see L<Unicode::Semantics> on CPAN.
444430
445431=end original
446432
447433さらなる詳細な議論については、CPAN の L<Unicode::Semantics> を
448434参照してください。
449435
450=head2 Why do some characters not uppercase or lowercase correctly?
451
452(なぜいくつかの文字は正しく大文字や小文字にならないの?)
453
454=begin original
455
456See the answer to the previous question.
457
458=end original
459
460前述の質問の答えを参照してください。
461
462436=head2 How can I determine if a string is a text string or a binary string?
463437
464438(文字列がテキスト文字列かバイナリ文字列かを決定するには?)
465439
466440=begin original
467441
468442You can't. Some use the UTF8 flag for this, but that's misuse, and makes well
469443behaved modules like Data::Dumper look bad. The flag is useless for this
470444purpose, because it's off when an 8 bit encoding (by default ISO-8859-1) is
471445used to store the string.
472446
473447=end original
474448
475449それはできません。
476450このために UTF8 フラグを使う人もいますが、これは誤用で、Data::Dumper のように
477451正しく振る舞うモジュールをおかしくします。
478452このフラグはこの目的のためには使えません; なぜなら文字列の保管に 8 ビット
479453エンコーディングが使われている場合 (デフォルトでは ISO-8859-1 です)、
480454オフだからです。
481455
482456=begin original
483457
484458This is something you, the programmer, has to keep track of; sorry. You could
485459consider adopting a kind of "Hungarian notation" to help with this.
486460
487461=end original
488462
489463把握しておく必要があるプログラマに言えることはこれです; ごめんなさい。
490464これを助けるために、「ハンガリアン記法」のようなものの採用を
491465検討することもできます。
492466
493467=head2 How do I convert from encoding FOO to encoding BAR?
494468
495469(エンコーディング FOO から エンコーディング BAR に変換するには?)
496470
497471=begin original
498472
499473By first converting the FOO-encoded byte string to a text string, and then the
500474text string to a BAR-encoded byte string:
501475
502476=end original
503477
504478まず FOO でエンコードされたバイト文字列をテキスト文字列に変化し、
505479それからテキスト文字列を BAR エンコードされたバイト文字列に変換します:
506480
507481 my $text_string = decode('FOO', $foo_string);
508482 my $bar_string = encode('BAR', $text_string);
509483
510484=begin original
511485
512486or by skipping the text string part, and going directly from one binary
513487encoding to the other:
514488
515489=end original
516490
517491あるいは、テキスト文字列の部分を飛ばして、あるバイナリエンコーディングから
518492他のものへ直接変換します:
519493
520494 use Encode qw(from_to);
521495 from_to($string, 'FOO', 'BAR'); # changes contents of $string
522496
523497=begin original
524498
525499or by letting automatic decoding and encoding do all the work:
526500
527501=end original
528502
529503あるいは、自動でデコードとエンコードをさせることで全ての作業を行います:
530504
531505 open my $foofh, '<:encoding(FOO)', 'example.foo.txt';
532506 open my $barfh, '>:encoding(BAR)', 'example.bar.txt';
533507 print { $barfh } $_ while <$foofh>;
534508
535509=head2 What are C<decode_utf8> and C<encode_utf8>?
536510
537511(C<decode_utf8> と C<encode_utf8> って何?)
538512
539513=begin original
540514
541515These are alternate syntaxes for C<decode('utf8', ...)> and C<encode('utf8',
542...)>. Do not use these functions for data exchange. Instead use
516...)>.
543C<decode('UTF-8', ...)> and C<encode('UTF-8', ...)>; see
544L</What's the difference between UTF-8 and utf8?> below.
545517
546518=end original
547519
548520これらは C<decode('utf8', ...)> および C<encode('utf8', ...)> のもう一つの
549521文法です。
550これらの関数をデータ交換に使わないでください。
551代わりに C<decode('UTF-8', ...)> と C<encode('UTF-8', ...)> を使ってください;
552後述する L</What's the difference between UTF-8 and utf8?> を
553参照してください。
554522
555523=head2 What is a "wide character"?
556524
557525(「ワイド文字」って何?)
558526
559527=begin original
560528
561This is a term used for characters occupying more than one byte.
529This is a term used both for characters with an ordinal value greater than 127,
530characters with an ordinal value greater than 255, or any character occupying
531than one byte, depending on the context.
562532
563533=end original
564534
565これは、1 バイトで収まらない文字う意味で使われる用語です。
535これは文脈に依存して 127 より大き序数を持つ文字、255 より大き序数を
536持つ文字、1 バイトで収まらない文字、のいずれかの意味で使われる用語です。
566537
567538=begin original
568539
569The Perl warning "Wide character in ..." is caused by such a character.
540The Perl warning "Wide character in ..." is caused by a character with an
570With no specified encoding layer, Perl tries to
541ordinal value greater than 255. With no specified encoding layer, Perl tries to
571fit things into a single byte. When it can't, it
542fit things in ISO-8859-1 for backward compatibility reasons. When it can't, it
572emits this warning (if warnings are enabled), and uses UTF-8 encoded data
543emits this warning (if warnings are enabled), and outputs UTF-8 encoded data
573544instead.
574545
575546=end original
576547
577Perl の警告 "Wide character in ..." はそのうな文字によって引き起こされます。
548Perl の警告 "Wide character in ..." は 255 り大きい序数を持つ文字によって
578エンコーディング層が指定されていない場合、Perl はそれを単一のバイトに
549引き起こされます。
579納めうとします。
550エンコーディング層が指定されていない場合、Perl は過去互換性の理由にって
551文字を ISO-8859-1 に合わせようとします。
580552これができないと、(警告が有効なら)この警告が出力され、代わりに UTF-8 で
581エンコードされたデータを使います。
553エンコードされたデータが出力されます。
582554
583555=begin original
584556
585557To avoid this warning and to avoid having different output encodings in a single
586558stream, always specify an encoding explicitly, for example with a PerlIO layer:
587559
588560=end original
589561
590562この警告を回避し、一つのストリームに異なった出力エンコーディングが
591563出力されることを回避するには、常に明示的にエンコーディングを指定してください;
592564例えば PerlIO 層を使って:
593565
594566 binmode STDOUT, ":encoding(UTF-8)";
595567
596568=head1 INTERNALS
597569
598570(内部構造)
599571
600572=head2 What is "the UTF8 flag"?
601573
602574(「UTF8 フラグ」って何?)
603575
604576=begin original
605577
606578Please, unless you're hacking the internals, or debugging weirdness, don't
607579think about the UTF8 flag at all. That means that you very probably shouldn't
608580use C<is_utf8>, C<_utf8_on> or C<_utf8_off> at all.
609581
610582=end original
611583
612584内部をハックしようとしているか、変なものをデバッグしようとしているのでない
613585限り、どうか UTF8 フラグのことは一切考えないでください。
614586これは、まず間違いなく C<is_utf8>, C<_utf8_on>, C<_utf8_off> を
615587一切使うべきでないことを意味します。
616588
617589=begin original
618590
619591The UTF8 flag, also called SvUTF8, is an internal flag that indicates that the
620592current internal representation is UTF-8. Without the flag, it is assumed to be
621ISO-8859-1. Perl converts between these automatically. (Actually Perl usually
593ISO-8859-1. Perl converts between these automatically.
622assumes the representation is ASCII; see L</Why do regex character classes
623sometimes match only in the ASCII range?> above.)
624594
625595=end original
626596
627597UTF8 フラグ(SvUTF8 とも呼ばれます)は、現在の内部表現が UTF-8 であることを
628598示す内部フラグです。
629599このフラグがない場合、ISO-8859-1 と仮定します。
630600Perl はこれらを自動的に変換します。
631(実際のところ Perl は普通表現が ASCII であると仮定します; 上述の L</Why do
632regex character classes sometimes match only in the ASCII range?> を
633参照してください。)
634601
635602=begin original
636603
637604One of Perl's internal formats happens to be UTF-8. Unfortunately, Perl can't
638605keep a secret, so everyone knows about this. That is the source of much
639606confusion. It's better to pretend that the internal format is some unknown
640607encoding, and that you always have to encode and decode explicitly.
641608
642609=end original
643610
644611Perl の内部表現の一つはたまたま UTF-8 です。
645612残念ながら、Perl は秘密を守れないので、このことはみんな知っています。
646613これが多くの混乱の源です。
647614内部表現は何か分からないエンコーディングで、常に明示的にエンコードと
648615デコードが必要ということにしておいた方がよいです。
649616
650617=head2 What about the C<use bytes> pragma?
651618
652619(C<use bytes> プラグマって何?)
653620
654621=begin original
655622
656623Don't use it. It makes no sense to deal with bytes in a text string, and it
657624makes no sense to deal with characters in a byte string. Do the proper
658625conversions (by decoding/encoding), and things will work out well: you get
659626character counts for decoded data, and byte counts for encoded data.
660627
661628=end original
662629
663630これは使わないでください。
664631テキスト文字列をバイト単位で扱うことに意味はありませんし、
665632バイト文字列を文字単位で扱うことには意味はありません。
666633適切な変換(デコードかエンコード)を行えば、物事はうまくいきます:
667634デコードしたデータの文字数を得られますし、エンコードしたデータのバイト数を
668635得られます。
669636
670637=begin original
671638
672639C<use bytes> is usually a failed attempt to do something useful. Just forget
673640about it.
674641
675642=end original
676643
677644C<use bytes> は何か有用なことをしようとするためには間違った方法です。
678645これのことは単に忘れてください。
679646
680647=head2 What about the C<use encoding> pragma?
681648
682649(C<use encoding> プラグマって何?)
683650
684651=begin original
685652
686653Don't use it. Unfortunately, it assumes that the programmer's environment and
687654that of the user will use the same encoding. It will use the same encoding for
688655the source code and for STDIN and STDOUT. When a program is copied to another
689656machine, the source code does not change, but the STDIO environment might.
690657
691658=end original
692659
693660これは使わないでください。
694661残念ながら、これはプログラマの環境とユーザーの環境が同じであると仮定します。
695662これはソースコードと STDIN や STDOUT で同じエンコーディングを使います。
696663プログラムが他のマシンにコピーされると、ソースコードは変わりませんが、
697664STDIO 環境は変わるかもしれません。
698665
699666=begin original
700667
701668If you need non-ASCII characters in your source code, make it a UTF-8 encoded
702669file and C<use utf8>.
703670
704671=end original
705672
706673もしソースコードに非 ASCII 文字が必要なら、ファイルを UTF-8 で
707674エンコードして、C<use utf8> を使ってください。
708675
709676=begin original
710677
711678If you need to set the encoding for STDIN, STDOUT, and STDERR, for example
712679based on the user's locale, C<use open>.
713680
714681=end original
715682
716683もし STDIN, STDOUT, STDERR のエンコーディングを、例えばユーザーのロケールに
717684合わせてセットする必要があるなら、C<use open> してください。
718685
719686=head2 What is the difference between C<:encoding> and C<:utf8>?
720687
721688(C<:encoding> と C<:utf8> の違いは?)
722689
723690=begin original
724691
725692Because UTF-8 is one of Perl's internal formats, you can often just skip the
726693encoding or decoding step, and manipulate the UTF8 flag directly.
727694
728695=end original
729696
730697UTF-8 は Perl の内部形式のひとつなので、しばしばエンコードやデコードの
731698手順を省略して、UTF8 フラグを直接操作できます。
732699
733700=begin original
734701
735702Instead of C<:encoding(UTF-8)>, you can simply use C<:utf8>, which skips the
736703encoding step if the data was already represented as UTF8 internally. This is
737704widely accepted as good behavior when you're writing, but it can be dangerous
738705when reading, because it causes internal inconsistency when you have invalid
739706byte sequences. Using C<:utf8> for input can sometimes result in security
740707breaches, so please use C<:encoding(UTF-8)> instead.
741708
742709=end original
743710
744711C<:encoding(UTF-8)> の代わりに単に C<:utf8> を使うことで、もしデータが
745712内部で既に UTF8 で表現されていれば、エンコードの手順を省略します。
746713これは、書き込むときにはよい振る舞いであると広く受け入れられていますが、
747714読み込むときには危険があります; なぜなら不正なバイト列を受け取ると
748715内部矛盾を引き起こすからです。
749716入力に C<:utf8> を使うとセキュリティ侵害を引き起こす可能性があるので、
750717どうか代わりに C<:encoding(UTF-8)> を使ってください。
751718
752719=begin original
753720
754721Instead of C<decode> and C<encode>, you could use C<_utf8_on> and C<_utf8_off>,
755722but this is considered bad style. Especially C<_utf8_on> can be dangerous, for
756723the same reason that C<:utf8> can.
757724
758725=end original
759726
760727C<decode> と C<encode> の代わりに、C<_utf8_on> と C<_utf8_off> を
761728使えますが、これは悪いスタイルと考えられています。
762729特に C<_utf8_on> は、C<:utf8> と同じ理由で危険です。
763730
764731=begin original
765732
766There are some shortcuts for oneliners;
733There are some shortcuts for oneliners; see C<-C> in L<perlrun>.
767see L<-C|perlrun/-C [numberE<sol>list]> in L<perlrun>.
768734
769735=end original
770736
771一行野郎のための省略形があります; L<perlrun> の
737一行野郎のための省略形があります; L<perlrun> の C<-C> を参照してください。
772L<-C|perlrun/-C [numberE<sol>list]> を参照してください。
773738
774739=head2 What's the difference between C<UTF-8> and C<utf8>?
775740
776741(C<UTF-8> と C<utf8> の違いは?)
777742
778743=begin original
779744
780745C<UTF-8> is the official standard. C<utf8> is Perl's way of being liberal in
781746what it accepts. If you have to communicate with things that aren't so liberal,
782747you may want to consider using C<UTF-8>. If you have to communicate with things
783748that are too liberal, you may have to use C<utf8>. The full explanation is in
784L<Encode/"UTF-8 vs. utf8 vs. UTF8">.
749L<Encode>.
785750
786751=end original
787752
788753C<UTF-8> は公式な標準です。
789754C<utf8> は、何を受け入れるかに関して自由な Perl のやり方です。
790755もしそれほど自由でないものと対話する必要があるなら、
791756C<UTF-8> を使うことを考えたくなるかもしれません。
792757自由すぎるものと対話する必要があるなら、C<utf8> を
793758使わなければならないかもしれません。
794完全な説明は L<Encode/"UTF-8 vs. utf8 vs. UTF8"> にあります。
759完全な説明は L<Encode> にあります。
795760
796761=begin original
797762
798763C<UTF-8> is internally known as C<utf-8-strict>. The tutorial uses UTF-8
799764consistently, even where utf8 is actually used internally, because the
800765distinction can be hard to make, and is mostly irrelevant.
801766
802767=end original
803768
804769C<UTF-8> は内部では C<utf-8-strict> として知られます。
805770チュートリアルでは、たとえ内部では実際には utf8 が使われる場合でも
806771一貫して UTF-8 を使っています; なぜなら区別をつけるのは難しく、ほとんど
807772無意味だからです。
808773
809774=begin original
810775
811776For example, utf8 can be used for code points that don't exist in Unicode, like
8127779999999, but if you encode that to UTF-8, you get a substitution character (by
813778default; see L<Encode/"Handling Malformed Data"> for more ways of dealing with
814779this.)
815780
816781=end original
817782
818783例えば utf8 は、9999999 のような、Unicode に存在しない符号位置も使えますが、
819784これを UTF-8 でエンコードすると、代替文字を得ることになります(これは
820785デフォルトの場合です; これを扱う他の方法については
821786L<Encode/"Handling Malformed Data"> を参照してください。)
822787
823788=begin original
824789
825790Okay, if you insist: the "internal format" is utf8, not UTF-8. (When it's not
826791some other encoding.)
827792
828793=end original
829794
830795わかりました、どうしてもと言うのなら:「内部形式」は utf8 であって、
831796UTF-8 ではありません。
832797(もしその他のエンコーディングでないのなら。)
833798
834799=head2 I lost track; what encoding is the internal format really?
835800
836801(迷子になりました; 実際のところ内部形式のエンコーディングは何?)
837802
838803=begin original
839804
840805It's good that you lost track, because you shouldn't depend on the internal
841806format being any specific encoding. But since you asked: by default, the
842807internal format is either ISO-8859-1 (latin-1), or utf8, depending on the
843808history of the string. On EBCDIC platforms, this may be different even.
844809
845810=end original
846811
847812迷子になったのはよいことです; なぜなら内部形式が特定のエンコーディングで
848813あることに依存するべきではないからです。
849814しかし聞かれたので答えましょう: デフォルトでは、内部形式は
850815ISO-8859-1 (latin-1) か utf8 で、どちらになるかは文字列の歴史に
851816依存します。
852817EBCDIC プラットフォームでは、これは異なっているかもしれません。
853818
854819=begin original
855820
856821Perl knows how it stored the string internally, and will use that knowledge
857822when you C<encode>. In other words: don't try to find out what the internal
858823encoding for a certain string is, but instead just encode it into the encoding
859824that you want.
860825
861826=end original
862827
863828Perl は文字列が内部でどのように保管されているかを知っていて、この知識を
864829C<エンコードする> ときに使います。
865830言い換えると: 特定の文字列の内部エンコーディングが何かを
866831調べようとしてはいけません; 代わりに、単に望みのエンコーディングに
867832エンコードしてください。
868833
869834=head1 AUTHOR
870835
871836Juerd Waalboer <#####@juerd.nl>
872837
873838=head1 SEE ALSO
874839
875840L<perlunicode>, L<perluniintro>, L<Encode>
876841
877842=begin meta
878843
879Translate: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-)
844Translate: Kentaro Shirakata <argrath@ub32.org> (5.10.0-)
880845Status: completed
881846
882847=end meta