open-1.02 > open

名前

open - perl pragma to set default PerlIO layers for input and output

open - 入出力のためにデフォルトのPerlIOレイヤセットするためのPerlのプラグマ

概要

    use open IN  => ":crlf", OUT => ":bytes";
    use open OUT => ':utf8';
    use open IO  => ":encoding(iso-8859-7)";

    use open IO  => ':locale';

    use open ':utf8';
    use open ':locale';
    use open ':encoding(iso-8859-7)';

    use open ':std';

説明

Full-fledged support for I/O layers is now implemented provided Perl is configured to use PerlIO as its IO system (which is now the default).

I/Oレイヤの徹底したサポートが現在実装され、提供されています。 PerlはそのIOシステムとしてPerlIOを使っています(現在、PerlIOはデフォルトです)。

The open pragma serves as one of the interfaces to declare default "layers" (also known as "disciplines") for all I/O. Any two-argument open(), readpipe() (aka qx//) and similar operators found within the lexical scope of this pragma will use the declared defaults. Three-argument opens are not affected by this pragma since there you (can) explicitly specify the layers and are supposed to know what you are doing.

openプラグマは全てのI/Oのデフォルトの"レイヤ"("disciplines"としても知られています) へのインターフェースの一つとしての役目をします。 このプラグマのレキシカルコープ内の、全ての二つの引数を取る、open()、 readpipe()(qx// としても知られる)、 それらと類似のオペレータは、宣言されたデフォルトを使うようになります。 三つの引数を取るopenなどは、明示的にレイヤを指定する(ことができる)ため、 自分がすることをわかっていると仮定するので、このプラグマは影響しません。

With the IN subpragma you can declare the default layers of input streams, and with the OUT subpragma you can declare the default layers of output streams. With the IO subpragma you can control both input and output streams simultaneously.

INサブプラグマで入力ストリームのデフォルトのレイヤを宣言できます。 OUTサブプラグマで出力ストリームのデフォルトのレイヤを宣言できます。 IOサブプラグマで入出力ストリームの両方を同時に制御できます。

If you have a legacy encoding, you can use the :encoding(...) tag.

レガシーのエンコーディングの場合、:encoding(...)タグが使えます。

if you want to set your encoding layers based on your locale environment variables, you can use the :locale tag. For example:

ロケールの環境変数でエンコーディングレイヤをセットするならば、:localeタグを使えます。 例:

    $ENV{LANG} = 'ru_RU.KOI8-R';
    # the :locale will probe the locale environment variables like LANG
    use open OUT => ':locale';
    open(O, ">koi8");
    print O chr(0x430); # Unicode CYRILLIC SMALL LETTER A = KOI8-R 0xc1
    close O;
    open(I, "<koi8");
    printf "%#x\n", ord(<I>), "\n"; # this should print 0xc1
    close I;

These are equivalent

次のものは同じです。

    use open ':utf8';
    use open IO => ':utf8';

as are these

これも同じです。

    use open ':locale';
    use open IO => ':locale';

and these

こちらも同じです。

    use open ':encoding(iso-8859-7)';
    use open IO => ':encoding(iso-8859-7)';

The matching of encoding names is loose: case does not matter, and many encodings have several aliases. See Encode::Supported for details and the list of supported locales.

エンコーディング名のマッチングはゆるいです:大文字小文字は問題になりませんし、 多くのエンコーディングに複数のエイリアスがあります。 詳細とサポートされているロケールのリストは、Encode::Supportedにあります。

Note that :utf8 PerlIO layer must always be specified exactly like that, it is not subject to the loose matching of encoding names.

次のことに注意して下さい。 :utf8 PerlIOレイヤは、常に厳密に指定されなければいけません。 これについてはエンコーディング名のゆるいマッチングの対象になっていません。

When open() is given an explicit list of layers they are appended to the list declared using this pragma.

open()に明示的なレイヤのリストが与えられた場合、 それらはこのプラグマを使って宣言されたリストに加えられます。

The :std subpragma on its own has no effect, but if combined with the :utf8 or :encoding subpragmas, it converts the standard filehandles (STDIN, STDOUT, STDERR) to comply with encoding selected for input/output handles. For example, if both input and out are chosen to be :utf8, a :std will mean that STDIN, STDOUT, and STDERR are also in :utf8. On the other hand, if only output is chosen to be in :encoding(koi8r), a :std will cause only the STDOUT and STDERR to be in koi8r. The :locale subpragma implicitly turns on :std.

:stdサブプラグマはそれ自身は何もしません。 ですが、:utf8:encodingサブプラグマと組み合わせた場合、 それは、標準のファイルハンドル(STDIN, STDOUT, STDERR)を input/output ハンドルで選ばれたエンコーディングに従うように変換します。 一方で、出力だけ:encoding(koi8r)であるように選ばれたなら、 :stdは、STDOUTとSTDERRだけがkoi8rになるようになります。 :localeサブプラグマは暗に:stdをつけています。 (訳注:組み合わせるというのは、use open ":utf8"; などの後に、 use open ":std" するということです)

The logic of :locale is as follows:

:localeのロジックは下記のようになっています:

  1. If the platform supports the langinfo(CODESET) interface, the codeset returned is used as the default encoding for the open pragma.

    プラットフォームがlanginfo(CODESET)インターフェースをサポートするなら、 コードセットは、open プラグマのデフォルトのエンコーディングとして使われます。

  2. If 1. didn't work but we are under the locale pragma, the environment variables LC_ALL and LANG (in that order) are matched for encodings (the part after ., if any), and if any found, that is used as the default encoding for the open pragma.

    1. がうまくいかないないが、localeプラグマのもとで、 環境変数LC_ALLとLANGが(この順番で)エンコーディング(the part after ., if any)にマッチし、 見つかったなら、open プラグマのデフォルトのエンコーディングとして使われます。

  3. If 1. and 2. didn't work, the environment variables LC_ALL and LANG (in that order) are matched for anything looking like UTF-8, and if any found, :utf8 is used as the default encoding for the open pragma.

    1. と 2. がうまくいかず、環境変数LC_ALLとLANG(がこの順で)UTF-8にみえるものにマッチし、 見つかったなら、:utf8がopen プラグマのでふぉるtのエンコーディングとして使われます。

If your locale environment variables (LC_ALL, LC_CTYPE, LANG) contain the strings 'UTF-8' or 'UTF8' (case-insensitive matching), the default encoding of your STDIN, STDOUT, and STDERR, and of any subsequent file open, is UTF-8.

ロケール環境変数(LC_ALL, LC_CTYPE, LANG)が、'UTF-8' か 'UTF8'(大文字小文字問わず) を 含むなら、STDIN、STDOUT、STDERR、どんなファイルのopenのデフォルトのエンコーディングは、 UTF-8になります。

Directory handles may also support PerlIO layers in the future.

将来は、ディレクトリハンドルも、PerlIOレイヤがサポートされるかもしれません。

非NPERLIO 機能

If Perl is not built to use PerlIO as its IO system then only the two pseudo-layers :bytes and :crlf are available.

PerlがIOシステムとしてPerlIOを使うようにビルドされていなくても、 二つの仮のレイヤ:bytes:crlfが利用可能です。

The :bytes layer corresponds to "binary mode" and the :crlf layer corresponds to "text mode" on platforms that distinguish between the two modes when opening files (which is many DOS-like platforms, including Windows). These two layers are no-ops on platforms where binmode() is a no-op, but perform their functions everywhere if PerlIO is enabled.

"バイナリモード"と"テキストモード"の二つのモードを区別するプラットフォーム(多くのDOSのような(Windowsを含む)プラットフォーム)上で、 ファイルを開くときに、:bytes レイヤは、"バイナリモード"に対応し、:crlfレイヤは、"テキストモード"に対応します。 binmode()が操作不能なプラットフォームでは、これらの二つのレイヤは操作不能です。 ですが、PerlIOが有効ならば、この機能はどこでも動きます。

実装の詳細

There is a class method in PerlIO::Layer find which is implemented as XS code. It is called by import to validate the layers:

PerlIO::Layer に クラスメソッドがあります。 find は、XS のコードで実装されています。 findは、importで呼ばれて、レイヤが有効か確認します。

   PerlIO::Layer::->find("perlio")

The return value (if defined) is a Perl object, of class PerlIO::Layer which is created by the C code in perlio.c. As yet there is nothing useful you can do with the object at the perl level.

返値は(定義されていれば)Perl オブジェクトです。 perlio.cのCのコードで作られたクラス PerlIO::Layerのものです。 今のところ、perl レベルのオブジェクトでできる有益なことは何もありません。

SEE ALSO

"binmode" in perlfunc, "open" in perlfunc, perlunicode, PerlIO, encoding

翻訳について

翻訳者:加藤敦 (ktat.is@gmail.com)

Perlドキュメント日本語訳 Project にて、 Perlモジュール、ドキュメントの翻訳を行っております。

 http://perldocjp.sourceforge.jp
 http://sourceforge.jp/projects/perldocjp/
 http://www.freeml.com/ctrl/html/MLInfoForm/perldocjp@freeml.com
 http://www.perldoc.jp