utf8-1.07 > utf8
utf8-1.07
Other versions:
utf8-1.08
utf8-1.02

名前

utf8 - Perl pragma to enable/disable UTF-8 (or UTF-EBCDIC) in source code

utf8 - ソースコード内に、UTF-8(か、UTF-EBCDIC)を有効/無効にするためのプラグマ

概要

    use utf8;
    no utf8;

    # Convert a Perl scalar to/from UTF-8.
    $num_octets = utf8::upgrade($string);
    $success    = utf8::downgrade($string[, FAIL_OK]);

    # Change the native bytes of a Perl scalar to/from UTF-8 bytes.
    utf8::encode($string);
    utf8::decode($string);

    $flag = utf8::is_utf8(STRING); # since Perl 5.8.1
    $flag = utf8::valid(STRING);

説明

The use utf8 pragma tells the Perl parser to allow UTF-8 in the program text in the current lexical scope (allow UTF-EBCDIC on EBCDIC based platforms). The no utf8 pragma tells Perl to switch back to treating the source text as literal bytes in the current lexical scope.

use utf8 プラグマは、Perl パーサーに、現在のレキシカルスコープ内の プログラムのテキストに、UTF-8(EBCDIC ベースのプラットフォームなら、UTF-EBCDIC) を許すように伝えます。 no utf8 プラグマは Perl に、現在のレキシカルスコープのソーステキストを リテラルなバイトとして扱うように伝えます。

Do not use this pragma for anything else than telling Perl that your script is written in UTF-8. The utility functions described below are directly usable without use utf8;.

スクリプトが UTF-8 で書かれているということを Perl に教える目的以外で このプラグマをつかってはいけません。 下に書かれているユーティリティ関数は、use utf8; なしで 直接使えます。

Because it is not possible to reliably tell UTF-8 from native 8 bit encodings, you need either a Byte Order Mark at the beginning of your source code, or use utf8;, to instruct perl.

ネイティブな 8 ビットエンコーディングから UTF-8 であると確実に 知らせることは不可能なので、perl に知らせるためにソースコードの先頭に バイト順マークを置くか、use utf8; を指定するかのどちらかが必要です。

When UTF-8 becomes the standard source format, this pragma will effectively become a no-op. For convenience in what follows the term UTF-X is used to refer to UTF-8 on ASCII and ISO Latin based platforms and UTF-EBCDIC on EBCDIC based platforms.

UTF-8 が標準ソースフォーマットとなったときには、 このプラグマは事実上何もしなくなります。 利便性のために、以下での UTF-X という単語は、 ASCII と ISO Latin ベースのプラットフォームでは UTF-8 を示し、 EBCDIC ベースのプラットフォームでは UTF-EBCDIC を示すものとします。

See also the effects of the -C switch and its cousin, the $ENV{PERL_UNICODE}, in perlrun.

perlrun にある、-C スイッチ及びそのいとこの $ENV{PERL_UNICODE} の 効果も参照してください。

Enabling the utf8 pragma has the following effect:

utf8 プラグマを有効にすると次の効果があります:

  • Bytes in the source text that have their high-bit set will be treated as being part of a literal UTF-X sequence. This includes most literals such as identifier names, string constants, and constant regular expression patterns.

    高ビットのセットのあるソーステキストにあるバイトは、 リテラルの UTF-X シーケンスの部分として扱われます。 このことは、ほとんどのリテラル、識別子の名前、 文字列定数、正規表現パターン定数も含みます。

    On EBCDIC platforms characters in the Latin 1 character set are treated as being part of a literal UTF-EBCDIC character.

    EBCDIC プラットフォームでは、Latin 1 文字集合の文字は、 リテラルの UTF-EBCDIC の文字として扱われます。

Note that if you have bytes with the eighth bit on in your script (for example embedded Latin-1 in your string literals), use utf8 will be unhappy since the bytes are most probably not well-formed UTF-X. If you want to have such bytes under use utf8, you can disable this pragma until the end the block (or file, if at top level) by no utf8;.

スクリプト内に 8 ビットのバイト列がある(たとえば、文字列リテラルに Latin-1 がある)なら、use utf8 は不幸をもたらすでしょう; バイト列は、ほとんどの場合、適切な UTF-X ではないからです。 use utf8 内でそのようなバイト列を使いたい場合は、ブロック(または、 最上部に書けば、ファイル)の終わりまで、no utf8; で、utf8 を 無効にできます。

ユーティリティ関数

The following functions are defined in the utf8:: package by the Perl core. You do not need to say use utf8 to use these and in fact you should not say that unless you really want to have UTF-8 source code.

下記の関数は、Perl core によって、utf8:: パッケージに定義されています。 これらの関数を使うのに、use utf8 する必要はありませんし、 実際、本当に UTF-8 のソースコードを書こうとするのでないなら、 use utf8 すべきではありません。

  • $num_octets = utf8::upgrade($string)

    Converts in-place the internal octet sequence in the native encoding (Latin-1 or EBCDIC) to the equivalent character sequence in UTF-X. $string already encoded as characters does no harm. Returns the number of octets necessary to represent the string as UTF-X. Can be used to make sure that the UTF-8 flag is on, so that \w or lc() work as Unicode on strings containing characters in the range 0x80-0xFF (on ASCII and derivatives).

    ネイティブエンコーディング (Latin-1 や EBCDIC) の内部オクテットシーケンスを、 等価な UTF-X の文字シーケンスに適切に変換します。 $string が既に文字としてエンコードされている場合は何もしません。 UTF-X として、文字列を表現するのに必要なオクテットの数を返します。 UTF-8 フラグがオンであることを確認するのに使えます; ですので、\wlc() は、(ASCII とその派生では) 0x80-0xFF の 文字を含む文字列で期待されるように動きます。

    Note that this function does not handle arbitrary encodings. Therefore Encode is recommended for the general purposes; see also Encode.

    この関数は、任意のエンコーディングを扱えるわけではないことに 注意してください。 従って、一般的な用途では Encode を推奨します; Encode も 参照してください。

  • $success = utf8::downgrade($string[, FAIL_OK])

    Converts in-place the internal octet sequence in UTF-X to the equivalent octet sequence in the native encoding (Latin-1 or EBCDIC). $string already encoded as native 8 bit does no harm. Can be used to make sure that the UTF-8 flag is off, e.g. when you want to make sure that the substr() or length() function works with the usually faster byte algorithm.

    UTF-Xの内部オクテットシーケンスを、 等価なネイティブエンコーディング (Latin-1 や EBCDIC) の 内部オクテットシーケンスに適切に変換します。 $string が既にネイティブな 8 ビットとしてエンコードされている場合は 何もしません。 たとえば、substr() や length()関数が、 通常はより速いバイトアルゴリズムで動くようにしたい場合、 UTF-8フラグがオフであるのを確認するのに使えます。

    Fails if the original UTF-X sequence cannot be represented in the native 8 bit encoding. On failure dies or, if the value of FAIL_OK is true, returns false.

    元の UTF-X シーケンスがネイティブな 8 ビットエンコーディングで 表現できない場合は失敗します。 失敗したときには、FAIL_OK の値が真の場合は偽を返し、そうでない場合は die します。

    Returns true on success.

    成功すると真を返します。

    Note that this function does not handle arbitrary encodings. Therefore Encode is recommended for the general purposes; see also Encode.

    この関数は、任意のエンコーディングを扱えるわけではないことに 注意してください。 従って、一般的な用途では Encode を推奨します; Encode も 参照してください。

  • utf8::encode($string)

    Converts in-place the character sequence to the corresponding octet sequence in UTF-X. The UTF8 flag is turned off, so that after this operation, the string is a byte string. Returns nothing.

    文字シーケンスを、対応する UTF-X のオクテットシーケンスに 適切に変換します。 UTF8 フラグはオフになるので、この操作の後、文字列は バイト文字列になります。 何も返しません。

    Note that this function does not handle arbitrary encodings. Therefore Encode is recommended for the general purposes; see also Encode.

    この関数は、任意のエンコーディングを扱えるわけではないことに 注意してください。 従って、一般的な用途では Encode を推奨します; Encode も 参照してください。

  • $success = utf8::decode($string)

    Attempts to convert in-place the octet sequence in UTF-X to the corresponding character sequence. The UTF-8 flag is turned on only if the source string contains multiple-byte UTF-X characters. If $string is invalid as UTF-X, returns false; otherwise returns true.

    UTF-X のオクテットシーケンスを、対応する文字シーケンスに 適切に変換しようとします。 UTF-8 フラグは、元の文字列が複数バイトの UTF-X 文字を含んでいる場合にのみ オンになります。 もし $stringUTF-X として不正なら、偽を返します; さもなければ真を返します。

    Note that this function does not handle arbitrary encodings. Therefore Encode is recommended for the general purposes; see also Encode.

    この関数は、任意のエンコーディングを扱えるわけではないことに 注意してください。 従って、一般的な用途では Encode を推奨します; Encode も 参照してください。

  • $flag = utf8::is_utf8(STRING)

    (Since Perl 5.8.1) Test whether STRING is in UTF-8 internally. Functionally the same as Encode::is_utf8().

    (Perl 5.8.1から) STRING が、内部的に UTF-8 かどうかをテストします。 機能的には、Encode::is_utf8() と同じです。

  • $flag = utf8::valid(STRING)

    [INTERNAL] Test whether STRING is in a consistent state regarding UTF-8. Will return true is well-formed UTF-8 and has the UTF-8 flag on or if string is held as bytes (both these states are 'consistent'). Main reason for this routine is to allow Perl's testsuite to check that operations have left strings in a consistent state. You most probably want to use utf8::is_utf8() instead.

    [内部関数] STRING が UTF-8 として一貫しているかを調べます。 STRING が well-formed UTF-8 で UTF-8 フラグがオンの場合 または 文字列がバイトとして保持されている場合に真を返します (両方とも状態は「一貫して」います)。 このルーチンの主目的は、 ある操作が文字列を一貫した状態に保っているかをPerl のテストスイートが 調べられるようにすることです。 おそらくたいていの場合は代わりに utf8::is_utf8() を使いたいはずです。

utf8::encode is like utf8::upgrade, but the UTF8 flag is cleared. See perlunicode for more on the UTF8 flag and the C API functions sv_utf8_upgrade, sv_utf8_downgrade, sv_utf8_encode, and sv_utf8_decode, which are wrapped by the Perl functions utf8::upgrade, utf8::downgrade, utf8::encode and utf8::decode. Also, the functions utf8::is_utf8, utf8::valid, utf8::encode, utf8::decode, utf8::upgrade, and utf8::downgrade are actually internal, and thus always available, without a require utf8 statement.

utf8::encode は、utf8::upgrade に似ていますが、UTF8 フラグが 削除されます。 UTF8 フラグの詳細および、Perl 関数の utf8::upgrade, utf8::downgrade, utf8::encode, utf8::decode でラッピングされている C API 関数 sv_utf8_upgrade, sv_utf8_downgrade, sv_utf8_encode, sv_utf8_decode については perlunicode を 参照してください。 また、utf8::is_utf8, utf8::valid, utf8::encode, utf8::decode, utf8::upgrade, utf8::downgrade は、 実際には内部関数なので、常に require utf8 なしで利用可能になっています。

バグ

One can have Unicode in identifier names, but not in package/class or subroutine names. While some limited functionality towards this does exist as of Perl 5.8.0, that is more accidental than designed; use of Unicode for the said purposes is unsupported.

識別子の名前として Unicode を使えますが、パッケージ/クラスや サブルーチンの名前としては使えません。 Perl 5.8.0 からはこれに関する限定された機能が存在しますが、 これらは予定されたものというよりは偶然の産物です; このような目的で Unicode を使うことは非サポートです。

One reason of this unfinishedness is its (currently) inherent unportability: since both package names and subroutine names may need to be mapped to file and directory names, the Unicode capability of the filesystem becomes important-- and there unfortunately aren't portable answers.

この不完全性の原因の一つは(現在のところ)先天的な非移植性です; パッケージ名とサブルーチン名はそれぞれファイルとディレクトリ名に 対応付けられる必要があるので、ファイルシステムの Unicode 対応が 重要となります -- そして残念ながら移植性のある答えが存在しません。

SEE ALSO

perlunitut, perluniintro, perlrun, bytes, perlunicode