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

名前

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

概要

    use utf8;
    no utf8;

    $num_octets = utf8::upgrade($string);
    $success    = utf8::downgrade($string[, FAIL_OK]);

    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 に、現在のレキシカルスコープのソーステキストを リテラルなバイトとして扱うように伝えます。

This pragma is primarily a compatibility device. Perl versions earlier than 5.6 allowed arbitrary bytes in source code, whereas in future we would like to standardize on the UTF-8 encoding for source text.

このプラグマは、そもそも、互換性のための工夫です。 Perl 5.6未満のバージョンでは、ソースコード中の恣意的なバイト列を許していました。 ですが、将来、ソーステキストに、UTF-8 エンコーディングを標準化したいと思っています。

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 useful for their own purposes, but they are not really part of the "pragmatic" effect.

Perl に script が UTF-8 で書かれているということを教える目的以外で このプラグマをつかってはいけません。 下に書かれている、utf8のユーティリティ関数は、それ自身の目的で有益です。 しかしそれらは、"プラグマ的な"効果の部分ではまったくありません。

Until UTF-8 becomes the default format for source text, either this pragma or the "encoding" pragma should be used to recognize UTF-8 in the source. 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-8 を認識させるためにこのプラグマか "encoding" プラグマを 使うべきです。 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.

-Cスイッチとそのいとこの、$ENV{PERL_UNICODE}を、perlrunで見て下さい。

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-8 character. This includes most literals such as identifier names, string constants, and constant regular expression patterns.

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

    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-8. If you want to have such bytes and use utf8, you can disable utf8 until the end the block (or file, if at top level) by no utf8;.

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

If you want to automatically upgrade your 8-bit legacy bytes to UTF-8, use the "encoding" pragma instead of this pragma. For example, if you want to implicitly upgrade your ISO 8859-1 (Latin-1) bytes to UTF-8 as used in e.g. chr() and \x{...}, try this:

自動的に、8bitレガシーのバイト列をUTF-8にアップグレードしたいなら、 "encoding" プラグマを、このプラグマの代わりに使ってください。 たとえば、例で使われているように、ISO 8859-1 (Latin-1) のバイト列を UTF-8に暗にアップグレードしたいなら、chr()\x{...}を使います。 次のようにします:

    use encoding "latin-1";
    my $c = chr(0xc4);
    my $x = "\x{c5}";

In case you are wondering: yes, use encoding 'utf8'; works much the same as use utf8;.

迷っている場合:こうしてください。 use encoding 'utf8';は、use 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.

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

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

    Converts (in-place) internal representation of string to Perl's internal UTF-X form. 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 expected on strings containing characters in the range 0x80-0xFF (oon ASCII and derivatives). Note that this should not be used to convert a legacy byte encoding to Unicode: use Encode for that. Affected by the encoding pragma.

    文字列の内部表現を、Perlの内部的なUTF-Xの形式に(適切に)変換します。 UTF-Xとして、文字列を表現するのに必要な8ビットの数を返します。 UTF-8フラグがオンであることを確認するのに使えます。 ですので、\wか、lc()は、0x80-0xFF(ASCIIとその派生) のキャラクタを含む文字列で期待されるように動きます。 次のことに注意してください。レガシーのバイトエンコーディングを、 Unicode にする変換に使うべきではありません: そのためには、Encode を使って下さい。encoding プラグマに影響を受けます。

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

    Converts (in-place) internal representation of string to be un-encoded bytes. Returns true on success. On failure dies or, if the value of FAIL_OK is true, returns false. 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. Note that this should not be used to convert Unicode back to a legacy byte encoding: use Encode for that. Not affected by the encoding pragma.

    文字列の内部表現を、エンコードされていないバイト列に(適切に)変換します。 成功すると真を返します。失敗すると、死ぬか、FAIL_OKが真であれば、 偽を返します。たとえば、substr() か length()関数が、 通常はより速いバイトアルゴリズムで動くようにしたい場合、 UTF-8フラグがオフであるのを確認するのに使えます。 次のことに注意してください。Unicode をレガシーのバイトエンコーディングに 戻す変換に使うべきではありません: そのためには、Encode を使って下さい。 encodingプラグマによって影響を受けません

  • utf8::encode($string)

    Converts (in-place) $string from logical characters to octet sequence representing it in Perl's UTF-X encoding. Returns nothing. Same as Encode::encode_utf8(). Note that this should not be used to convert a legacy byte encoding to Unicode: use Encode for that.

    論理キャラクタを、8ビットシーケンス表現に、PerlのUTF-Xエンコーディングに、 (適切に)変換します。何も返しません。 Encode::encode_utf8()と同じです。 次のことに注意してください。レガシーのバイトエンコーディングを、 Unicode にする変換に使うべきではありません:そのためには、Encode を使って下さい。

  • utf8::decode($string)

    Attempts to convert $string in-place from Perl's UTF-X encoding into logical characters. Returns nothing. Same as Encode::decode_utf8(). Note that this should not be used to convert Unicode back to a legacy byte encoding: use Encode for that.

    $stringを、PerlのUTF-Xエンコーディングから論理キャラクタに、 適切に、変換しようとします。何も返しません。 Encode::decode_utf8()と同じです。 次のことに注意してください。Unicode をレガシーのバイトエンコーディングに 戻す変換に使うべきではありません: そのためには、Encode を使って下さい。

  • $flag = utf8::is_utf8(STRING)

    (Since Perl 5.8.1) Test whether STRING is in UTF-8. 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 フラグが on の場合 または 文字列がバイトとして保持されている場合に真を返します (両方とも状態は「一貫して」います)。 このルーチンの主目的は、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. Note that in the Perl 5.8.0 and 5.8.1 implementation the functions utf8::is_utf8, utf8::valid, utf8::encode, utf8::decode, utf8::upgrade, and utf8::downgrade are always available, without a require utf8 statement-- this may change in future releases.

utf8::encode は、utf8::upgrade に似ています、 ですが、UTF8 フラグが削除されます。 UTF8フラグの詳細は、perlunicode を見て下さい。 そして、C API 関数 sv_utf8_upgradesv_utf8_downgradesv_utf8_encodesv_utf8_decodeは、 Perl関数の、utf8::upgradeutf8::downgradeutf8::encodeutf8::decode でラッピングされています。 Perl 5.8.0 と、5.8.1 の実装で、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

perluniintro, encoding, perlrun, bytes, perlunicode