目次 TABLE OF CONTENTS
fields - クラスフィールドのコンパイル時決定 fields - compile-time class fields
{
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;
}
}
fields プラグマはクラスフィールドのコンパイル時の検証を可能にします.
The fields pragma enables compile-time verified class fields.
現在の実装では宣言されたフィールドを呼び出したパッケージの %FIELDS ハッシュに保持していますが, 今後変更される可能性があります. %FIELDS ハッシュを直接操作しないでください. これはコンパイル時に生成され, このプラグマによって管理されます. NOTE: The current implementation keeps the declared fields in the %FIELDS hash of the calling package, but this may change in future versions. Do not 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.
もしリファレンスを保持しているある型のレキシカル変数がハッシュ要素に アクセスし, その型と同じ名前のパッケージがこのプラグマを使って クラスフィールドを宣言しているのなら, その操作はコンパイル時に 配列操作へと変換されます. 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.
関連する base プラグマは, 基底クラスのフィールドと
fields プラグマを使って宣言されているフィールドを結合します.
これはフィールドの継承を適切に機能させます.
The related base pragma will combine fields from base classes and any
fields declared using the fields pragma. This enables field
inheritance to work properly.
アンダースコア文字から始まるフィールド名はプライベートなフィールドとされ,
派生クラスからは見えません. 継承されたフィールドはオーバーライドすることが
できますが -w スイッチの環境下では警告を生成します.
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 -w
switch.
このプラグマによって名前付きフィールドを持つにもかかわらず 配列と同等に高速でコンパクトなオブジェクトを作ることができます. これはオブジェクトが適切な型の変数を通してアクセスされる時にのみ 機能します. もしオブジェクトが型を持っていなければアクセスは実行時にのみ 確認されます. 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.
以下の関数がサポートされています. The following functions are supported:
fields::new() は fields プラグマを使って宣言されたフィールドを
指定したクラスに保持させた仮想ハッシュを生成しブレスします.
fields::new() creates and blesses a pseudo-hash comprised of the fields
declared using the fields pragma into the specified class.
これはこのようなコンストラクタの記述を可能にします: This makes it possible to write a constructor like this:
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;
}
fields::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.
1つ目の引数が配列へのリファレンスだったのならその配列に含まれるキーから 仮想ハッシュが生成されます. もし2つ目の引数が置かれていたらそれもまた 配列へのリファレンスでなければなりません. 今度の要素は値として使われます. もし2つ目の配列が1つ目の配列より少ない要素しかもっていなければ 残りに相当する仮想ハッシュの要素は初期化されません. これは特に関数の引数から仮想ハッシュを生成する時に役に立ちます. 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:
sub dogtag {
my $tag = fields::phash([qw(name rank ser_num)], [@_]);
}
fields::phash() は仮想ハッシュを構築するためのキー値ペアの リストも受け付けます. 例: fields::phash() also accepts a list of key-value pairs that will be used to construct the pseudo hash. Examples:
my $tag = fields::phash(name => "Joe",
rank => "captain",
ser_num => 42);
my $pseudohash = fields::phash(%args);
base [CPAN], "Pseudo-hashes: Using an array as a hash" in perlref [CPAN]
山科 氷魚 (YAMASHINA Hio) <hio@hio.jp>
原典: perl VERSION 5.8.0. 翻訳日: 2003-08-29. (修正: 2007-08-03.) Original distribution is perl VERSION 5.8.0. Translated on 2003-08-29. (Updated on 2007-08-03.)