base-2.14 > fields
base-2.14
Other versions:
base-2.22

名前

fields - compile-time class fields

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 $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;
        }
    }

説明

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.

perl 5.9.0 より前: fields::new() は fields プラグマを使って宣言されたフィールドからなる疑似ハッシュを 指定されたクラスに作成して、bless します。

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.

perl 5.9.0 以降: fields::new() は fields プラグマを使って宣言されたフィールドからなる制限ハッシュを 指定されたクラスに作成して、bless します。

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

この関数は疑似ハッシュありでもなしでも有用です。 これは fields ベースのオブジェクトを作るための推奨する方法です。

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() は通常の(bless されていない) 疑似ハッシュの作成と初期化に使われます。 この関数は疑似ハッシュを直接作る代わりに常に使われるべきです。

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:

最初の引数が配列へのリファレンスの場合、疑似ハッシュはこの配列を キーとして作成されます。 2 番目の引数が指定された場合、これは要素が値として使われる配列への リファレンスでなければなりません。 二つ目の配列の要素数が一つ目より少ない場合、 疑似ハッシュの残りの要素は初期化されません。 これは特にサブルーチンの引数から疑似ハッシュを作成するのに有用です:

    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 で Perl から取り除かれました。 代わりに制限ハッシュや fields::new() を使うことを検討してください。 fields::phash() を使うとエラーになります。

SEE ALSO

base