perlunifaq > 5.14.1 との差分

perlunifaq 5.14.1 と 5.20.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
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
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
240240 open my $fh, '>:encoding(UTF-8)', $filename; # auto encoding on write
241241 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
251251 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
385381=begin original
386382
387383Starting in Perl 5.14 (and partially in Perl 5.12), just put a
388384C<use feature 'unicode_strings'> near the beginning of your program.
389385Within its lexical scope you shouldn't have this problem. It also is
390automatically enabled under C<use feature ':5.12'> or using C<-E> on the
386automatically enabled under C<use feature ':5.12'> or C<use v5.12> or
391command line for Perl 5.12 or higher.
387using C<-E> on the command line for Perl 5.12 or higher.
392388
393389=end original
394390
395391Perl 5.14 から (そして部分的に Perl 5.12 から、) 単にプログラムの先頭付近に
396392C<use feature 'unicode_strings'> を書いてください。
397393このレキシカルスコープ内ではこの問題は発生しないはずです。
398これはまた C<use feature ':5.12'> が有効か、Perl 5.12 以降でコマンドラインで
394これはまた C<use feature ':5.12'> または C<use v5.12> が有効か、Perl 5.12
399C<-E> を使っていると自動的に有効になります。
395以降でコマンドラインで C<-E> を使っていると自動的に有効になります。
400396
401397=begin original
402398
403399The rationale for requiring this is to not break older programs that
404400rely on the way things worked before Unicode came along. Those older
405401programs knew only about the ASCII character set, and so may not work
406402properly for additional characters. When a string is encoded in UTF-8,
407403Perl assumes that the program is prepared to deal with Unicode, but when
408the string isn't, Perl assumes that only ASCII (unless it is an EBCDIC
404the string isn't, Perl assumes that only ASCII
409platform) is wanted, and so those characters that are not ASCII
405is wanted, and so those characters that are not ASCII
410406characters aren't recognized as to what they would be in Unicode.
411407C<use feature 'unicode_strings'> tells Perl to treat all characters as
412408Unicode, whether the string is encoded in UTF-8 or not, thus avoiding
413409the problem.
414410
415411=end original
416412
417413これが必要な理論的根拠は、Unicode がやってくる前に動作する方法に
418414依存している古いプログラムを壊さないことです。
419415このような古いプログラムは ASCII 文字集合のみを知っているので、追加の
420416文字については正しく動作しないかも知れません。
421417Perl はプログラムが Unicode を扱えるように準備されていると仮定しますが、
422418文字列がそうでなかった場合、Perl は (EBCDIC プラットフォームでなければ)
423419ASCII のみが求められていると仮定するので、非 ASCII 文字は Unicode に
424420するべきものとして認識しません。
425421C<use feature 'unicode_strings'> は Perl に、文字が UTF-8 で
426422エンコードされているかどうかにかかわらず全ての文字を Unicode として
427423扱うように知らせて、この問題を回避します。
428424
429425=begin original
430426
431427However, on earlier Perls, or if you pass strings to subroutines outside
432the feature's scope, you can force Unicode semantics by changing the
428the feature's scope, you can force Unicode rules by changing the
433429encoding to UTF-8 by doing C<utf8::upgrade($string)>. This can be used
434430safely on any string, as it checks and does not change strings that have
435431already been upgraded.
436432
437433=end original
438434
439435しかし、以前の Perl であったり、この機能のスコープの外側のサブルーチンに
440436文字列を渡した場合、C<utf8::upgrade($string)> とすることでエンコーディングを
441UTF-8 にすることで強制的に Unicode の動作使えます。
437UTF-8 にすることで Unicode の規則強制できます。
442438これは既に昇格している文字列は変更しないので、どのような文字列に対しても
443439安全に用いることができます。
444440
445441=begin original
446442
447443For a more detailed discussion, see L<Unicode::Semantics> on CPAN.
448444
449445=end original
450446
451447さらなる詳細な議論については、CPAN の L<Unicode::Semantics> を
452448参照してください。
453449
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
454462=head2 How can I determine if a string is a text string or a binary string?
455463
456464(文字列がテキスト文字列かバイナリ文字列かを決定するには?)
457465
458466=begin original
459467
460468You can't. Some use the UTF8 flag for this, but that's misuse, and makes well
461469behaved modules like Data::Dumper look bad. The flag is useless for this
462470purpose, because it's off when an 8 bit encoding (by default ISO-8859-1) is
463471used to store the string.
464472
465473=end original
466474
467475それはできません。
468476このために UTF8 フラグを使う人もいますが、これは誤用で、Data::Dumper のように
469477正しく振る舞うモジュールをおかしくします。
470478このフラグはこの目的のためには使えません; なぜなら文字列の保管に 8 ビット
471479エンコーディングが使われている場合 (デフォルトでは ISO-8859-1 です)、
472480オフだからです。
473481
474482=begin original
475483
476484This is something you, the programmer, has to keep track of; sorry. You could
477485consider adopting a kind of "Hungarian notation" to help with this.
478486
479487=end original
480488
481489把握しておく必要があるプログラマに言えることはこれです; ごめんなさい。
482490これを助けるために、「ハンガリアン記法」のようなものの採用を
483491検討することもできます。
484492
485493=head2 How do I convert from encoding FOO to encoding BAR?
486494
487495(エンコーディング FOO から エンコーディング BAR に変換するには?)
488496
489497=begin original
490498
491499By first converting the FOO-encoded byte string to a text string, and then the
492500text string to a BAR-encoded byte string:
493501
494502=end original
495503
496504まず FOO でエンコードされたバイト文字列をテキスト文字列に変化し、
497505それからテキスト文字列を BAR エンコードされたバイト文字列に変換します:
498506
499507 my $text_string = decode('FOO', $foo_string);
500508 my $bar_string = encode('BAR', $text_string);
501509
502510=begin original
503511
504512or by skipping the text string part, and going directly from one binary
505513encoding to the other:
506514
507515=end original
508516
509517あるいは、テキスト文字列の部分を飛ばして、あるバイナリエンコーディングから
510518他のものへ直接変換します:
511519
512520 use Encode qw(from_to);
513521 from_to($string, 'FOO', 'BAR'); # changes contents of $string
514522
515523=begin original
516524
517525or by letting automatic decoding and encoding do all the work:
518526
519527=end original
520528
521529あるいは、自動でデコードとエンコードをさせることで全ての作業を行います:
522530
523531 open my $foofh, '<:encoding(FOO)', 'example.foo.txt';
524532 open my $barfh, '>:encoding(BAR)', 'example.bar.txt';
525533 print { $barfh } $_ while <$foofh>;
526534
527535=head2 What are C<decode_utf8> and C<encode_utf8>?
528536
529537(C<decode_utf8> と C<encode_utf8> って何?)
530538
531539=begin original
532540
533541These are alternate syntaxes for C<decode('utf8', ...)> and C<encode('utf8',
534542...)>.
535543
536544=end original
537545
538546これらは C<decode('utf8', ...)> および C<encode('utf8', ...)> のもう一つの
539547文法です。
540548
541549=head2 What is a "wide character"?
542550
543551(「ワイド文字」って何?)
544552
545553=begin original
546554
547555This is a term used both for characters with an ordinal value greater than 127,
548556characters with an ordinal value greater than 255, or any character occupying
549557more than one byte, depending on the context.
550558
551559=end original
552560
553561これは文脈に依存して、127 より大きい序数を持つ文字、255 より大きい序数を
554562持つ文字、1 バイトで収まらない文字、のいずれかの意味で使われる用語です。
555563
556564=begin original
557565
558566The Perl warning "Wide character in ..." is caused by a character with an
559567ordinal value greater than 255. With no specified encoding layer, Perl tries to
560568fit things in ISO-8859-1 for backward compatibility reasons. When it can't, it
561569emits this warning (if warnings are enabled), and outputs UTF-8 encoded data
562570instead.
563571
564572=end original
565573
566574Perl の警告 "Wide character in ..." は 255 より大きい序数を持つ文字によって
567575引き起こされます。
568576エンコーディング層が指定されていない場合、Perl は過去互換性の理由によって
569577文字を ISO-8859-1 に合わせようとします。
570578これができないと、(警告が有効なら)この警告が出力され、代わりに UTF-8 で
571579エンコードされたデータが出力されます。
572580
573581=begin original
574582
575583To avoid this warning and to avoid having different output encodings in a single
576584stream, always specify an encoding explicitly, for example with a PerlIO layer:
577585
578586=end original
579587
580588この警告を回避し、一つのストリームに異なった出力エンコーディングが
581589出力されることを回避するには、常に明示的にエンコーディングを指定してください;
582590例えば PerlIO 層を使って:
583591
584592 binmode STDOUT, ":encoding(UTF-8)";
585593
586594=head1 INTERNALS
587595
588596(内部構造)
589597
590598=head2 What is "the UTF8 flag"?
591599
592600(「UTF8 フラグ」って何?)
593601
594602=begin original
595603
596604Please, unless you're hacking the internals, or debugging weirdness, don't
597605think about the UTF8 flag at all. That means that you very probably shouldn't
598606use C<is_utf8>, C<_utf8_on> or C<_utf8_off> at all.
599607
600608=end original
601609
602610内部をハックしようとしているか、変なものをデバッグしようとしているのでない
603611限り、どうか UTF8 フラグのことは一切考えないでください。
604612これは、まず間違いなく C<is_utf8>, C<_utf8_on>, C<_utf8_off> を
605613一切使うべきでないことを意味します。
606614
607615=begin original
608616
609617The UTF8 flag, also called SvUTF8, is an internal flag that indicates that the
610618current internal representation is UTF-8. Without the flag, it is assumed to be
611619ISO-8859-1. Perl converts between these automatically. (Actually Perl usually
612620assumes the representation is ASCII; see L</Why do regex character classes
613621sometimes match only in the ASCII range?> above.)
614622
615623=end original
616624
617625UTF8 フラグ(SvUTF8 とも呼ばれます)は、現在の内部表現が UTF-8 であることを
618626示す内部フラグです。
619627このフラグがない場合、ISO-8859-1 と仮定します。
620628Perl はこれらを自動的に変換します。
621629(実際のところ Perl は普通表現が ASCII であると仮定します; 上述の L</Why do
622630regex character classes sometimes match only in the ASCII range?> を
623631参照してください。)
624632
625633=begin original
626634
627635One of Perl's internal formats happens to be UTF-8. Unfortunately, Perl can't
628636keep a secret, so everyone knows about this. That is the source of much
629637confusion. It's better to pretend that the internal format is some unknown
630638encoding, and that you always have to encode and decode explicitly.
631639
632640=end original
633641
634642Perl の内部表現の一つはたまたま UTF-8 です。
635643残念ながら、Perl は秘密を守れないので、このことはみんな知っています。
636644これが多くの混乱の源です。
637645内部表現は何か分からないエンコーディングで、常に明示的にエンコードと
638646デコードが必要ということにしておいた方がよいです。
639647
640648=head2 What about the C<use bytes> pragma?
641649
642650(C<use bytes> プラグマって何?)
643651
644652=begin original
645653
646654Don't use it. It makes no sense to deal with bytes in a text string, and it
647655makes no sense to deal with characters in a byte string. Do the proper
648656conversions (by decoding/encoding), and things will work out well: you get
649657character counts for decoded data, and byte counts for encoded data.
650658
651659=end original
652660
653661これは使わないでください。
654662テキスト文字列をバイト単位で扱うことに意味はありませんし、
655663バイト文字列を文字単位で扱うことには意味はありません。
656664適切な変換(デコードかエンコード)を行えば、物事はうまくいきます:
657665デコードしたデータの文字数を得られますし、エンコードしたデータのバイト数を
658666得られます。
659667
660668=begin original
661669
662670C<use bytes> is usually a failed attempt to do something useful. Just forget
663671about it.
664672
665673=end original
666674
667675C<use bytes> は何か有用なことをしようとするためには間違った方法です。
668676これのことは単に忘れてください。
669677
670678=head2 What about the C<use encoding> pragma?
671679
672680(C<use encoding> プラグマって何?)
673681
674682=begin original
675683
676684Don't use it. Unfortunately, it assumes that the programmer's environment and
677685that of the user will use the same encoding. It will use the same encoding for
678686the source code and for STDIN and STDOUT. When a program is copied to another
679687machine, the source code does not change, but the STDIO environment might.
680688
681689=end original
682690
683691これは使わないでください。
684692残念ながら、これはプログラマの環境とユーザーの環境が同じであると仮定します。
685693これはソースコードと STDIN や STDOUT で同じエンコーディングを使います。
686694プログラムが他のマシンにコピーされると、ソースコードは変わりませんが、
687695STDIO 環境は変わるかもしれません。
688696
689697=begin original
690698
691699If you need non-ASCII characters in your source code, make it a UTF-8 encoded
692700file and C<use utf8>.
693701
694702=end original
695703
696704もしソースコードに非 ASCII 文字が必要なら、ファイルを UTF-8 で
697705エンコードして、C<use utf8> を使ってください。
698706
699707=begin original
700708
701709If you need to set the encoding for STDIN, STDOUT, and STDERR, for example
702710based on the user's locale, C<use open>.
703711
704712=end original
705713
706714もし STDIN, STDOUT, STDERR のエンコーディングを、例えばユーザーのロケールに
707715合わせてセットする必要があるなら、C<use open> してください。
708716
709717=head2 What is the difference between C<:encoding> and C<:utf8>?
710718
711719(C<:encoding> と C<:utf8> の違いは?)
712720
713721=begin original
714722
715723Because UTF-8 is one of Perl's internal formats, you can often just skip the
716724encoding or decoding step, and manipulate the UTF8 flag directly.
717725
718726=end original
719727
720728UTF-8 は Perl の内部形式のひとつなので、しばしばエンコードやデコードの
721729手順を省略して、UTF8 フラグを直接操作できます。
722730
723731=begin original
724732
725733Instead of C<:encoding(UTF-8)>, you can simply use C<:utf8>, which skips the
726734encoding step if the data was already represented as UTF8 internally. This is
727735widely accepted as good behavior when you're writing, but it can be dangerous
728736when reading, because it causes internal inconsistency when you have invalid
729737byte sequences. Using C<:utf8> for input can sometimes result in security
730738breaches, so please use C<:encoding(UTF-8)> instead.
731739
732740=end original
733741
734742C<:encoding(UTF-8)> の代わりに単に C<:utf8> を使うことで、もしデータが
735743内部で既に UTF8 で表現されていれば、エンコードの手順を省略します。
736744これは、書き込むときにはよい振る舞いであると広く受け入れられていますが、
737745読み込むときには危険があります; なぜなら不正なバイト列を受け取ると
738746内部矛盾を引き起こすからです。
739747入力に C<:utf8> を使うとセキュリティ侵害を引き起こす可能性があるので、
740748どうか代わりに C<:encoding(UTF-8)> を使ってください。
741749
742750=begin original
743751
744752Instead of C<decode> and C<encode>, you could use C<_utf8_on> and C<_utf8_off>,
745753but this is considered bad style. Especially C<_utf8_on> can be dangerous, for
746754the same reason that C<:utf8> can.
747755
748756=end original
749757
750758C<decode> と C<encode> の代わりに、C<_utf8_on> と C<_utf8_off> を
751759使えますが、これは悪いスタイルと考えられています。
752760特に C<_utf8_on> は、C<:utf8> と同じ理由で危険です。
753761
754762=begin original
755763
756There are some shortcuts for oneliners; see C<-C> in L<perlrun>.
764There are some shortcuts for oneliners;
765see L<-C|perlrun/-C [numberE<sol>list]> in L<perlrun>.
757766
758767=end original
759768
760一行野郎のための省略形があります; L<perlrun> の C<-C> を参照してください。
769一行野郎のための省略形があります; L<perlrun> の
770L<-C|perlrun/-C [numberE<sol>list]> を参照してください。
761771
762772=head2 What's the difference between C<UTF-8> and C<utf8>?
763773
764774(C<UTF-8> と C<utf8> の違いは?)
765775
766776=begin original
767777
768778C<UTF-8> is the official standard. C<utf8> is Perl's way of being liberal in
769779what it accepts. If you have to communicate with things that aren't so liberal,
770780you may want to consider using C<UTF-8>. If you have to communicate with things
771781that are too liberal, you may have to use C<utf8>. The full explanation is in
772782L<Encode>.
773783
774784=end original
775785
776786C<UTF-8> は公式な標準です。
777787C<utf8> は、何を受け入れるかに関して自由な Perl のやり方です。
778788もしそれほど自由でないものと対話する必要があるなら、
779789C<UTF-8> を使うことを考えたくなるかもしれません。
780790自由すぎるものと対話する必要があるなら、C<utf8> を
781791使わなければならないかもしれません。
782792完全な説明は L<Encode> にあります。
783793
784794=begin original
785795
786796C<UTF-8> is internally known as C<utf-8-strict>. The tutorial uses UTF-8
787797consistently, even where utf8 is actually used internally, because the
788798distinction can be hard to make, and is mostly irrelevant.
789799
790800=end original
791801
792802C<UTF-8> は内部では C<utf-8-strict> として知られます。
793803チュートリアルでは、たとえ内部では実際には utf8 が使われる場合でも
794804一貫して UTF-8 を使っています; なぜなら区別をつけるのは難しく、ほとんど
795805無意味だからです。
796806
797807=begin original
798808
799809For example, utf8 can be used for code points that don't exist in Unicode, like
8008109999999, but if you encode that to UTF-8, you get a substitution character (by
801811default; see L<Encode/"Handling Malformed Data"> for more ways of dealing with
802812this.)
803813
804814=end original
805815
806816例えば utf8 は、9999999 のような、Unicode に存在しない符号位置も使えますが、
807817これを UTF-8 でエンコードすると、代替文字を得ることになります(これは
808818デフォルトの場合です; これを扱う他の方法については
809819L<Encode/"Handling Malformed Data"> を参照してください。)
810820
811821=begin original
812822
813823Okay, if you insist: the "internal format" is utf8, not UTF-8. (When it's not
814824some other encoding.)
815825
816826=end original
817827
818828わかりました、どうしてもと言うのなら:「内部形式」は utf8 であって、
819829UTF-8 ではありません。
820830(もしその他のエンコーディングでないのなら。)
821831
822832=head2 I lost track; what encoding is the internal format really?
823833
824834(迷子になりました; 実際のところ内部形式のエンコーディングは何?)
825835
826836=begin original
827837
828838It's good that you lost track, because you shouldn't depend on the internal
829839format being any specific encoding. But since you asked: by default, the
830840internal format is either ISO-8859-1 (latin-1), or utf8, depending on the
831841history of the string. On EBCDIC platforms, this may be different even.
832842
833843=end original
834844
835845迷子になったのはよいことです; なぜなら内部形式が特定のエンコーディングで
836846あることに依存するべきではないからです。
837847しかし聞かれたので答えましょう: デフォルトでは、内部形式は
838848ISO-8859-1 (latin-1) か utf8 で、どちらになるかは文字列の歴史に
839849依存します。
840850EBCDIC プラットフォームでは、これは異なっているかもしれません。
841851
842852=begin original
843853
844854Perl knows how it stored the string internally, and will use that knowledge
845855when you C<encode>. In other words: don't try to find out what the internal
846856encoding for a certain string is, but instead just encode it into the encoding
847857that you want.
848858
849859=end original
850860
851861Perl は文字列が内部でどのように保管されているかを知っていて、この知識を
852862C<エンコードする> ときに使います。
853863言い換えると: 特定の文字列の内部エンコーディングが何かを
854864調べようとしてはいけません; 代わりに、単に望みのエンコーディングに
855865エンコードしてください。
856866
857867=head1 AUTHOR
858868
859869Juerd Waalboer <#####@juerd.nl>
860870
861871=head1 SEE ALSO
862872
863873L<perlunicode>, L<perluniintro>, L<Encode>
864874
865875=begin meta
866876
867877Translate: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-)
868878Status: completed
869879
870880=end meta