=encoding euc-jp =head1 NAME =begin original perluniintro - Perl Unicode introduction =end original perluniintro - Perl Unicode の手引き =head1 DESCRIPTION =begin original This document gives a general idea of Unicode and how to use Unicode in Perl. =end original このドキュメントは、Unicode の一般的な考えと、 Perl で Unicode をどのように使うかを書いています。 =head2 Unicode =begin original Unicode is a character set standard which plans to codify all of the writing systems of the world, plus many other symbols. =end original Unicode は、文字セットの標準です。 世界の全ての書記体系と、それに加えて、 他の多くのシンボルを体系化することを計画しています。 =begin original Unicode and ISO/IEC 10646 are coordinated standards that provide code points for characters in almost all modern character set standards, covering more than 30 writing systems and hundreds of languages, including all commercially-important modern languages. All characters in the largest Chinese, Japanese, and Korean dictionaries are also encoded. The standards will eventually cover almost all characters in more than 250 writing systems and thousands of languages. Unicode 1.0 was released in October 1991, and 4.0 in April 2003. =end original Unicode と、ISO/IEC 10646 は、よく調整された、コードポイントを提供する 標準です。 Unicode は、ほとんど全ての現代の文字セット標準、30 以上の書記体系と、 100 以上の言語を網羅します。 全ての商業的に重要な現代の言語を含みます。 もっとも大きい中国語、日本語、韓国語、それぞれの辞書の全ての文字もまた、 符号化されています。 この標準は、最終的には、250 の書記体系と、1000 以上の言語のほとんどすべての 文字を網羅する予定です。 Unicode 1.0 は、1991 年 10 月にリリースされ、Unicode 4.0 は、 2003 年 4 月にリリースされました。 =begin original A Unicode I is an abstract entity. It is not bound to any particular integer width, especially not to the C language C. Unicode is language-neutral and display-neutral: it does not encode the language of the text and it does not define fonts or other graphical layout details. Unicode operates on characters and on text built from those characters. =end original Unicode の I<文字> は、抽象的な存在です。 Unicode の文字は、どんな特定の整数幅にも、特に、C 言語の C にも 束縛されません。 Unicode は、言語中立で、表示中立です: Unicode は、テキストの言語をエンコードしませんし、 フォントや他のグラフィカルなレイアウトの詳細を定義しません。 Unicode は、文字と、それらの文字からなるテキストを操作します。 =begin original Unicode defines characters like C or C and unique numbers for the characters, in this case 0x0041 and 0x03B1, respectively. These unique numbers are called I. =end original Unicode は、C や C のような 文字と、その文字について固有の番号を定義します。 この場合は、それぞれ、0x0041 と、0x03B1 になります。 このような固有の番号は、I<コードポイント> と呼ばれます。 =begin original The Unicode standard prefers using hexadecimal notation for the code points. If numbers like C<0x0041> are unfamiliar to you, take a peek at a later section, L. The Unicode standard uses the notation C, to give the hexadecimal code point and the normative name of the character. =end original Unicode 標準は、コードポイントに 16 進記法を使うのを好みます。 C<0x0041> のような番号に馴染みがなければ、後のセクション、 L を覗いて見て下さい。 Unicode 標準は、C という表記を使って、 16 進法のコードポイントと標準的な文字の名前を書きます。 =begin original Unicode also defines various I for the characters, like "uppercase" or "lowercase", "decimal digit", or "punctuation"; these properties are independent of the names of the characters. Furthermore, various operations on the characters like uppercasing, lowercasing, and collating (sorting) are defined. =end original Unicode はまた、様々な文字の I<性質> を定義します。 "大文字"、"小文字"、"10進数字"、"句読点" など; これらの性質は、文字の名前と独立です。 更に、様々な文字に対する操作、大文字化や小文字化や並び替えが 定義されています。 =begin original A Unicode character consists either of a single code point, or a I (like C), followed by one or more I (like C). This sequence of base character and modifiers is called a I. =end original Unicode 文字は、1 つのコードポイントか、または、 (C のような)、I<基本文字> に続いて、 1つ以上の(C のような) I<修飾語句> の、 どちらか一方から成っています。 この基本文字と修飾語句の順番は、I と 呼ばれます。 =begin original Whether to call these combining character sequences "characters" depends on your point of view. If you are a programmer, you probably would tend towards seeing each element in the sequences as one unit, or "character". The whole sequence could be seen as one "character", however, from the user's point of view, since that's probably what it looks like in the context of the user's language. =end original これらの combining character sequence を "複数の文字" と呼ぶかどうかは、 考え方によります。 プログラマならば、おそらく、この順番のそれぞれの要素を、 1 つの単位か "文字" として、見ようとするでしょう。 全ての順番を一つの"文字"として見ることができます。 ですが、ユーザの考え方からは、おそらくユーザの言語の文脈でみえるような ものでしょう。 =begin original With this "whole sequence" view of characters, the total number of characters is open-ended. But in the programmer's "one unit is one character" point of view, the concept of "characters" is more deterministic. In this document, we take that second point of view: one "character" is one Unicode code point, be it a base character or a combining character. =end original この、文字の"すべての順番"の見え方では、文字の総数には、制限がありません。 ですが、プログラマーの"一つの単位は一つの文字"という考え方では、"文字" の 概念は、より決定論的です。 このドキュメントでは、2 番目の考え方を取ります: 1 つの"文字"は、1 つの Unicode のコードポイントであり、それは、基本文字か、 combining character であるとします。 =begin original For some combinations, there are I characters. C, for example, is defined as a single code point. These precomposed characters are, however, only available for some combinations, and are mainly meant to support round-trip conversions between Unicode and legacy standards (like the ISO 8859). In the general case, the composing method is more extensible. To support conversion between different compositions of the characters, various I to standardize representations are also defined. =end original いくつかの組合せにとって、I<まだ構成されていない> 文字があります。 たとえば、C は、 単一のコードポイントとして定義されています。 しかし、これらの、まだ構成されていない文字は、いくつかの組合せで 可能でしかありません。 そして、それらは、主に、Unicode と、レガシー標準(ISO 8859 のような)との 間の相互変換をサポートすることを意味します。 一般的なケースでは、構成する方法はより広がります。 違った構成間の変換をサポートするために、表現を標準化する、様々な I もまた定義されています。 =begin original Because of backward compatibility with legacy encodings, the "a unique number for every character" idea breaks down a bit: instead, there is "at least one number for every character". The same character could be represented differently in several legacy encodings. The converse is also not true: some code points do not have an assigned character. Firstly, there are unallocated code points within otherwise used blocks. Secondly, there are special Unicode control characters that do not represent true characters. =end original レガシーエンコーディングとの後方互換性のために、 "全ての文字に固有の番号" という考えは、少々壊れています: その代わりに、"少なくとも全ての文字に 1 つの番号" があります。 同じ文字が、いくつかのレガシーエンコーディングの中で、違うように 表現されていました。 逆は真でもなく: コードポイントには、文字が割り当てられていないものも あります。 1 番目に、使われているブロック内にもかかわらず、割り当てられていない コードポイントがあります。 2 番目に、特別な Unicode のコントロール文字があり、それらは、本物の文字を 表現しません。 =begin original A common myth about Unicode is that it would be "16-bit", that is, Unicode is only represented as C<0x10000> (or 65536) characters from C<0x0000> to C<0xFFFF>. B Since Unicode 2.0 (July 1996), Unicode has been defined all the way up to 21 bits (C<0x10FFFF>), and since Unicode 3.1 (March 2001), characters have been defined beyond C<0xFFFF>. The first C<0x10000> characters are called the I, or the I (BMP). With Unicode 3.1, 17 (yes, seventeen) planes in all were defined--but they are nowhere near full of defined characters, yet. =end original Unicode について、よく知られている神話は、Unicode が、"16-ビット" である、 というものです。 Unicode は、C<0x0000> から、C<0xFFFF> までで、C<0x10000> (か、65536)の 文字を表現するだけ、というものです。 B<これは真実ではありません>。 Unicode 2.0(1996 年 7 月)から、Unicode は、21ビット(C<0x10FFFF>)まで、 様々に定義されています。 Unicode 3.1(2001 年 3 月)から、文字は、C<0xFFFF> を超えて、定義されました。 最初の C<0x10000> 文字は、 I、または、I(BMP)と、と呼ばれました。 Unicode 3.1 で、全部で 17(そう、17)の Plane が定義されました -- ですが、 まだ、定義された全文字のどこにも、まだ近くにありません。 =begin original Another myth is that the 256-character blocks have something to do with languages--that each block would define the characters used by a language or a set of languages. B The division into blocks exists, but it is almost completely accidental--an artifact of how the characters have been and still are allocated. Instead, there is a concept called I, which is more useful: there is C script, C script, and so on. Scripts usually span varied parts of several blocks. For further information see L. =end original もう 1 つの神話は 256 文字ブロックが、何か言語を取り扱うことがある-- それぞれのブロックは言語か言語のセットで使われている文字を 定義するということです。 B<これもまた真実ではありません>。 ブロックに分割されたものはありますが、それは、ほとんど完全に、 予想外のものです。 代わりに、I と呼ばれる、より有益なコンセプトがあります: C script と、C script と、その他のものがあります。 scripts は、ふつう、いくつかのブロックの変えられた部分を測っています。 詳しくは、L を見て下さい。 =begin original The Unicode code points are just abstract numbers. To input and output these abstract numbers, the numbers must be I or I somehow. Unicode defines several I, of which I is perhaps the most popular. UTF-8 is a variable length encoding that encodes Unicode characters as 1 to 6 bytes (only 4 with the currently defined characters). Other encodings include UTF-16 and UTF-32 and their big- and little-endian variants (UTF-8 is byte-order independent) The ISO/IEC 10646 defines the UCS-2 and UCS-4 encoding forms. =end original Unicode のコードポイントは、抽象的な数字です。 この抽象的な数字を入出力するために、数字は、I<エンコードされる> 必要が あるか、I<シリアライズされる> か、どうか、しなければなりません。 Unicode は、複数の I を定義していますが、 その中で、I は、たぶん、もっとも有名です。 UTF-8 は、可変長のエンコーディングで、Unicode 文字を 1 から 6 バイト (現在定義されている文字では 4 バイトまでです)。 他のエンコーディングは、UTF-16 と UTF-32 と、それらの大小のエンディアンの 変形(UTF-8 は、バイトオーダーと独立です)を含みます。 ISO/IEC 10646 は、UCS-2 と UCS-4 の encoding forms を定義しています。 =begin original For more information about encodings--for instance, to learn what I and I (BOMs) are--see L. =end original エンコーディングについて -- たとえば、I と I(BOMs) -- もっと知りたければ L を 見て下さい。 =head2 Perl's Unicode Support (Perl の Unicode サポート) =begin original Starting from Perl 5.6.0, Perl has had the capacity to handle Unicode natively. Perl 5.8.0, however, is the first recommended release for serious Unicode work. The maintenance release 5.6.1 fixed many of the problems of the initial Unicode implementation, but for example regular expressions still do not work with Unicode in 5.6.1. =end original Perl 5.6.0 から、Perl は、Unicode をネイティブに扱う能力を持っていました。 しかし、Perl 5.8.0 の最初の RC リリースは、重大な Unicode の仕事です。 メンテナンスリリースの 5.6.1 は、最初の Unicode 実装の多くの問題を 修正しました。 ですが、たとえば、5.6.1 で、Unicode での正規表現はまだ働きません。 =begin original B is no longer necessary.> In earlier releases the C pragma was used to declare that operations in the current block or file would be Unicode-aware. This model was found to be wrong, or at least clumsy: the "Unicodeness" is now carried with the data, instead of being attached to the operations. Only one case remains where an explicit C is needed: if your Perl script itself is encoded in UTF-8, you can use UTF-8 in your identifier names, and in string and regular expression literals, by saying C. This is not the default because scripts with legacy 8-bit data in them would break. See L. =end original B の使用は、もはや必要ではありません>。 初期のリリースでは、C プラグマは、現在のブロックやファイルの操作が、 Unicode であると明示するのを宣言するために使われました。 このモデルは間違いが見つけられるか、少なくとも、不格好です: 操作に添付するかわりに、"Unicodeness" は、今や、データに持ち込まれています。 唯一残されている、明示的に C をする必要がある場所は: Perl スクリプト自身が UTF-8 でエンコードされていれば、 識別子の名前、文字の中と、正規表現のリテラルに、UTF-8 を使うことができます。 これは、デフォルトではありません。 なぜなら、レガシーの 8-bit データのスクリプトが壊れるからです。 L を参照してください。 =head2 Perl's Unicode Model (Perl の Unicode モデル) =begin original Perl supports both pre-5.6 strings of eight-bit native bytes, and strings of Unicode characters. The principle is that Perl tries to keep its data as eight-bit bytes for as long as possible, but as soon as Unicodeness cannot be avoided, the data is transparently upgraded to Unicode. =end original Perl は、Perl 5.6 より前での 8 ビットネイティブのバイト列の文字列と、 Unicode 文字の文字列の両方をサポートします。 方針は、Perl は、可能な限り長く 8 ビットバイト列としてデータを 保とうとします。 ですが、Unicode 性が避けられなくなった時点で、 データは透過的に Unicode に昇格されます。 =begin original Internally, Perl currently uses either whatever the native eight-bit character set of the platform (for example Latin-1) is, defaulting to UTF-8, to encode Unicode strings. Specifically, if all code points in the string are C<0xFF> or less, Perl uses the native eight-bit character set. Otherwise, it uses UTF-8. =end original 内部的には、Perl は、プラットフォームの 8 ビット文字セット (例えば、Latin-1)は、デフォルトは UTF-8 ですが、Unicode 文字列に エンコードします。 特に、文字列中の、全てのコードポイントは、C<0xFF> 以下であれば、 Perl は、ネイティブの 8 ビットの文字セットを使います。 そうでなければ、UTF-8 を使います。 =begin original A user of Perl does not normally need to know nor care how Perl happens to encode its internal strings, but it becomes relevant when outputting Unicode strings to a stream without a PerlIO layer -- one with the "default" encoding. In such a case, the raw bytes used internally (the native character set or UTF-8, as appropriate for each string) will be used, and a "Wide character" warning will be issued if those strings contain a character beyond 0x00FF. =end original Perlのユーザは普通は、Perl がその内部文字列をたまたま、どのように エンコードするかを、知る必要も、気にする必要もありませんが、Unicode 文字列を PerlIO 層なしに -- 「デフォルトの」エンコーディングで -- ストリームに 出力しようとすると、関係するようになります。 このようなケースでは、内部的に使われている生のバイト列(それぞれの 文字列の必要に応じて、ネイティブの文字セットか、UTF-8)が使われます。 それらの文字列に、0x00FF を超える文字があれば、"Wide character" の警告が 出されます。 =begin original For example, =end original たとえば、 perl -e 'print "\x{DF}\n", "\x{0100}\x{DF}\n"' =begin original produces a fairly useless mixture of native bytes and UTF-8, as well as a warning: =end original ネイティブのバイト列と UTF-8 の、まったく役に立たない混合もまた、 同様に警告を出します: Wide character in print at ... =begin original To output UTF-8, use the C<:encoding> or C<:utf8> output layer. Prepending =end original UTF-8 を出力するために、C<:encoding> か C<:utf8> 出力層を使います。 先頭にこれを追加することで: binmode(STDOUT, ":utf8"); =begin original to this sample program ensures that the output is completely UTF-8, and removes the program's warning. =end original このサンプルプログラムが、出力が完全に UTF-8 であることを保証し、 プログラムの警告を削除します。 =begin original You can enable automatic UTF-8-ification of your standard file handles, default C layer, and C<@ARGV> by using either the C<-C> command line switch or the C environment variable, see L for the documentation of the C<-C> switch. =end original C<-C> コマンドラインスイッチか、C 環境変数のどちらか一方を 使うことで、標準ファイルハンドルと、デフォルトの C 層と、 C<@ARGV> の UTF-8 化を自動的に有効に出来ます; L の C<-C> スイッチのドキュメントを見て下さい。 =begin original Note that this means that Perl expects other software to work, too: if Perl has been led to believe that STDIN should be UTF-8, but then STDIN coming in from another command is not UTF-8, Perl will complain about the malformed UTF-8. =end original このことは、Perl が他のソフトウェアの動きを予測することを意味するのに 気をつけて下さい: Perl が STDIN が UTF-8 であるべきと信じるように主導されていて、 他のコマンドからくる STDIN が UTF-8 でなければ、 Perl は、不正な UTF-8 であると文句を言います。 =begin original All features that combine Unicode and I/O also require using the new PerlIO feature. Almost all Perl 5.8 platforms do use PerlIO, though: you can see whether yours is by running "perl -V" and looking for C. =end original Unicode と、I/O の結合の特徴もまた、新しい PerlIO の特徴を使うことを 必要とします。 ほとんど全ての Perl5.8 プラットフォームは、PerlIO を使います。 ですが: "perl -V" を動かして、C を見れば、PerlIO を 使っているかどうか、わかります。 =head2 Unicode and EBCDIC (Unicode と EBCDIC) =begin original Perl 5.8.0 also supports Unicode on EBCDIC platforms. There, Unicode support is somewhat more complex to implement since additional conversions are needed at every step. Some problems remain, see L for details. =end original Perl 5.8.0 は、EBCDIC プラットフォームでもまた、Unicode をサポートします。 ここでは、各ステップで追加の変換が必要になるので、Unicode サポートは 実装が若干より複雑になります。 いくつかの問題が残っています; 詳しくは L を参照してください。 =begin original In any case, the Unicode support on EBCDIC platforms is better than in the 5.6 series, which didn't work much at all for EBCDIC platform. On EBCDIC platforms, the internal Unicode encoding form is UTF-EBCDIC instead of UTF-8. The difference is that as UTF-8 is "ASCII-safe" in that ASCII characters encode to UTF-8 as-is, while UTF-EBCDIC is "EBCDIC-safe". =end original ともかく、EBCDIC プラットフォームでの Unicode 対応は、EBCDIC プラットフォームではうまく動かなかった 5.6 シリーズよりは改善しています。 EBCDIC プラットフォームでは、内部 Unicode エンコーディング形式は UTF-8 ではなく UTF-EBCDIC です。 この違いは、ASCII 文字はそのまま UTF-8 にエンコードされるという点において "ASCII セーフ" ですが、一方 UTF-EBCDIC は "EBCDIC セーフ" です。 =head2 Creating Unicode (Unicode の作成) =begin original To create Unicode characters in literals for code points above C<0xFF>, use the C<\x{...}> notation in double-quoted strings: =end original C<0xFF> を超えるコードポイントのリテラルで Unicode 文字を作るためには、 C<\x{...}> 記法をダブルクォートされた文字列の中で使います。 my $smiley = "\x{263a}"; =begin original Similarly, it can be used in regular expression literals =end original 同様に、正規表現の中でも使えます。 $smiley =~ /\x{263a}/; =begin original At run-time you can use C: =end original C を使って、実行時に: my $hebrew_alef = chr(0x05d0); =begin original See L for how to find all these numeric codes. =end original 全てのこれらの数字のコードを見つける方法は、L を 参照してください。 =begin original Naturally, C will do the reverse: it turns a character into a code point. =end original 当然、C は、逆を行います: 文字をコードポイントに変えます。 =begin original Note that C<\x..> (no C<{}> and only two hexadecimal digits), C<\x{...}>, and C for arguments less than C<0x100> (decimal 256) generate an eight-bit character for backward compatibility with older Perls. For arguments of C<0x100> or more, Unicode characters are always produced. If you want to force the production of Unicode characters regardless of the numeric value, use C instead of C<\x..>, C<\x{...}>, or C. =end original C<0x100>(10 進の 256)未満の引数の、C<\x..> (C<{}> なしで、 2 つの 16 進数の数字のみです)と、C<\x{...}> と、C は、古い Perl との互換性のために、8 ビットの文字列を生成します。 C かそれ以上の引数では、常に Unicode 文字が生成されます。 何が何でも、数字の値の Unicode 文字を、強制的に生成したければ、 C<\x..>、C<\x{...}>、C の代わりに、C を使って下さい。 =begin original You can also use the C pragma to invoke characters by name in double-quoted strings: =end original ダブルクォートされた文字列内で、名前で文字を呼び出すために、 C プラグマを使うこともできます: use charnames ':full'; my $arabic_alef = "\N{ARABIC LETTER ALEF}"; =begin original And, as mentioned above, you can also C numbers into Unicode characters: =end original そして、上述のように、数字を Unicode 文字に、C できます: my $georgian_an = pack("U", 0x10a0); =begin original Note that both C<\x{...}> and C<\N{...}> are compile-time string constants: you cannot use variables in them. if you want similar run-time functionality, use C and C. =end original C<\x{...}> と、C<\N{...}> の両方は、コンパイル時の文字列定数です: その中に変数を使うことはできません。 類似のことを実行時にしたいなら、c と、C を 使ってください。 =begin original If you want to force the result to Unicode characters, use the special C<"U0"> prefix. It consumes no arguments but causes the following bytes to be interpreted as the UTF-8 encoding of Unicode characters: =end original 結果を Unicode 文字に強制したいなら、特別な、C<"U0"> 接頭辞を使って下さい。 これは引数を消費しませんが、引き続くバイト列を Unicode 文字の UTF-8 エンコーディングとして解釈させます: my $chars = pack("U0W*", 0x80, 0x42); =begin original Likewise, you can stop such UTF-8 interpretation by using the special C<"C0"> prefix. =end original 同様に、特殊な C<"C0"> 接頭辞を使うことによって、このような UTF-8 の解釈を 停止させることができます。 =head2 Handling Unicode (Unicode を扱う) =begin original Handling Unicode is for the most part transparent: just use the strings as usual. Functions like C, C, and C will work on the Unicode characters; regular expressions will work on the Unicode characters (see L and L). =end original Unicode を扱うことは、多くの部分にとって、透過的です: 文字列をいつものように使うだけです。 C、C、C のような関数は Unicode 文字列で 動きます; 正規表現も Unicode 文字列で動きます(L と L を 参照してください)。 =begin original Note that Perl considers combining character sequences to be separate characters, so for example =end original Perl は combining character sequences を別々の文字と考えていることに 注意して下さい。 ですので、例えば use charnames ':full'; print length("\N{LATIN CAPITAL LETTER A}\N{COMBINING ACUTE ACCENT}"), "\n"; =begin original will print 2, not 1. The only exception is that regular expressions have C<\X> for matching a combining character sequence. =end original これは 1 ではなく、2 を表示します。 唯一の例外は、正規表現に、combining character sequence に マッチするために、C<\X> がある場合です。 =begin original Life is not quite so transparent, however, when working with legacy encodings, I/O, and certain special cases: =end original しかし、レガシーエンコーディング、I/O、そしてある種の特殊な状況では、 人生はそれほど透過的ではありません: =head2 Legacy Encodings (レガシーエンコーディング) =begin original When you combine legacy data and Unicode the legacy data needs to be upgraded to Unicode. Normally ISO 8859-1 (or EBCDIC, if applicable) is assumed. =end original レガシーデータと Unicode とを組み合わせる時は、 レガシーデータを Unicode に昇格しなければなりません。 通常、ISO 8859-1 (か、必要なら EBCDIC)が仮定されます。 =begin original The C module knows about many encodings and has interfaces for doing conversions between those encodings: =end original C モジュールは多くのエンコーディングを知っており、 それらのエンコーディングの間の変換をするインターフェースを持っています。 use Encode 'decode'; $data = decode("iso-8859-3", $data); # convert from legacy to utf-8 =head2 Unicode I/O =begin original Normally, writing out Unicode data =end original 通常、Unicode データを書き出すことは、 print FH $some_string_with_unicode, "\n"; =begin original produces raw bytes that Perl happens to use to internally encode the Unicode string. Perl's internal encoding depends on the system as well as what characters happen to be in the string at the time. If any of the characters are at code points C<0x100> or above, you will get a warning. To ensure that the output is explicitly rendered in the encoding you desire--and to avoid the warning--open the stream with the desired encoding. Some examples: =end original Unicode 文字列を内部的にエンコードするのにたまたまつかわれている、 生のバイト列を生成することです。 Perl の内部エンコーディングは、そのときに、文字列にたまたまある文字と 同じように、システム依存です。 どの文字も、コードポイントが C<0x100> 以上なら、警告がでます。 出力が明示的に、望んでいるエンコーディングにレンダリングされていることを 保証し、警告を避けるために、望んでいるエンコーディングでストリームを open してください。 いくつか例示します: open FH, ">:utf8", "file"; open FH, ">:encoding(ucs2)", "file"; open FH, ">:encoding(UTF-8)", "file"; open FH, ">:encoding(shift_jis)", "file"; =begin original and on already open streams, use C: =end original すでに開かれているストリームに関しては、C を使います: binmode(STDOUT, ":utf8"); binmode(STDOUT, ":encoding(ucs2)"); binmode(STDOUT, ":encoding(UTF-8)"); binmode(STDOUT, ":encoding(shift_jis)"); =begin original The matching of encoding names is loose: case does not matter, and many encodings have several aliases. Note that the C<:utf8> layer must always be specified exactly like that; it is I subject to the loose matching of encoding names. Also note that C<:utf8> is unsafe for input, because it accepts the data without validating that it is indeed valid UTF8. =end original エンコーディング名のマッチングはルーズです: 大文字小文字は重要ではなく、 多くのエンコーディングでは、いくつかの別名があります。 C<:utf8> 層は、常に、きっちりとそのように指定される必要があります。 このことは、エンコーディング名のルーズなマッチングの対象では I<ありません>。 データが妥当な UTF8 であるかどうかを検証せずに受け入れるので、入力に 対しては C<:utf8> は安全ではないことにも注意してください。 =begin original See L for the C<:utf8> layer, L and L for the C<:encoding()> layer, and L for many encodings supported by the C module. =end original C<:utf8> 層に関しては、L を参照してください; C<:encoding()> に関しては、L を参照してください; C モジュールでサポートされている、多くのエンコーディングに関しては、 L を参照してください。 =begin original Reading in a file that you know happens to be encoded in one of the Unicode or legacy encodings does not magically turn the data into Unicode in Perl's eyes. To do that, specify the appropriate layer when opening files =end original Unicode かレガシーのエンコーディングどちらかでたまたまエンコードされている ファイルを読み込んでも、Perl の目で、魔法のように、データが Unicode に 変わったりしません。 そうするためには、ファイルを開くときに、適切な層を指定します。 open(my $fh,'<:encoding(utf8)', 'anything'); my $line_of_unicode = <$fh>; open(my $fh,'<:encoding(Big5)', 'anything'); my $line_of_unicode = <$fh>; =begin original The I/O layers can also be specified more flexibly with the C pragma. See L, or look at the following example. =end original I/O 層は、もっと柔軟に、C プラグマでもまた、指定することが出来ます。 L を参照するか、次の例を見て下さい。 use open ':encoding(utf8)'; # input/output default encoding will be UTF-8 open X, ">file"; print X chr(0x100), "\n"; close X; open Y, "); # this should print 0x100 close Y; =begin original With the C pragma you can use the C<:locale> layer =end original C プラグマで、C<:local> 層も使えます: BEGIN { $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R' } # the :locale will probe the locale environment variables like LC_ALL use open OUT => ':locale'; # russki parusski open(O, ">koi8"); print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1 close O; open(I, "), "\n"; # this should print 0xc1 close I; =begin original These methods install a transparent filter on the I/O stream that converts data from the specified encoding when it is read in from the stream. The result is always Unicode. =end original ストリームからファイルが読まれるときに、特定のエンコーディングから、 データを変換する I/O ストリーム上の透過的な層です。 その結果は常に Unicode です。 =begin original The L pragma affects all the C calls after the pragma by setting default layers. If you want to affect only certain streams, use explicit layers directly in the C call. =end original L プラグマは、デフォルトの層を設定することで、プラグマの後の 全ての C 呼び出しに影響します。 あるストリームだけに影響を限定したいなら、C 呼び出しで明示的な 層を使って下さい。 =begin original You can switch encodings on an already opened stream by using C; see L. =end original C を使って、すでに開かれているストリームのエンコーディングを 変えることが出来ます; L を参照してください。 =begin original The C<:locale> does not currently (as of Perl 5.8.0) work with C and C, only with the C pragma. The C<:utf8> and C<:encoding(...)> methods do work with all of C, C, and the C pragma. =end original C<:locale> は、現在(Perl 5.8.0 の時点で)、C と C では 動きません。 C プラグマでのみ動きます。 C<:utf8> と C<:encoding(...)> メソッドは、全ての C、C、 C プラグマで動きます。 =begin original Similarly, you may use these I/O layers on output streams to automatically convert Unicode to the specified encoding when it is written to the stream. For example, the following snippet copies the contents of the file "text.jis" (encoded as ISO-2022-JP, aka JIS) to the file "text.utf8", encoded as UTF-8: =end original 似たようなものに、ストリームに書き出すときに、自動的に Unicode を 特定のエンコーディングに変換する、出力ストリームの I/O 層を使うかも 知れません。 例えば、次のようなコードの断片は、(ISO-2022-JP, またの名を JIS として エンコードされている)" text.jis" ファイルの内容を、UTF-8 として エンコードされる "text.utf8" ファイルにコピーします。 open(my $nihongo, '<:encoding(iso-2022-jp)', 'text.jis'); open(my $unicode, '>:utf8', 'text.utf8'); while (<$nihongo>) { print $unicode $_ } =begin original The naming of encodings, both by the C and by the C pragma allows for flexible names: C and C will both be understood. =end original C と、C プラグマの両方で使われているエンコーディングの 名前は、フレキシブルな名前でも使えます: Cと、C は、 両方とも理解されます。 =begin original Common encodings recognized by ISO, MIME, IANA, and various other standardisation organisations are recognised; for a more detailed list see L. =end original ISO、MIME、IANA、様々な他の標準化機関が認識している、一般的な エンコーディングが認識されます; より詳細なリストは、 L を参照してください。 =begin original C reads characters and returns the number of characters. C and C operate on byte counts, as do C and C. =end original C は、文字を読み、文字の数を返します。 C と C は、バイトカウントに関して操作します。 C と C も同様です。 =begin original Notice that because of the default behaviour of not doing any conversion upon input if there is no default layer, it is easy to mistakenly write code that keeps on expanding a file by repeatedly encoding the data: =end original デフォルト層がなければ、入力になんの変換もしないのがデフォルトの 振る舞いなので、繰り返しデータをエンコーディングすることで、 ファイルを展開し続けるコードを誤って書きやすいです。 # BAD CODE WARNING open F, "file"; local $/; ## read in the whole file of 8-bit characters $t = ; close F; open F, ">:encoding(utf8)", "file"; print F $t; ## convert to UTF-8 on output close F; =begin original If you run this code twice, the contents of the F will be twice UTF-8 encoded. A C would have avoided the bug, or explicitly opening also the F for input as UTF-8. =end original このコードを 2 回実行すると、F の内容は、UTF-8 に、2 回 エンコードされます。 C は、バグを避けるでしょう; もしくは、UTF-8 として入力するために、F を、明示的に開くことです。 =begin original B: the C<:utf8> and C<:encoding> features work only if your Perl has been built with the new PerlIO feature (which is the default on most systems). =end original B<注意>: C<:utf8> と C<:encoding> の機能は、Perl が新しい PerlIO の機能で ビルドされている場合(ほとんどのシステムで、それがデフォルトです)にのみ 動作します。 =head2 Displaying Unicode As Text (Unicode をテキストとして表示する) =begin original Sometimes you might want to display Perl scalars containing Unicode as simple ASCII (or EBCDIC) text. The following subroutine converts its argument so that Unicode characters with code points greater than 255 are displayed as C<\x{...}>, control characters (like C<\n>) are displayed as C<\x..>, and the rest of the characters as themselves: =end original Unicode を含んでいる Perl のスカラを、単純な ASCII(か、EBCDIC)の テキストとして、表示したいかもしれません。 下のサブルーチンは、引数を変換して、255 より大きいコードポイントの Unicode 文字を、C<\x{...}> として表示し、(C<\n> のような)制御文字は、 C<\x..> として表示します。 残りの文字は、そのまま表示します: sub nice_string { join("", map { $_ > 255 ? # if wide character... sprintf("\\x{%04X}", $_) : # \x{...} chr($_) =~ /[[:cntrl:]]/ ? # else if control character ... sprintf("\\x%02X", $_) : # \x.. quotemeta(chr($_)) # else quoted or as themselves } unpack("W*", $_[0])); # unpack Unicode characters } =begin original For example, =end original たとえば、 nice_string("foo\x{100}bar\n") =begin original returns the string =end original は、次のような文字列になり: 'foo\x{0100}bar\x0A' =begin original which is ready to be printed. =end original 表示可能になります。 =head2 Special Cases (特殊な状況) =over 4 =item * =begin original Bit Complement Operator ~ And vec() =end original 演算子 ~ と、vec() についてのちょっとした補足 =begin original The bit complement operator C<~> may produce surprising results if used on strings containing characters with ordinal values above 255. In such a case, the results are consistent with the internal encoding of the characters, but not with much else. So don't do that. Similarly for C: you will be operating on the internally-encoded bit patterns of the Unicode characters, not on the code point values, which is very probably not what you want. =end original 演算子 C<~> は、255 を超える序数の値の文字を含んでいる文字列で、 使われると、驚くべき結果になるでしょう。 そのようなケースでは、その結果は文字の内部的なエンコーディングで一貫性が あります。 しかし、他の多くでは違います。 ですので、そのようなことはしないでください。 C も同様です: コードポイントの値ではなく、内部的にエンコードされた、 Unicode 文字列のビットパターンを操作することになります; おそらく、それは望んだことではないでしょう。 =item * =begin original Peeking At Perl's Internal Encoding =end original Perl の内部エンコーディングをのぞき見る =begin original Normal users of Perl should never care how Perl encodes any particular Unicode string (because the normal ways to get at the contents of a string with Unicode--via input and output--should always be via explicitly-defined I/O layers). But if you must, there are two ways of looking behind the scenes. =end original Perl がどのように、特定の Unicode 文字列をエンコードしているかを、 Perl の普通のユーザは気にするべきではありません (Unicode で文字列の内容に達する普通の方法 -- 入出力によって -- は、常に 明示的に定義された I/O 層を経由すべきだからです)。 ですが、もし必要なら、隠れている裏側を見る 2 つの方法があります。 =begin original One way of peeking inside the internal encoding of Unicode characters is to use C to get the bytes of whatever the string encoding happens to be, or C to get the bytes of the UTF-8 encoding: =end original Unicode 文字の内部エンコーディングの内側を覗き見る方法の 1 つは、 エンコーディングが何であろうとも文字列のバイト列を得るために C を使うか、UTF-8 エンコーディングのバイト列を得るために C を使うことです: # this prints c4 80 for the UTF-8 bytes 0xc4 0x80 print join(" ", unpack("U0(H2)*", pack("U", 0x100))), "\n"; =begin original Yet another way would be to use the Devel::Peek module: =end original もう一つの方法は、Dvel::Peek モジュールを使うことです: perl -MDevel::Peek -e 'Dump(chr(0x100))' =begin original That shows the C flag in FLAGS and both the UTF-8 bytes and Unicode characters in C. See also later in this document the discussion about the C function. =end original これは、FLAGS の C フラグと、UTF-8 バイトと、C の中の Unicode 文字の両方を見せます。 このドキュメントの後にある、C 機能についての議論も 参照してください。 =back =head2 Advanced Topics (発展した話題) =over 4 =item * =begin original String Equivalence =end original 文字列の等価性 =begin original The question of string equivalence turns somewhat complicated in Unicode: what do you mean by "equal"? =end original 文字列の等価性の疑問は、Unicode でいくぶん複雑になります。 "等価" で、何を意味していますか? =begin original (Is C equal to C?) =end original (IS C と Cは、等しいでしょうか?) =begin original The short answer is that by default Perl compares equivalence (C, C) based only on code points of the characters. In the above case, the answer is no (because 0x00C1 != 0x0041). But sometimes, any CAPITAL LETTER As should be considered equal, or even As of any case. =end original 短く答えれば次のようになります。 デフォルトでは、Perl は(C と、C で)等価性を 比較しますが、これらは、文字のコードポイントでのみに基づいています。 上のケースでは、答えはいいえです(0x00C1 != 0x0041 ですから)。 しかし、どんな CAPITAL LETTER A も等しいと考えるべき時や、大文字小文字に 関わらずどんな A も等しいと考えるべき時もあります。 =begin original The long answer is that you need to consider character normalization and casing issues: see L, Unicode Technical Reports #15 and #21, I and I, http://www.unicode.org/unicode/reports/tr15/ and http://www.unicode.org/unicode/reports/tr21/ =end original 長く答えるなら、文字の正規化と大文字小文字の問題を考える必要があります: L、Unicode テクニカルレポート #15 と #21 である I と I ( http://www.unicode.org/unicode/reports/tr15/, http://www.unicode.org/unicode/reports/tr21/ ) を参照してください。 =begin original As of Perl 5.8.0, the "Full" case-folding of I is implemented. =end original Perl 5.8.0 から、I の、 "Full" ケースフォールディングが実装されています。 =item * =begin original String Collation =end original 文字列の照合(Collation) =begin original People like to see their strings nicely sorted--or as Unicode parlance goes, collated. But again, what do you mean by collate? =end original 文字列がうまくソートされている(あるいは Unicode の業界用語を使えば、 照合されている(collated))のを好みます。 ですが、再び、照合で何を意味していますか? =begin original (Does C come before or after C?) =end original (C は、 C より、 前でしょうか後でしょうか?) =begin original The short answer is that by default, Perl compares strings (C, C, C, C, C) based only on the code points of the characters. In the above case, the answer is "after", since C<0x00C1> > C<0x00C0>. =end original 短く答えれば次のようになります。 Perl は、文字列を、(C、C、C、C、C)で比較しますが、 これらは、文字のコードポイントでのみに基づいています。 上のケースでは、答えは、C<0x00C1> > C<0x00C0> ですので、"後"になります。 =begin original The long answer is that "it depends", and a good answer cannot be given without knowing (at the very least) the language context. See L, and I http://www.unicode.org/unicode/reports/tr10/ =end original 長く答えるなら、"依存して"います。 良い答えは、(とても少なくとも)言語のコンテキストを知らずには、 与えられません。 L, I http://www.unicode.org/unicode/reports/tr10/ を参照してください。 =back =head2 Miscellaneous (その他) =over 4 =item * =begin original Character Ranges and Classes =end original 文字の範囲とクラス =begin original Character ranges in regular expression character classes (C) and in the C (also known as C) operator are not magically Unicode-aware. What this means that C<[A-Za-z]> will not magically start to mean "all alphabetic letters"; not that it does mean that even for 8-bit characters, you should be using C in that case. =end original 正規表現の文字クラス(C)内と、C(C としても知られる) 演算子内では、自動的には Unicode 対応にはなりません。 これが意味することは、C<[A-Za-z]> は、自動的に "全てのアルファベット文字"を 意味するようにはなりません; 8 ビットの文字で意味します; このケースでは、C を使うべきです。 =begin original For specifying character classes like that in regular expressions, you can use the various Unicode properties--C<\pL>, or perhaps C<\p{Alphabetic}>, in this particular case. You can use Unicode code points as the end points of character ranges, but there is no magic associated with specifying a certain range. For further information--there are dozens of Unicode character classes--see L. =end original 正規表現内のこのような特定の文字クラスに、様々な Unicode プロパティ -- C<\pL>、または、この特別なケースでは、たぶん、C<\p{Alphabetic}> を 使うことができます。 文字の範囲の終わりのポイントとして、Unicode のコードポイントを 使うことが出来ます。 ですが、ある範囲を特定するのに結びつける何の魔法もありません。 詳しいことは--数ダースの Unicode 文字クラスがあります-- L を見て下さい。 =item * =begin original String-To-Number Conversions =end original 文字列から数字への変換 =begin original Unicode does define several other decimal--and numeric--characters besides the familiar 0 to 9, such as the Arabic and Indic digits. Perl does not support string-to-number conversion for digits other than ASCII 0 to 9 (and ASCII a to f for hexadecimal). =end original Unicode は、なじみのある、アラビアやインドの数字のような 0 から 9 に加えて、 複数の他の 10 進数--と数字の--文字を、定義します。 Perl は、ASCII の 0 から 9(と、16 進法の ASCII の a から f)以外の数字の、 文字列から数への変換をサポートしません。 =back =head2 Questions With Answers (質問と回答) =over 4 =item * =begin original Will My Old Scripts Break? =end original 古いスクリプトは壊れるでしょうか? =begin original Very probably not. Unless you are generating Unicode characters somehow, old behaviour should be preserved. About the only behaviour that has changed and which could start generating Unicode is the old behaviour of C where supplying an argument more than 255 produced a character modulo 255. C, for example, was equal to C or "-" (in ASCII), now it is LATIN CAPITAL LETTER I WITH BREVE. =end original たぶん、壊れません。 どうにかして、Unicode 文字を生成していなければ、古い挙動は保護されます。 振る舞いが変更され、Unicode を生成し始める唯一のものは、古い C は 引数として 255 を超える値が指定されると、255 を法とした文字が生成される というものです。 例えば、C は、C あるいは (ASCII の)"-" と同じでした。 現在ではそれは、LATIN CAPITAL LETTER I WITH BREVE です。 =item * =begin original How Do I Make My Scripts Work With Unicode? =end original 私のスクリプトを Unicode で動かすには? =begin original Very little work should be needed since nothing changes until you generate Unicode data. The most important thing is getting input as Unicode; for that, see the earlier I/O discussion. =end original Unicode データを生成するまで、ほとんど何もする必要はありません。 もっとも重要なものは、Unicode として、入力を得ることです; なので、以前の I/O の議論を見て下さい。 =item * =begin original How Do I Know Whether My String Is In Unicode? =end original Unicode の文字列かどうかを知るには? =begin original You shouldn't care. No, you really shouldn't. No, really. If you have to care--beyond the cases described above--it means that we didn't get the transparency of Unicode quite right. =end original 気にすることはありません。 ええ、本当に、気にするべきではありません。 ええ、本当に。 気にする必要があるなら -- このケースは上で書いています -- 本当に正しく、Unicode の透過性を得ていないことを意味します。 =begin original Okay, if you insist: =end original オーケー、どうしてもというなら: print utf8::is_utf8($string) ? 1 : 0, "\n"; =begin original But note that this doesn't mean that any of the characters in the string are necessary UTF-8 encoded, or that any of the characters have code points greater than 0xFF (255) or even 0x80 (128), or that the string has any characters at all. All the C does is to return the value of the internal "utf8ness" flag attached to the C<$string>. If the flag is off, the bytes in the scalar are interpreted as a single byte encoding. If the flag is on, the bytes in the scalar are interpreted as the (multi-byte, variable-length) UTF-8 encoded code points of the characters. Bytes added to an UTF-8 encoded string are automatically upgraded to UTF-8. If mixed non-UTF-8 and UTF-8 scalars are merged (double-quoted interpolation, explicit concatenation, and printf/sprintf parameter substitution), the result will be UTF-8 encoded as if copies of the byte strings were upgraded to UTF-8: for example, =end original しかし、これは、文字列の文字のすべてが UTF-8 でエンコードされているとか、 文字のすべてが 0xFF(255)(あるいは 0x80(128)) より大きいコードポイントを 持つとか、文字列に文字が含まれているかとかいうことを 意味するわけではないことに注意してください。 C がすることの全ては、C<$string> につけられている内部の "utf8ness" フラグの値を返すことです。 フラグが無効であれば、スカラ内のバイト列は、シングルバイト エンコーディングとして解釈されます。 フラグが有効であれば、スカラ内のバイト列は、(マルチバイト、可変長の) UTF-8 でエンコードされた文字のコードポイントとして解釈されます。 (ダブルクオートされた語句、明示的な連結、printf/sprintf パラメタ 置換によって)非 UTF-8 と、UTF-8 のスカラがマージされた場合、この結果は、 バイト文字列のコピーが UTF-8 で昇格されたかのように、 UTF-8 でエンコードされています: 例えば、 $a = "ab\x80c"; $b = "\x{100}"; print "$a = $b\n"; =begin original the output string will be UTF-8-encoded C, but C<$a> will stay byte-encoded. =end original 出力する文字列は UTF-8 エンコードされた C になりますが、 C<$a> は、バイトエンコードされたままです。 =begin original Sometimes you might really need to know the byte length of a string instead of the character length. For that use either the C function or the C pragma and its only defined function C: =end original 文字列の長さではなく、文字列のバイト長を知る必要があるかもしれません。 そのためには、C 関数か、C プラグマの どちらかを使い、定義された C 関数を使うだけです: my $unicode = chr(0x100); print length($unicode), "\n"; # will print 1 require Encode; print length(Encode::encode_utf8($unicode)), "\n"; # will print 2 use bytes; print length($unicode), "\n"; # will also print 2 # (the 0xC4 0x80 of the UTF-8) =item * =begin original How Do I Detect Data That's Not Valid In a Particular Encoding? =end original あるエンコーディングで、データが妥当でないことを検出するには? =begin original Use the C package to try converting it. For example, =end original C パッケージを使って、それを変換してみてください。 例えば、 use Encode 'decode_utf8'; eval { decode_utf8($string, Encode::FB_CROAK) }; if ($@) { # $string is valid utf8 } else { # $string is not valid utf8 } =begin original Or use C to try decoding it: =end original または C を使ってデコードしてみてください: use warnings; @chars = unpack("C0U*", $string_of_bytes_that_I_think_is_utf8); =begin original If invalid, a C warning is produced. The "C0" means "process the string character per character". Without that, the C would work in C mode (the default if the format string starts with C) and it would return the bytes making up the UTF-8 encoding of the target string, something that will always work. =end original 妥当でなければ、C の警告が出ます。 "C0" は、「文字列を文字単位で処理する」ことを意味します。 それ以外では、C は (フォーマット文字列が C で 始まっている場合のデフォルトである)C モードで動作し、ターゲット文字列の UTF08 エンコーディングでのバイト数を返し、このようなことは常に動作します。 =item * =begin original How Do I Convert Binary Data Into a Particular Encoding, Or Vice Versa? =end original バイナリデータを特定のエンコーディングに変換するには? また、その逆は? =begin original This probably isn't as useful as you might think. Normally, you shouldn't need to. =end original たぶん、思うようには有益ではないでしょう。 ふつう、必要とするべきではありません。 =begin original In one sense, what you are asking doesn't make much sense: encodings are for characters, and binary data are not "characters", so converting "data" into some encoding isn't meaningful unless you know in what character set and encoding the binary data is in, in which case it's not just binary data, now is it? =end original ある意味では、あなたが尋ねようとしていることはほとんど意味はありません: エンコーディングは文字のためのもので、バイナリデータは「文字」ではないので、 「データ」を何かのエンコーディングに変換するということは、 バイナリデータの文字セットとエンコーディングが分かっていない限り無意味です; そして分かっているならそれは単なるバイナリデータではないですよね? =begin original If you have a raw sequence of bytes that you know should be interpreted via a particular encoding, you can use C: =end original どのエンコーディングで解釈するべきか分かっている生のバイト列がある場合、 C が使えます: use Encode 'from_to'; from_to($data, "iso-8859-1", "utf-8"); # from latin-1 to utf-8 =begin original The call to C changes the bytes in C<$data>, but nothing material about the nature of the string has changed as far as Perl is concerned. Both before and after the call, the string C<$data> contains just a bunch of 8-bit bytes. As far as Perl is concerned, the encoding of the string remains as "system-native 8-bit bytes". =end original C の呼び出しは C<$data> のバイト列を変更しますが、Perl が 関知する限りにおいては文字列の性質は何も変わりません。 呼び出しの前後の両方において、文字列 C<$data> は単に 8 ビットのバイトの 塊です。 Perl が関知するかぎりにおいては、文字列のエンコーディングは 「システムにネイティブな 8 ビットのバイト列」のままです。 =begin original You might relate this to a fictional 'Translate' module: =end original これを架空の 'Translate' モジュールと関連付けることもできます: use Translate; my $phrase = "Yes"; Translate::from_to($phrase, 'english', 'deutsch'); ## phrase now contains "Ja" =begin original The contents of the string changes, but not the nature of the string. Perl doesn't know any more after the call than before that the contents of the string indicates the affirmative. =end original 文字列の内容は変わりますが、文字列の性質は変わりません。 Perl は文字列の内容が肯定を意味するという呼び出し前の状態よりも多く、 呼び出しによって何かを知ることはありません。 =begin original Back to converting data. If you have (or want) data in your system's native 8-bit encoding (e.g. Latin-1, EBCDIC, etc.), you can use pack/unpack to convert to/from Unicode. =end original データの変換に戻ります。 もしシステムのネイティブな 8 ビットエンコーディング(例えば、Latin-1, EBCDIC など)のデータを持っている(あるいは欲しい)場合は、Unicode との変換に pack/unpack を使えます。 $native_string = pack("W*", unpack("U*", $Unicode_string)); $Unicode_string = pack("U*", unpack("W*", $native_string)); =begin original If you have a sequence of bytes you B is valid UTF-8, but Perl doesn't know it yet, you can make Perl a believer, too: =end original あなたはバイト列が妥当な UTF-8 であると B<分かっている> けれども、Perl が 知らない場合、Perl に信じさせることもできます: use Encode 'decode_utf8'; $Unicode = decode_utf8($bytes); =begin original or: =end original または: $Unicode = pack("U0a*", $bytes); =begin original You can convert well-formed UTF-8 to a sequence of bytes, but if you just want to convert random binary data into UTF-8, you can't. B. You can use C for the former, and you can create well-formed Unicode data by C. =end original 整形された UTF-8 はバイト列に変換できますが、単にランダムなバイナリデータを UTF-8 に変換したいのなら、それはできません。 B<全てのランダムなバイト列が整形された UTF-8 にはなりません。>. 前者のためには C が使えますし、 C を使って整形された Unicode データを作成できます。 =item * =begin original How Do I Display Unicode? How Do I Input Unicode? =end original Unicode を表示するには? Unicode を入力するには? =begin original See http://www.alanwood.net/unicode/ and http://www.cl.cam.ac.uk/~mgk25/unicode.html =end original http://www.alanwood.net/unicode/ と http://www.cl.cam.ac.uk/~mgk25/unicode.html を参照してください。 =item * =begin original How Does Unicode Work With Traditional Locales? =end original 伝統的なロケールと Unicode は、どのように動きますか? =begin original In Perl, not very well. Avoid using locales through the C pragma. Use only one or the other. But see L for the description of the C<-C> switch and its environment counterpart, C<$ENV{PERL_UNICODE}> to see how to enable various Unicode features, for example by using locale settings. =end original Perl では、あまりうまくありません。 C プラグマでロケールを使うことを避けて下さい。 どちらか片方だけを使ってください。 しかし、C<-C> スイッチと、それに対応する環境変数である C<$ENV{PERL_UNICODE}> の説明のために、L を参照してください; 様々な Unicode の機能が、どのように可能になっているのか、たとえば、 ロケールの設定によって、が、わかります。 =back =head2 Hexadecimal Notation (16 進記法) =begin original The Unicode standard prefers using hexadecimal notation because that more clearly shows the division of Unicode into blocks of 256 characters. Hexadecimal is also simply shorter than decimal. You can use decimal notation, too, but learning to use hexadecimal just makes life easier with the Unicode standard. The C notation uses hexadecimal, for example. =end original Unicode 標準は、16 進記法を使うのを好みます。 それは、256 の文字のブロックに Unicode を分割しているのが、 他の表記よりわかりやすいからです。 10 進記法を使うことも出来ますが、16 進記法を使うことを学べば、 Unicode 標準との暮らしが楽になります。 例えば、C 表記は、16 進記法を使います。 =begin original The C<0x> prefix means a hexadecimal number, the digits are 0-9 I a-f (or A-F, case doesn't matter). Each hexadecimal digit represents four bits, or half a byte. C will show a hexadecimal number in decimal, and C will show a decimal number in hexadecimal. If you have just the "hex digits" of a hexadecimal number, you can use the C function. =end original C<0x> の接頭辞は、16 進の数字を意味しています。 数字は、0-9 I<および> a-f(か、A-F、大文字小文字は問いません)です。 それぞれの 16 進の数字は 4 ビット、1/2 バイトを表します。 C は 16 進数を 10 進で見せます。 C は 10 進数を 16 進で見せます。 "hex digits" の 16 進の数字があるなら、C 関数を使うことが出来ます。 print 0x0009, "\n"; # 9 print 0x000a, "\n"; # 10 print 0x000f, "\n"; # 15 print 0x0010, "\n"; # 16 print 0x0011, "\n"; # 17 print 0x0100, "\n"; # 256 print 0x0041, "\n"; # 65 printf "%x\n", 65; # 41 printf "%#x\n", 65; # 0x41 print hex("41"), "\n"; # 65 =head2 Further Resources (更なるリソース) =over 4 =item * Unicode Consortium http://www.unicode.org/ =item * Unicode FAQ http://www.unicode.org/unicode/faq/ =item * Unicode Glossary http://www.unicode.org/glossary/ =item * Unicode Useful Resources http://www.unicode.org/unicode/onlinedat/resources.html =item * Unicode and Multilingual Support in HTML, Fonts, Web Browsers and Other Applications http://www.alanwood.net/unicode/ =item * UTF-8 and Unicode FAQ for Unix/Linux http://www.cl.cam.ac.uk/~mgk25/unicode.html =item * Legacy Character Sets http://www.czyborra.com/ http://www.eki.ee/letter/ =item * =begin original The Unicode support files live within the Perl installation in the directory =end original Unicode サポートのファイルは、Perl をインストールしたディレクトリ内に あります; $Config{installprivlib}/unicore =begin original in Perl 5.8.0 or newer, and =end original Perl 5.8.0 以上ではここです; $Config{installprivlib}/unicode =begin original in the Perl 5.6 series. (The renaming to F was done to avoid naming conflicts with lib/Unicode in case-insensitive filesystems.) The main Unicode data file is F (or F in Perl 5.6.1.) You can find the C<$Config{installprivlib}> by =end original Perl 5.6 シリーズではここです。 (大文字小文字を区別しないファイルシステムでは、lib/Unicode との名前の衝突を 避けるために、F にリネームされています。) メインの Unicode データファイルは、F(か、Perl 5.6.1 の F)です。 次のようにして、C<$Config{installprivlib}> を見つけることができます。 perl "-V:installprivlib" =begin original You can explore various information from the Unicode data files using the C module. =end original C モジュールを使って、Unicode データファイルから 様々な情報を調べることができます。 =back =head1 UNICODE IN OLDER PERLS (古い Perl での Unicode) =begin original If you cannot upgrade your Perl to 5.8.0 or later, you can still do some Unicode processing by using the modules C, C, and C, available from CPAN. If you have the GNU recode installed, you can also use the Perl front-end C for character conversions. =end original Perl 5.8.0 以降にアップグレード出来なくても、まだ、CPAN から利用できる、 C、C、C を使って、 いくつかの Unicode 処理ができます。 GNU recode がインストールされているなら、それの Perl フロントエンドである C を文字変換のために使えます。 =begin original The following are fast conversions from ISO 8859-1 (Latin-1) bytes to UTF-8 bytes and back, the code works even with older Perl 5 versions. =end original 下記のものは、ISO 8859-1 (Latin-1) バイト列から UTF-8 バイト列に (あるいはその逆に)素早く変換するものです。 このコードは古い Perl 5 でも動きます。 # ISO 8859-1 to UTF-8 s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg; # UTF-8 to ISO 8859-1 s/([\xC2\xC3])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg; =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L, L =head1 ACKNOWLEDGMENTS (謝辞) Thanks to the kind readers of the perl5-porters@perl.org, perl-unicode@perl.org, linux-utf8@nl.linux.org, and unicore@unicode.org mailing lists for their valuable feedback. =head1 AUTHOR, COPYRIGHT, AND LICENSE (著者、著作権、ライセンス) Copyright 2001-2002 Jarkko Hietaniemi Ejhi@iki.fiE This document may be distributed under the same terms as Perl itself. =begin meta Translate: ktats (5.8.1) Update: SHIRAKATA Kentaro (5.10.0-) Status: in progress =end meta