perlunifaq > 5.38.0 との差分

perlunifaq 5.38.0 と 5.24.1 の差分

11
2=encoding utf8
2=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
4040Perl has an abstracted interface for all supported character encodings, so this
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.
7676The tutorial and FAQ assume the latest release.
7777
7878=end original
7979
8080うーん、もし可能なら、最新にアップグレードしてください; 但し、確実に
8181C<5.8.1> 以降にはしてください。
8282チュートリアルと FAQ は最新リリースを仮定しています。
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
184It depends on what you output and how you output it.
184Your text string will be sent using the bytes in Perl's internal format. In
185some cases, Perl will warn you that you're doing something wrong, with a
186friendly warning:
185187
186188=end original
187189
188どうやって出力するかによります。
190テキスト文字列は Perl の内部形式のバイト列使って送信されます。
191いくつかの場合では、Perl は何かが間違っていることを、親切なメッセージで
192警告します:
189193
190=head3 Output via a filehandle
194 Wide character in print at example.pl line 2.
191195
192=over
193
194196=begin original
195197
196=item * If the string's characters are all code point 255 or lower, Perl
197outputs bytes that match those code points. This is what happens with encoded
198strings. It can also, though, happen with unencoded strings that happen to be
199all code point 255 or lower.
200
201=end original
202
203=item * 文字列の文字の符号位置が全て 255 以下の場合、Perl は
204その符号位置に一致するバイトを出力します。
205これはエンコードされた文字列の時に起きることです。
206しかし、たまたま全ての符号位置が 255 以下のエンコードされていない文字列でも
207起きます。
208
209=begin original
210
211=item * Otherwise, Perl outputs the string encoded as UTF-8. This only happens
212with strings you neglected to encode. Since that should not happen, Perl also
213throws a "wide character" warning in this case.
214
215=end original
216
217=item * さもなければ、Perl は UTF-8 としてエンコードされた文字列を出力します。
218これはあなたがエンコードを拒否した文字列にのみ起きます。
219これは起きるべきではないので、Perl はこの場合
220"wide character" 警告も投げます。
221
222=back
223
224=head3 Other output mechanisms (e.g., C<exec>, C<chdir>, ..)
225
226(その他の出力機構 (例えば C<exec>, C<chdir>, ..))
227
228=begin original
229
230Your text string will be sent using the bytes in Perl's internal format.
231
232=end original
233
234テキスト文字列は Perl の内部フォーマットのバイトを使って送ります。
235
236=begin original
237
238198Because the internal format is often UTF-8, these bugs are hard to spot,
239199because UTF-8 is usually the encoding you wanted! But don't be lazy, and don't
240200use the fact that Perl's internal format is UTF-8 to your advantage. Encode
241201explicitly to avoid weird bugs, and to show to maintenance programmers that you
242202thought this through.
243203
244204=end original
245205
246206内部形式はしばしば UTF-8 なので、このバグは発見しにくいです; なぜなら
247207あなたがほしいのは普通 UTF-8 だからです!
248208しかし、手を抜かないでください; そして Perl の内部形式が UTF-8 であることを
249209利用しようとしないでください。
250210奇妙なバグを防ぐため、そして保守プログラマに対してあなたが何を考えたかを
251211示すために、明示的にエンコードしてください。
252212
253213=head2 Is there a way to automatically decode or encode?
254214
255215(自動的にデコードやエンコードする方法はある?)
256216
257217=begin original
258218
259219If all data that comes from a certain handle is encoded in exactly the same
260220way, you can tell the PerlIO system to automatically decode everything, with
261221the C<encoding> layer. If you do this, you can't accidentally forget to decode
262222or encode anymore, on things that use the layered handle.
263223
264224=end original
265225
266226もし、あるハンドルから来る全てのデータが正確に同じ方法で
267227エンコードされているなら、C<encoding> 層を使って、 PerlIO システムに自動的に
268228全てをデコードするように伝えることができます。
269229これを行えば、この層のハンドルを使っている限り、うっかりデコードや
270230エンコードを忘れることはありません。
271231
272232=begin original
273233
274234You can provide this layer when C<open>ing the file:
275235
276236=end original
277237
278238ファイルを C<open> するときにこの層を指定することができます:
279239
280240 open my $fh, '>:encoding(UTF-8)', $filename; # auto encoding on write
281241 open my $fh, '<:encoding(UTF-8)', $filename; # auto decoding on read
282242
283243=begin original
284244
285245Or if you already have an open filehandle:
286246
287247=end original
288248
289249あるいは既にオープンしているファイルハンドルがあるなら:
290250
291251 binmode $fh, ':encoding(UTF-8)';
292252
293253=begin original
294254
295255Some database drivers for DBI can also automatically encode and decode, but
296256that is sometimes limited to the UTF-8 encoding.
297257
298258=end original
299259
300260DBI のデータベースドライバのいくつかも、エンコードとデコードを自動的に
301261行いますが、ときどきこれは UTF-8 エンコーディングに制限されています。
302262
303263=head2 What if I don't know which encoding was used?
304264
305265(どのエンコーディングが使われているかわからないときは?)
306266
307267=begin original
308268
309269Do whatever you can to find out, and if you have to: guess. (Don't forget to
310270document your guess with a comment.)
311271
312272=end original
313273
314274なんとかして見つけるか、もし必要なら、推測してください。
315275(どう推測したかをコメントとして文書化するのを忘れないでください。)
316276
317277=begin original
318278
319279You could open the document in a web browser, and change the character set or
320280character encoding until you can visually confirm that all characters look the
321281way they should.
322282
323283=end original
324284
325285ドキュメントを web ブラウザで開いて、全ての文字があるべき形であることを
326286視覚的に確認できるまで文字集合や文字エンコーディングを変更する方法も
327287あります。
328288
329289=begin original
330290
331291There is no way to reliably detect the encoding automatically, so if people
332292keep sending you data without charset indication, you may have to educate them.
333293
334294=end original
335295
336296エンコーディングを自動的に検出するための信頼性のある方法はないので、
337297もし人々があなたに文字集合の指示なしにデータを送り続けるなら、彼らを
338298教育する必要があるかもしれません。
339299
340300=head2 Can I use Unicode in my Perl sources?
341301
342302(Perl のソースコードに Unicode は使える?)
343303
344304=begin original
345305
346306Yes, you can! If your sources are UTF-8 encoded, you can indicate that with the
347307C<use utf8> pragma.
348308
349309=end original
350310
351311はい、できます!
352312ソースコードが UTF-8 でエンコードされているなら、C<use utf8> プラグマを
353313使ってそれを示すことができます。
354314
355315 use utf8;
356316
357317=begin original
358318
359319This doesn't do anything to your input, or to your output. It only influences
360320the way your sources are read. You can use Unicode in string literals, in
361321identifiers (but they still have to be "word characters" according to C<\w>),
362322and even in custom delimiters.
363323
364324=end original
365325
366326これは入出力に対しては何も行いません。
367327ソースを読み込む方法のみに影響を与えます。
368328文字列リテラル、識別子(しかし C<\w> に従った「単語文字」である必要が
369329あります)、そして独自デリミタにすら Unicode が使えます。
370330
371331=head2 Data::Dumper doesn't restore the UTF8 flag; is it broken?
372332
373333(Data::Dumper は UTF8 フラグを復元しません; これは壊れてるの?)
374334
375335=begin original
376336
377337No, Data::Dumper's Unicode abilities are as they should be. There have been
378338some complaints that it should restore the UTF8 flag when the data is read
379339again with C<eval>. However, you should really not look at the flag, and
380340nothing indicates that Data::Dumper should break this rule.
381341
382342=end original
383343
384344いいえ、Data::Dumper の Unicode 能力は、あるべき形であります。
385345C<eval> で再びデータを読み込むとき、UTF8 フラグを復元するべきだという
386346苦情が来ることがあります。
387347しかし、実際にはフラグを見るべきではないですし、Data::Dumper がこの規則を
388348破っていることを示すものは何もありません。
389349
390350=begin original
391351
392352Here's what happens: when Perl reads in a string literal, it sticks to 8 bit
393353encoding as long as it can. (But perhaps originally it was internally encoded
394354as UTF-8, when you dumped it.) When it has to give that up because other
395355characters are added to the text string, it silently upgrades the string to
396356UTF-8.
397357
398358=end original
399359
400360起きているのは以下のようなことです: Perl が文字列リテラルを読み込むとき、
401361可能な限り長く 8 ビットエンコーディングにこだわります。
402362(しかしおそらく、これをダンプしたときには内部では UTF-8 でエンコード
403363されていました。)
404364それ以外の文字をテキスト文字列に追加するためにこれを諦めなければならない
405365とき、Perl は暗黙のうちに文字列を UTF-8 に昇格させます。
406366
407367=begin original
408368
409369If you properly encode your strings for output, none of this is of your
410370concern, and you can just C<eval> dumped data as always.
411371
412372=end original
413373
414374出力用の文字列を適切にエンコードしていれば、これについてあなたは何も
415375心配することはなく、いつも通りにダンプしたデータを C<eval> できます。
416376
417377=head2 Why do regex character classes sometimes match only in the ASCII range?
418378
419379(なぜ正規表現文字クラスは時々 ASCII の範囲にしかマッチしないの?)
420380
421381=begin original
422382
423383Starting in Perl 5.14 (and partially in Perl 5.12), just put a
424384C<use feature 'unicode_strings'> near the beginning of your program.
425385Within its lexical scope you shouldn't have this problem. It also is
426386automatically enabled under C<use feature ':5.12'> or C<use v5.12> or
427387using C<-E> on the command line for Perl 5.12 or higher.
428388
429389=end original
430390
431391Perl 5.14 から (そして部分的に Perl 5.12 から、) 単にプログラムの先頭付近に
432392C<use feature 'unicode_strings'> を書いてください。
433393このレキシカルスコープ内ではこの問題は発生しないはずです。
434394これはまた C<use feature ':5.12'> または C<use v5.12> が有効か、Perl 5.12
435395以降でコマンドラインで C<-E> を使っていると自動的に有効になります。
436396
437397=begin original
438398
439399The rationale for requiring this is to not break older programs that
440400rely on the way things worked before Unicode came along. Those older
441401programs knew only about the ASCII character set, and so may not work
442402properly for additional characters. When a string is encoded in UTF-8,
443403Perl assumes that the program is prepared to deal with Unicode, but when
444404the string isn't, Perl assumes that only ASCII
445405is wanted, and so those characters that are not ASCII
446406characters aren't recognized as to what they would be in Unicode.
447407C<use feature 'unicode_strings'> tells Perl to treat all characters as
448408Unicode, whether the string is encoded in UTF-8 or not, thus avoiding
449409the problem.
450410
451411=end original
452412
453413これが必要な理論的根拠は、Unicode がやってくる前に動作する方法に
454414依存している古いプログラムを壊さないことです。
455415このような古いプログラムは ASCII 文字集合のみを知っているので、追加の
456416文字については正しく動作しないかも知れません。
457417Perl はプログラムが Unicode を扱えるように準備されていると仮定しますが、
458418文字列がそうでなかった場合、Perl は (EBCDIC プラットフォームでなければ)
459419ASCII のみが求められていると仮定するので、非 ASCII 文字は Unicode に
460420するべきものとして認識しません。
461421C<use feature 'unicode_strings'> は Perl に、文字が UTF-8 で
462422エンコードされているかどうかにかかわらず全ての文字を Unicode として
463423扱うように知らせて、この問題を回避します。
464424
465425=begin original
466426
467427However, on earlier Perls, or if you pass strings to subroutines outside
468428the feature's scope, you can force Unicode rules by changing the
469429encoding to UTF-8 by doing C<utf8::upgrade($string)>. This can be used
470430safely on any string, as it checks and does not change strings that have
471431already been upgraded.
472432
473433=end original
474434
475435しかし、以前の Perl であったり、この機能のスコープの外側のサブルーチンに
476436文字列を渡した場合、C<utf8::upgrade($string)> とすることでエンコーディングを
477437UTF-8 にすることで Unicode の規則を強制できます。
478438これは既に昇格している文字列は変更しないので、どのような文字列に対しても
479439安全に用いることができます。
480440
481441=begin original
482442
483443For a more detailed discussion, see L<Unicode::Semantics> on CPAN.
484444
485445=end original
486446
487447さらなる詳細な議論については、CPAN の L<Unicode::Semantics> を
488448参照してください。
489449
490450=head2 Why do some characters not uppercase or lowercase correctly?
491451
492452(なぜいくつかの文字は正しく大文字や小文字にならないの?)
493453
494454=begin original
495455
496456See the answer to the previous question.
497457
498458=end original
499459
500460前述の質問の答えを参照してください。
501461
502462=head2 How can I determine if a string is a text string or a binary string?
503463
504464(文字列がテキスト文字列かバイナリ文字列かを決定するには?)
505465
506466=begin original
507467
508468You can't. Some use the UTF8 flag for this, but that's misuse, and makes well
509469behaved modules like Data::Dumper look bad. The flag is useless for this
510470purpose, because it's off when an 8 bit encoding (by default ISO-8859-1) is
511471used to store the string.
512472
513473=end original
514474
515475それはできません。
516476このために UTF8 フラグを使う人もいますが、これは誤用で、Data::Dumper のように
517477正しく振る舞うモジュールをおかしくします。
518478このフラグはこの目的のためには使えません; なぜなら文字列の保管に 8 ビット
519479エンコーディングが使われている場合 (デフォルトでは ISO-8859-1 です)、
520480オフだからです。
521481
522482=begin original
523483
524484This is something you, the programmer, has to keep track of; sorry. You could
525485consider adopting a kind of "Hungarian notation" to help with this.
526486
527487=end original
528488
529489把握しておく必要があるプログラマに言えることはこれです; ごめんなさい。
530490これを助けるために、「ハンガリアン記法」のようなものの採用を
531491検討することもできます。
532492
533493=head2 How do I convert from encoding FOO to encoding BAR?
534494
535495(エンコーディング FOO から エンコーディング BAR に変換するには?)
536496
537497=begin original
538498
539499By first converting the FOO-encoded byte string to a text string, and then the
540500text string to a BAR-encoded byte string:
541501
542502=end original
543503
544504まず FOO でエンコードされたバイト文字列をテキスト文字列に変化し、
545505それからテキスト文字列を BAR エンコードされたバイト文字列に変換します:
546506
547507 my $text_string = decode('FOO', $foo_string);
548508 my $bar_string = encode('BAR', $text_string);
549509
550510=begin original
551511
552512or by skipping the text string part, and going directly from one binary
553513encoding to the other:
554514
555515=end original
556516
557517あるいは、テキスト文字列の部分を飛ばして、あるバイナリエンコーディングから
558518他のものへ直接変換します:
559519
560520 use Encode qw(from_to);
561521 from_to($string, 'FOO', 'BAR'); # changes contents of $string
562522
563523=begin original
564524
565525or by letting automatic decoding and encoding do all the work:
566526
567527=end original
568528
569529あるいは、自動でデコードとエンコードをさせることで全ての作業を行います:
570530
571531 open my $foofh, '<:encoding(FOO)', 'example.foo.txt';
572532 open my $barfh, '>:encoding(BAR)', 'example.bar.txt';
573533 print { $barfh } $_ while <$foofh>;
574534
575535=head2 What are C<decode_utf8> and C<encode_utf8>?
576536
577537(C<decode_utf8> と C<encode_utf8> って何?)
578538
579539=begin original
580540
581541These are alternate syntaxes for C<decode('utf8', ...)> and C<encode('utf8',
582...)>. Do not use these functions for data exchange. Instead use
542...)>.
583C<decode('UTF-8', ...)> and C<encode('UTF-8', ...)>; see
584L</What's the difference between UTF-8 and utf8?> below.
585543
586544=end original
587545
588546これらは C<decode('utf8', ...)> および C<encode('utf8', ...)> のもう一つの
589547文法です。
590これらの関数をデータ交換に使わないでください。
591代わりに C<decode('UTF-8', ...)> と C<encode('UTF-8', ...)> を使ってください;
592後述する L</What's the difference between UTF-8 and utf8?> を
593参照してください。
594548
595549=head2 What is a "wide character"?
596550
597551(「ワイド文字」って何?)
598552
599553=begin original
600554
601555This is a term used for characters occupying more than one byte.
602556
603557=end original
604558
605559これは、1 バイトで収まらない文字という意味で使われる用語です。
606560
607561=begin original
608562
609563The Perl warning "Wide character in ..." is caused by such a character.
610564With no specified encoding layer, Perl tries to
611565fit things into a single byte. When it can't, it
612566emits this warning (if warnings are enabled), and uses UTF-8 encoded data
613567instead.
614568
615569=end original
616570
617571Perl の警告 "Wide character in ..." はそのような文字によって引き起こされます。
618572エンコーディング層が指定されていない場合、Perl はそれを単一のバイトに
619573納めようとします。
620574これができないと、(警告が有効なら)この警告が出力され、代わりに UTF-8 で
621575エンコードされたデータを使います。
622576
623577=begin original
624578
625579To avoid this warning and to avoid having different output encodings in a single
626580stream, always specify an encoding explicitly, for example with a PerlIO layer:
627581
628582=end original
629583
630584この警告を回避し、一つのストリームに異なった出力エンコーディングが
631585出力されることを回避するには、常に明示的にエンコーディングを指定してください;
632586例えば PerlIO 層を使って:
633587
634588 binmode STDOUT, ":encoding(UTF-8)";
635589
636590=head1 INTERNALS
637591
638592(内部構造)
639593
640594=head2 What is "the UTF8 flag"?
641595
642596(「UTF8 フラグ」って何?)
643597
644598=begin original
645599
646600Please, unless you're hacking the internals, or debugging weirdness, don't
647601think about the UTF8 flag at all. That means that you very probably shouldn't
648602use C<is_utf8>, C<_utf8_on> or C<_utf8_off> at all.
649603
650604=end original
651605
652606内部をハックしようとしているか、変なものをデバッグしようとしているのでない
653607限り、どうか UTF8 フラグのことは一切考えないでください。
654608これは、まず間違いなく C<is_utf8>, C<_utf8_on>, C<_utf8_off> を
655609一切使うべきでないことを意味します。
656610
657611=begin original
658612
659613The UTF8 flag, also called SvUTF8, is an internal flag that indicates that the
660614current internal representation is UTF-8. Without the flag, it is assumed to be
661615ISO-8859-1. Perl converts between these automatically. (Actually Perl usually
662616assumes the representation is ASCII; see L</Why do regex character classes
663617sometimes match only in the ASCII range?> above.)
664618
665619=end original
666620
667621UTF8 フラグ(SvUTF8 とも呼ばれます)は、現在の内部表現が UTF-8 であることを
668622示す内部フラグです。
669623このフラグがない場合、ISO-8859-1 と仮定します。
670624Perl はこれらを自動的に変換します。
671625(実際のところ Perl は普通表現が ASCII であると仮定します; 上述の L</Why do
672626regex character classes sometimes match only in the ASCII range?> を
673627参照してください。)
674628
675629=begin original
676630
677631One of Perl's internal formats happens to be UTF-8. Unfortunately, Perl can't
678632keep a secret, so everyone knows about this. That is the source of much
679633confusion. It's better to pretend that the internal format is some unknown
680634encoding, and that you always have to encode and decode explicitly.
681635
682636=end original
683637
684638Perl の内部表現の一つはたまたま UTF-8 です。
685639残念ながら、Perl は秘密を守れないので、このことはみんな知っています。
686640これが多くの混乱の源です。
687641内部表現は何か分からないエンコーディングで、常に明示的にエンコードと
688642デコードが必要ということにしておいた方がよいです。
689643
690644=head2 What about the C<use bytes> pragma?
691645
692646(C<use bytes> プラグマって何?)
693647
694648=begin original
695649
696650Don't use it. It makes no sense to deal with bytes in a text string, and it
697651makes no sense to deal with characters in a byte string. Do the proper
698652conversions (by decoding/encoding), and things will work out well: you get
699653character counts for decoded data, and byte counts for encoded data.
700654
701655=end original
702656
703657これは使わないでください。
704658テキスト文字列をバイト単位で扱うことに意味はありませんし、
705659バイト文字列を文字単位で扱うことには意味はありません。
706660適切な変換(デコードかエンコード)を行えば、物事はうまくいきます:
707661デコードしたデータの文字数を得られますし、エンコードしたデータのバイト数を
708662得られます。
709663
710664=begin original
711665
712666C<use bytes> is usually a failed attempt to do something useful. Just forget
713667about it.
714668
715669=end original
716670
717671C<use bytes> は何か有用なことをしようとするためには間違った方法です。
718672これのことは単に忘れてください。
719673
720674=head2 What about the C<use encoding> pragma?
721675
722676(C<use encoding> プラグマって何?)
723677
724678=begin original
725679
726680Don't use it. Unfortunately, it assumes that the programmer's environment and
727681that of the user will use the same encoding. It will use the same encoding for
728682the source code and for STDIN and STDOUT. When a program is copied to another
729683machine, the source code does not change, but the STDIO environment might.
730684
731685=end original
732686
733687これは使わないでください。
734688残念ながら、これはプログラマの環境とユーザーの環境が同じであると仮定します。
735689これはソースコードと STDIN や STDOUT で同じエンコーディングを使います。
736690プログラムが他のマシンにコピーされると、ソースコードは変わりませんが、
737691STDIO 環境は変わるかもしれません。
738692
739693=begin original
740694
741695If you need non-ASCII characters in your source code, make it a UTF-8 encoded
742696file and C<use utf8>.
743697
744698=end original
745699
746700もしソースコードに非 ASCII 文字が必要なら、ファイルを UTF-8 で
747701エンコードして、C<use utf8> を使ってください。
748702
749703=begin original
750704
751705If you need to set the encoding for STDIN, STDOUT, and STDERR, for example
752706based on the user's locale, C<use open>.
753707
754708=end original
755709
756710もし STDIN, STDOUT, STDERR のエンコーディングを、例えばユーザーのロケールに
757711合わせてセットする必要があるなら、C<use open> してください。
758712
759713=head2 What is the difference between C<:encoding> and C<:utf8>?
760714
761715(C<:encoding> と C<:utf8> の違いは?)
762716
763717=begin original
764718
765719Because UTF-8 is one of Perl's internal formats, you can often just skip the
766720encoding or decoding step, and manipulate the UTF8 flag directly.
767721
768722=end original
769723
770724UTF-8 は Perl の内部形式のひとつなので、しばしばエンコードやデコードの
771725手順を省略して、UTF8 フラグを直接操作できます。
772726
773727=begin original
774728
775729Instead of C<:encoding(UTF-8)>, you can simply use C<:utf8>, which skips the
776730encoding step if the data was already represented as UTF8 internally. This is
777731widely accepted as good behavior when you're writing, but it can be dangerous
778732when reading, because it causes internal inconsistency when you have invalid
779733byte sequences. Using C<:utf8> for input can sometimes result in security
780734breaches, so please use C<:encoding(UTF-8)> instead.
781735
782736=end original
783737
784738C<:encoding(UTF-8)> の代わりに単に C<:utf8> を使うことで、もしデータが
785739内部で既に UTF8 で表現されていれば、エンコードの手順を省略します。
786740これは、書き込むときにはよい振る舞いであると広く受け入れられていますが、
787741読み込むときには危険があります; なぜなら不正なバイト列を受け取ると
788742内部矛盾を引き起こすからです。
789743入力に C<:utf8> を使うとセキュリティ侵害を引き起こす可能性があるので、
790744どうか代わりに C<:encoding(UTF-8)> を使ってください。
791745
792746=begin original
793747
794748Instead of C<decode> and C<encode>, you could use C<_utf8_on> and C<_utf8_off>,
795749but this is considered bad style. Especially C<_utf8_on> can be dangerous, for
796750the same reason that C<:utf8> can.
797751
798752=end original
799753
800754C<decode> と C<encode> の代わりに、C<_utf8_on> と C<_utf8_off> を
801755使えますが、これは悪いスタイルと考えられています。
802756特に C<_utf8_on> は、C<:utf8> と同じ理由で危険です。
803757
804758=begin original
805759
806760There are some shortcuts for oneliners;
807see L<-C in perlrun|perlrun/-C [numberE<sol>list]>.
761see L<-C|perlrun/-C [numberE<sol>list]> in L<perlrun>.
808762
809763=end original
810764
811765一行野郎のための省略形があります; L<perlrun> の
812L<-C in perlrun|perlrun/-C [numberE<sol>list]> を参照してください。
766L<-C|perlrun/-C [numberE<sol>list]> を参照してください。
813767
814768=head2 What's the difference between C<UTF-8> and C<utf8>?
815769
816770(C<UTF-8> と C<utf8> の違いは?)
817771
818772=begin original
819773
820774C<UTF-8> is the official standard. C<utf8> is Perl's way of being liberal in
821775what it accepts. If you have to communicate with things that aren't so liberal,
822776you may want to consider using C<UTF-8>. If you have to communicate with things
823777that are too liberal, you may have to use C<utf8>. The full explanation is in
824L<Encode/"UTF-8 vs. utf8 vs. UTF8">.
778L<Encode>.
825779
826780=end original
827781
828782C<UTF-8> は公式な標準です。
829783C<utf8> は、何を受け入れるかに関して自由な Perl のやり方です。
830784もしそれほど自由でないものと対話する必要があるなら、
831785C<UTF-8> を使うことを考えたくなるかもしれません。
832786自由すぎるものと対話する必要があるなら、C<utf8> を
833787使わなければならないかもしれません。
834完全な説明は L<Encode/"UTF-8 vs. utf8 vs. UTF8"> にあります。
788完全な説明は L<Encode> にあります。
835789
836790=begin original
837791
838792C<UTF-8> is internally known as C<utf-8-strict>. The tutorial uses UTF-8
839793consistently, even where utf8 is actually used internally, because the
840794distinction can be hard to make, and is mostly irrelevant.
841795
842796=end original
843797
844798C<UTF-8> は内部では C<utf-8-strict> として知られます。
845799チュートリアルでは、たとえ内部では実際には utf8 が使われる場合でも
846800一貫して UTF-8 を使っています; なぜなら区別をつけるのは難しく、ほとんど
847801無意味だからです。
848802
849803=begin original
850804
851805For example, utf8 can be used for code points that don't exist in Unicode, like
8528069999999, but if you encode that to UTF-8, you get a substitution character (by
853807default; see L<Encode/"Handling Malformed Data"> for more ways of dealing with
854808this.)
855809
856810=end original
857811
858812例えば utf8 は、9999999 のような、Unicode に存在しない符号位置も使えますが、
859813これを UTF-8 でエンコードすると、代替文字を得ることになります(これは
860814デフォルトの場合です; これを扱う他の方法については
861815L<Encode/"Handling Malformed Data"> を参照してください。)
862816
863817=begin original
864818
865819Okay, if you insist: the "internal format" is utf8, not UTF-8. (When it's not
866820some other encoding.)
867821
868822=end original
869823
870824わかりました、どうしてもと言うのなら:「内部形式」は utf8 であって、
871825UTF-8 ではありません。
872826(もしその他のエンコーディングでないのなら。)
873827
874828=head2 I lost track; what encoding is the internal format really?
875829
876830(迷子になりました; 実際のところ内部形式のエンコーディングは何?)
877831
878832=begin original
879833
880834It's good that you lost track, because you shouldn't depend on the internal
881835format being any specific encoding. But since you asked: by default, the
882836internal format is either ISO-8859-1 (latin-1), or utf8, depending on the
883837history of the string. On EBCDIC platforms, this may be different even.
884838
885839=end original
886840
887841迷子になったのはよいことです; なぜなら内部形式が特定のエンコーディングで
888842あることに依存するべきではないからです。
889843しかし聞かれたので答えましょう: デフォルトでは、内部形式は
890844ISO-8859-1 (latin-1) か utf8 で、どちらになるかは文字列の歴史に
891845依存します。
892846EBCDIC プラットフォームでは、これは異なっているかもしれません。
893847
894848=begin original
895849
896850Perl knows how it stored the string internally, and will use that knowledge
897851when you C<encode>. In other words: don't try to find out what the internal
898852encoding for a certain string is, but instead just encode it into the encoding
899853that you want.
900854
901855=end original
902856
903857Perl は文字列が内部でどのように保管されているかを知っていて、この知識を
904858C<エンコードする> ときに使います。
905859言い換えると: 特定の文字列の内部エンコーディングが何かを
906860調べようとしてはいけません; 代わりに、単に望みのエンコーディングに
907861エンコードしてください。
908862
909863=head1 AUTHOR
910864
911865Juerd Waalboer <#####@juerd.nl>
912866
913867=head1 SEE ALSO
914868
915869L<perlunicode>, L<perluniintro>, L<Encode>
916870
917871=begin meta
918872
919873Translate: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-)
920874Status: completed
921875
922876=end meta