=encoding euc-jp =head1 NAME =begin original perlpragma - how to write a user pragma =end original perlpragma - ユーザープラグマの書き方 =head1 DESCRIPTION =begin original A pragma is a module which influences some aspect of the compile time or run time behaviour of Perl, such as C or C. With Perl 5.10 you are no longer limited to the built in pragmata; you can now create user pragmata that modify the behaviour of user functions within a lexical scope. =end original プラグマは、C や C のように、コンパイル時や実行時の Perl の 振る舞いにある種の影響を与えるモジュールです。 Perl 5.10 から、プラグマは組み込みのものに制限されません; レキシカル スコープ内のユーザー関数の振る舞いを変えるユーザープラグマを 作れるようになりました。 =head1 A basic example (基本的な例) =begin original For example, say you need to create a class implementing overloaded mathematical operators, and would like to provide your own pragma that functions much like C You'd like this code =end original 例えば、オーバーロードされた算術演算子を実装するクラスを作る必要が あるとして、C のように働く独自のプラグマを提供したいと します。 以下のようなコードで use MyMaths; my $l = MyMaths->new(1.2); my $r = MyMaths->new(3.4); print "A: ", $l + $r, "\n"; use myint; print "B: ", $l + $r, "\n"; { no myint; print "C: ", $l + $r, "\n"; } print "D: ", $l + $r, "\n"; no myint; print "E: ", $l + $r, "\n"; =begin original to give the output =end original 以下のように出力されます A: 4.6 B: 4 C: 4.6 D: 4 E: 4.6 =begin original I, where C is in effect, addition operations are forced to integer, whereas by default they are not, with the default behaviour being restored via C =end original I<つまり>、C が有効のときには加法演算子は整数に強制され、 一方デフォルトではそうではありませんし、デフォルトの振る舞いは C で復元されています。 =begin original The minimal implementation of the package C would be something like this: =end original C パッケージの最低限の実装は以下のようなものです: package MyMaths; use warnings; use strict; use myint(); use overload '+' => sub { my ($l, $r) = @_; # Pass 1 to check up one call level from here if (myint::in_effect(1)) { int($$l) + int($$r); } else { $$l + $$r; } }; sub new { my ($class, $value) = @_; bless \$value, $class; } 1; =begin original Note how we load the user pragma C with an empty list C<()> to prevent its C being called. =end original C が呼び出されないようにユーザープラグマ C を空リスト C<()> 付きで呼び出す方法に注意してください。 =begin original The interaction with the Perl compilation happens inside package C: =end original Perl コンパイラとの相互作用はパッケージ C の内側で起こります: package myint; use strict; use warnings; sub import { $^H{myint} = 1; } sub unimport { $^H{myint} = 0; } sub in_effect { my $level = shift // 0; my $hinthash = (caller($level))[10]; return $hinthash->{myint}; } 1; =begin original As pragmata are implemented as modules, like any other module, C becomes =end original 他のモジュールと同様、プラグマもモジュールとして実装され、C は 以下のようになり: BEGIN { require myint; myint->import(); } =begin original and C is =end original C は以下のようになります: BEGIN { require myint; myint->unimport(); } =begin original Hence the C and C routines are called at B for the user's code. =end original 従って C と C のルーチンはユーザーコードの B<コンパイル時> に呼び出されます。 =begin original User pragmata store their state by writing to the magical hash C<%^H>, hence these two routines manipulate it. The state information in C<%^H> is stored in the optree, and can be retrieved at runtime with C, at index 10 of the list of returned results. In the example pragma, retrieval is encapsulated into the routine C, which takes as parameter the number of call frames to go up to find the value of the pragma in the user's script. This uses C to determine the value of C<$^H{myint}> when each line of the user's script was called, and therefore provide the correct semantics in the subroutine implementing the overloaded addition. =end original ユーザープラグマは、マジカルなハッシュ C<%^H> に書き込むことで状態を 保持するので、これらの二つのサブルーチンはこれを操作します。 C<%^H> の状態情報は構文木に保管され、C から返されたリストの インデックス 10 の要素としてして実行中に取得できます。 例のプラグマでは、返されたものはユーザースクリプトのプラグマの値を 見つけるために上がっていく呼び出しフレームの数をパラメータとして受け取る C ルーチンでカプセル化されます。 これはユーザーのスクリプトの各行が呼び出されるときに C<$^H{myint}> の 値を決定するために C を使うので、オーバーロードされた加算を 実装しているサブルーチン内で正しい意味論を提供します。 =head1 Implementation details (実装の詳細) =begin original The optree is shared between threads. This means there is a possibility that the optree will outlive the particular thread (and therefore the interpreter instance) that created it, so true Perl scalars cannot be stored in the optree. Instead a compact form is used, which can only store values that are integers (signed and unsigned), strings or C - references and floating point values are stringified. If you need to store multiple values or complex structures, you should serialise them, for example with C. The deletion of a hash key from C<%^H> is recorded, and as ever can be distinguished from the existence of a key with value C with C. =end original 構文木はスレッド間で共有されます。 つまり、構文木はそれを作った特定のスレッド(従ってインタプリタ実体)よりも 長生きする可能性があると言うことです。 従って、真の Perl スカラを構文木に保管することが出来ません。 圧縮形式を使う代わりに、整数(符号付きと符号なし)、文字列、C の いずれかの値のみを保管できます - リファレンスと浮動小数点数は 文字列化されます。 もし複数の値や複雑な構造体を保管する必要があるなら、例えば C などを 使って直列化するべきです。 C<%^H> からのハッシュキーの削除は記録され、いままで通り C を使うことで 値が C でキーが存在することと区別できます。 =begin original B attempt to store references to data structures as integers which are retrieved via C and converted back, as this will not be threadsafe. Accesses would be to the structure without locking (which is not safe for Perl's scalars), and either the structure has to leak, or it has to be freed when its creating thread terminates, which may be before the optree referencing it is deleted, if other threads outlive it. =end original リファレンスをC 経由で取得して変換し直した整数としてデータ構造に 保管しようとしてはいけません; これはスレッドセーフではないからです。 アクセスはロックなしでの構造体に対してであり(これは Perl のスカラに対しては 安全ではありません)、構造体がメモリリークするか、作成したスレッドが終了時に 解放され、もしもし他のスレッドが長生きしすぎると、構文木が削除されたものを 参照することになります。 =begin meta Translate: SHIRAKATA Kentaro Status; completed =end meta