=for comment made from [Class-Fields-0.16] [lib/fields.pod] =head1 NAME fields - compile-time class fields J<< ja/fields - クラスフィールドのコンパイル時決定 >> =head1 SYNOPSIS { package Foo; use fields qw(foo bar _Foo_private); sub new { my Foo $self = shift; unless (ref $self) { $self = fields::new($self); $self->{_Foo_private} = "this is Foo's secret"; } $self->{foo} = 10; $self->{bar} = 20; return $self; } } my Foo $var = Foo->new; $var->{foo} = 42; # this will generate a compile-time error # これはコンパイルエラーとなります $var->{zap} = 42; # subclassing # サブクラス化 { package Bar; use base 'Foo'; use fields qw(baz _Bar_private); # not shared with Foo sub new { my $class = shift; my $self = fields::new($class); $self->SUPER::new(); # init base fields $self->{baz} = 10; # init own fields $self->{_Bar_private} = "this is Bar's secret"; return $self; } } =head1 DESCRIPTION The C pragma enables compile-time verified class fields. J<< ja/ C プラグマはクラスフィールドのコンパイル時の検証を可能にします. >> NOTE: The current implementation keeps the declared fields in the %FIELDS hash of the calling package, but this may change in future versions. Do B update the %FIELDS hash directly, because it must be created at compile-time for it to be fully useful, as is done by this pragma. J<< ja/ 現在の実装では宣言されたフィールドを呼び出したパッケージの %FIELDS ハッシュに保持していますが, 今後変更される可能性があります. %FIELDS ハッシュを直接操作B<しないでください>. これはコンパイル時に生成され, このプラグマによって管理されます. >> If a typed lexical variable holding a reference is used to access a hash element and a package with the same name as the type has declared class fields using this pragma, then the operation is turned into an array access at compile time. J<< ja; もしリファレンスを保持しているある型のレキシカル変数がハッシュ要素に アクセスし, その型と同じ名前のパッケージがこのプラグマを使って クラスフィールドを宣言しているのなら, その操作はコンパイル時に 配列操作へと変換されます. >> The related C pragma will combine fields from base classes and any fields declared using the C pragma. This enables field inheritance to work properly. J<< ja/ 関連する C プラグマは, 基底クラスのフィールドと C プラグマを使って宣言されているフィールドを結合します. これはフィールドの継承を適切に機能させます. >> Field names that start with an underscore character are made private to the class and are not visible to subclasses. Inherited fields can be overridden but will generate a warning if used together with the C<-w> switch. J<< ja/ アンダースコア文字から始まるフィールド名はプライベートなフィールドとされ, 派生クラスからは見えません. 継承されたフィールドはオーバーライドすることが できますが C<-w> スイッチの環境下では警告を生成します. >> The effect of all this is that you can have objects with named fields which are as compact and as fast arrays to access. This only works as long as the objects are accessed through properly typed variables. If the objects are not typed, access is only checked at run time. J<< ja; このプラグマによって名前付きフィールドを持つにもかかわらず 配列と同等に高速でコンパクトなオブジェクトを作ることができます. これはオブジェクトが適切な型の変数を通してアクセスされる時にのみ 機能します. もしオブジェクトが型を持っていなければアクセスは実行時にのみ 確認されます. >> The following functions are supported: J<< ja/ 以下の関数がサポートされています. >> =over 8 =item new fields::new() creates and blesses a pseudo-hash comprised of the fields declared using the C pragma into the specified class. J<< ja/ fields::new() は C プラグマを使って宣言されたフィールドを 指定したクラスに保持させた仮想ハッシュを生成しブレスします. >> This makes it possible to write a constructor like this: J<< ja; これはこのようなコンストラクタの記述を可能にします: >> package Critter::Sounds; use fields qw(cat dog bird); sub new { my Critter::Sounds $self = shift; $self = fields::new($self) unless ref $self; $self->{cat} = 'meow'; # scalar element @$self{'dog','bird'} = ('bark','tweet'); # slice return $self; } =item phash fields::phash() can be used to create and initialize a plain (unblessed) pseudo-hash. This function should always be used instead of creating pseudo-hashes directly. J<< ja; fields::phash() プレーンな(まだブレスされていない)仮想ハッシュを生成して 初期化するために使います. 仮想ハッシュを直接生成するよりは常にこれを 使うべきです. >> If the first argument is a reference to an array, the pseudo-hash will be created with keys from that array. If a second argument is supplied, it must also be a reference to an array whose elements will be used as the values. If the second array contains less elements than the first, the trailing elements of the pseudo-hash will not be initialized. This makes it particularly useful for creating a pseudo-hash from subroutine arguments: J<< ja; 1つ目の引数が配列へのリファレンスだったのならその配列に含まれるキーから 仮想ハッシュが生成されます. もし2つ目の引数が置かれていたらそれもまた 配列へのリファレンスでなければなりません. 今度の要素は値として使われます. もし2つ目の配列が1つ目の配列より少ない要素しかもっていなければ 残りに相当する仮想ハッシュの要素は初期化されません. これは特に関数の引数から仮想ハッシュを生成する時に役に立ちます. >> sub dogtag { my $tag = fields::phash([qw(name rank ser_num)], [@_]); } fields::phash() also accepts a list of key-value pairs that will be used to construct the pseudo hash. Examples: J<< ja; fields::phash() は仮想ハッシュを構築するためのキー値ペアの リストも受け付けます. 例: >> my $tag = fields::phash(name => "Joe", rank => "captain", ser_num => 42); my $pseudohash = fields::phash(%args); =back =head1 SEE ALSO L, L =head1 TRANSALTE TO JAPANESE J<< ja;和訳 >> 山科 氷魚 (YAMASHINA Hio) Original distribution is perl VERSION 5.8.0. Translated on 2003-08-29. (Updated on 2007-08-03.) J<< ja; 原典: perl VERSION 5.8.0. 翻訳日: 2003-08-29. (修正: 2007-08-03.) >>