fields - compile-time class fields

目次


NAME 名前

fields - compile-time class fields

fields - クラスフィールドのコンパイル時決定


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 $var = Foo->new;
    $var->{foo} = 42;

    # this will generate an 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;
	}
    }

DESCRIPTION 説明

The fields pragma enables compile-time verified class 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.

現在の実装では宣言されたフィールドを呼び出したパッケージの %FIELDS ハッシュに保持していますが, 今後変更される可能性があります. %FIELDS ハッシュを直接操作しないでください. これはコンパイル時に生成され, このプラグマによって管理されます.

Only valid for perl before 5.9.0:

5.9.0 より前の perl のみでの実装:

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.

もしリファレンスを保持しているある型のレキシカル変数がハッシュ要素に アクセスし, その型と同じ名前のパッケージがこのプラグマを使って クラスフィールドを宣言しているのなら, その操作はコンパイル時に 配列操作へと変換されます.

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.

関連する base プラグマは, 基底クラスのフィールドと fields プラグマを使って宣言されているフィールドを結合します. これはフィールドの継承を適切に機能させます.

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.

アンダースコア文字から始まるフィールド名はプライベートなフィールドとされ, 派生クラスからは見えません. 継承されたフィールドはオーバーライドすることが できますが -w スイッチの環境下では警告を生成します.

Only valid for perls before 5.9.0:

5.9.0 より前の perl のみでの実装:

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:

以下の関数がサポートされています.

new

perl before 5.9.0: fields::new() creates and blesses a pseudo-hash comprised of the fields declared using the fields pragma into the specified class.

5.9.0 より前の perl: fields::new() は fields プラグマを使って宣言されたフィールドを 指定したクラスに保持させた仮想ハッシュを生成しブレスします.

perl 5.9.0 and higher: fields::new() creates and blesses a restricted-hash comprised of the fields declared using the fields pragma into the specified class.

5.9.0 及びそれ以降の perl: fields::new() は fields プラグマを使って宣言されたフィールドを 指定したクラスに保持させた制限されたハッシュを生成しブレスします.

This function is usable with or without pseudo-hashes. It is the recommended way to construct a fields-based object.

この機能は仮想ハッシュであってもそうでなくても便利です. これはフィールドベースのオブジェクトを生成する 推奨された方法です.

This makes it possible to write a constructor like this:

これはこのようなコンストラクタの記述を可能にします:

    package Critter::Sounds;
    use fields qw(cat dog bird);

    sub new {
	my $self = shift;
	$self = fields::new($self) unless ref $self;
	$self->{cat} = 'meow';				# scalar element
	@$self{'dog','bird'} = ('bark','tweet');	# slice
	return $self;
    }
phash

before perl 5.9.0:

perl 5.9.0 より前:

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.

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:

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:

fields::phash() は仮想ハッシュを構築するためのキー値ペアの リストも受け付けます. 例:

    my $tag = fields::phash(name => "Joe",
                            rank => "captain",
                            ser_num => 42);

    my $pseudohash = fields::phash(%args);

perl 5.9.0 and higher:

perl 5.9.0 及びそれ以降:

Pseudo-hashes have been removed from Perl as of 5.10. Consider using restricted hashes or fields::new() instead. Using fields::phash() will cause an error.

仮想ハッシュは 5.10 において 削除されます. 代わりに制限されたハッシュ 若しくは fields::new() を使うように考え直してください. fields::phash() はエラーとなるでしょう.


SEE ALSO 関連項目

base [CPAN]


TRANSALTE TO JAPANESE 和訳

 山科 氷魚 (YAMASHINA Hio) <hio@hio.jp>

Original distribution is perl VERSION 5.8.8. Translated on 2003-08-29 (perl-5.8.0, Class-Fields-0.16). (Updated on 2007-08-03.)

原典: perl VERSION 5.8.8. 翻訳日: 2003-08-29 (perl-5.8.0, Class-Fields-0.16) (修正: 2007-08-03.)

fields - compile-time class fields

索引

fields - compile-time class fields