=encoding utf8 =head1 NAME =begin original perlclass - Perl class syntax reference =end original perlclass - Perl クラス構文リファレンス =head1 SYNOPSIS use v5.38; use feature 'class'; class My::Example 1.234 { field $x; ADJUST { $x = "Hello, world"; } method print_message { say $x; } } My::Example->new->print_message; =head1 DESCRIPTION =begin original This document describes the syntax of the Perl's C feature, which provides native keywords supporting object-oriented programming paradigm. =end original この文書では、オブジェクト指向プログラミングパラダイムをサポートする ネイティブなキーワードを提供する Perl の C 機能の構文について 説明します。 =head2 History (歴史) =begin original Since Perl 5, support for objects resolved around the concept of I references with a package name. Such reference could then be used to call subroutines from the package it was blessed with (or any of its parents). This system, while bare-bones, was flexible enough to allow creation of multiple more advanced, community-driven systems for object orientation. =end original Perl 5 以降、オブジェクトのサポートは、パッケージ名によって I リファレンスの概念に基づいて解決されました。 このようなリファレンスは、bless されたパッケージ(またはその親のいずれか)から サブルーチンを呼び出すために使えます。 このシステムは、必要最小限のものではありますが、オブジェクト指向のための 複数のより高度な、コミュニティ駆動システムの作成を可能にするのに 十分な柔軟性を備えていました。 =begin original Class feature is a core implementation of class syntax which is familiar to what one would find in other programming languages. It isn't a C wrapper, but a completely new system built right into the perl interpreter. =end original クラス機能は、他のプログラミング言語で見られるような、クラス構文の コア実装です。 これは C ラッパーではなく、perl インタプリタに直接組み込まれた 全く新しいシステムです。 =head1 KEYWORDS (キーワード) =begin original Enabling the C feature allows the usage of the following new keywords in the scope of current package: =end original C 機能を有効にすると、現在のパッケージのスコープ内で次の新しい キーワードを使えます。 =head2 class class NAME BLOCK class NAME VERSION BLOCK class NAME; class NAME VERSION; =begin original The C keyword declares a new package which is intended to be a class. All other keywords from the C feature should be used in scope of this declaration. =end original C キーワードは、クラスとして意図された新しいパッケージを宣言します。 C機能からの他のすべてのキーワードは、この宣言のスコープ内で 使われる必要があります。 class WithVersion 1.000 { # class definition goes here } =begin original Classes can be declared in either block or statement syntax. If a block is used, the body of the block contains the implementation of the class. If the statement form is used, the remainder of the file is used up until the next C or C statement. =end original クラスは、ブロック構文または文構文のいずれかで宣言できます。 ブロックを使う場合、ブロックの本体にはクラスの実装が含まれます。 文形式を使用する場合、ファイルの残りの部分は、次の C または C ステートメントまで使い切られます。 =begin original C and C declarations are similar, but classes automatically get a constructor named C - You don't have to (and should not) write one. Additionally, in the class BLOCK you are allowed to declare fields and methods. =end original C と C の宣言は似ていますが、クラスは自動的に C という 名前のコンストラクタを取得します - あなたはこれを書く必要はありません(書くべきではありません)。 さらに、class の BLOCK の中では、フィールドとメソッドを宣言できます。 =head2 field field VARIABLE_NAME; field VARIABLE_NAME = EXPR; field VARIABLE_NAME : ATTRIBUTES; field VARIABLE_NAME : ATTRIBUTES = EXPR; =begin original Fields are variables which are visible in the scope of the class - more specifically within L and C blocks. Each class instance get their own storage of fields, independent of each other. =end original フィールドは、クラスのスコープ内(より具体的には L および C ブロック内)で見える変数です。 各クラス実体は、フィールドのための互いに独立した独自の保管場所を 取得します。 =begin original A field behaves like a normal lexically scoped variable. It has a sigil and is private to the class (though creation of an accessor method will make it accessible from the outside). The main difference is that different instances access different values in the same scope. =end original フィールドは、通常のレキシカルスコープの変数のように振る舞います。 それには印(sigil)があり、クラスに対してプライベートです(ただし、 アクセサメソッドを作成すると、外部からアクセスできるようになります)。 主な違いは、異なる実体は同じスコープ内の異なる値にアクセスすることです。 class WithFields { field $scalar = 42; field @array = qw(this is just an array); field %hash = (species => 'Martian', planet => 'Mars'); } =begin original Fields may optionally have initializing expressions. If present, the expression will be evaluated within the constructor of each object instance. During each evaluation, the expression can use the value of any previously-set field, as well as see any other variables in scope. =end original フィールドは、オプションで初期化式を持つことができます。 存在する場合、式は各オブジェクト実体のコンストラクタ内で評価されます。 それぞれの評価中に、式はそれまでに設定されたフィールドの値を使用でき、 スコープ内の他の変数も参照できます。 class WithACounter { my $next_count = 1; field $count = $next_count++; } =begin original When combined with the C<:param> field attribute, the defaulting expression can use any of the C<=>, C or C<||=> operators. Expressions using C<=> will apply whenever the caller did not pass the corresponding parameter to the constructor at all. Expressions using C will also apply if the caller did pass the parameter but the value was undefined, and expressions using C<||=> will apply if the value was false. =end original C<:param> フィールド属性と組み合わせた場合、デフォルト式では、 C<=>, C, C<||$=> 演算子のいずれかを使えます。 C<=> を使った式は、呼び出し側がコンストラクタに対応する引数を まったく渡さなかった場合に適用されます。 C を使った式は、呼び出し側が引数を渡したが値が 未定義であった場合にも適用され、 C<||=> を使った式は、値が偽であった場合に適用されます。 =head2 method method METHOD_NAME SIGNATURE BLOCK method METHOD_NAME BLOCK method SIGNATURE BLOCK method BLOCK =begin original Methods are subroutines intended to be called in the context of class objects. =end original メソッドは、クラスオブジェクトのコンテキスト内で呼び出されることを目的とした サブルーチンです。 =begin original A variable named C<$self> populated with the current object instance will automatically be created in the lexical scope of C. =end original 現在のオブジェクト実体が設定された C<$self> という名前の変数が、 C のレキシカルスコープに自動的に作成されます。 =begin original Methods always act as if C is in effect, but C<$self> will not appear in the arguments list as far as the signature is concerned. =end original メソッドは常に C が有効であるかのように 動作しますが、C<$self> はシグニチャに関する限り引数リストには表示されません。 class WithMethods { field $greetings; ADJUST { $greetings = "Hello"; } method greet($name = "someone") { say "$greetings, $name"; } } =begin original Just like regular subroutines, methods I be anonymous: =end original 通常のサブルーチンと同じように、メソッドは匿名にすることが I<できます>。 class AnonMethodFactory { method get_anon_method { return method { return 'this is an anonymous method'; }; } } =head1 ATTRIBUTES (属性) =begin original Specific aspects of the keywords mentioned above are managed using I. Attributes all start with a colon, and one or more of them can be appended after the item's name, separated by a space. =end original 上述のキーワードの特定の様相は、I<属性> を使って管理されます。 属性はすべてコロンで始まり、一つまたは複数の属性をアイテム名の後に スペースで区切って追加できます。 =head2 Class attributes (クラス属性) =head3 :isa =begin original Classes may inherit from B superclass, by using the C<:isa> class attribute. =end original クラスは、C<:isa> クラス属性を使用して、B<一つの> スーパークラスから 継承できます。 class Example::Base { ... } class Example::Subclass :isa(Example::Base) { ... } =begin original Inherited methods are visible and may be invoked. Fields are always lexical and therefore not visible by inheritance. =end original 継承されたメソッドはアクセス可能で、呼び出すことができます。 フィールドは常にレキシカルであるため、継承によってアクセス可能にはなりません。 =begin original The C<:isa> attribute may request a minimum version of the base class; it is applied similar to C - if the provided version is too low it will fail at compile time. =end original C<:isa> 属性は、基本クラスの最小バージョンを要求する場合があります; これは、C と同様に適用されます - 提供バージョンが低すぎる場合、コンパイル時に失敗します。 class Example::Subclass :isa(Example::Base 2.345) { ... } =begin original The C<:isa> attribute will attempt to C the named module if it is not already loaded. =end original C<:isa> 属性は、指定されたモジュールがまだロードされていない場合、 そのモジュールを C しようとします。 =head2 Field attributes (フィールド属性) =head3 :param =begin original A scalar field with a C<:param> attribute will take its value from a named parameter passed to the constructor. By default the parameter will have the same name as the field (minus its leading C<$> sigil), but a different name can be specified in the attribute. =end original C<:param> 属性を持つスカラーフィールドは、コンストラクタに渡された 名前付き引数から値を取得します。 デフォルトでは、引数はフィールドと同じ名前 (先頭の C<$> 印を除く) を 持ちますが、属性で別の名前を指定できます。 field $x :param; field $y :param(the_y_value); =begin original If there is no defaulting expression then the parameter is required by the constructor; the caller must pass it or an exception is thrown. With a defaulting expression this becomes optional. =end original デフォルトの式がない場合、コンストラクタには引数が必要です; 呼び出し元はパラメータを渡す必要があり、渡さない場合は例外が投げられます。 デフォルトの式を使用すると、これはオプションになります。 =head2 Method attributes (メソッド属性) =begin original None yet. =end original まだありません。 =head1 OBJECT LIFECYCLE (オブジェクトのライフサイクル) =head2 Construction (構築) =begin original Each object begins its life with a constructor call. The constructor is always named C and is invoked like a method call on the class name: =end original 各オブジェクトは、コンストラクタ呼び出しから始まります。 コンストラクタは常に C という名前で、クラス名に対する メソッド呼び出しのように呼び出されます。 my $object = My::Class->new(%arguments); =begin original During the construction, class fields are compared to C<%arguments> hash and populated where possible. =end original 構築中に、クラスフィールドは C<%arguments> ハッシュと比較され、 可能な場所では値が設定されます。 =head2 Adjustment (調整) =begin original Object adjustment can be performed during the construction to run user-defined code. It is done with the help of C blocks, which are called in order of declaration. =end original オブジェクトの調整は、ユーザ定義コードを実行するための構築中に実行できます。 これは、宣言の順序で呼び出される C ブロックの助けを借りて行われます。 =begin original They are similar to C blocks, which run during the compilation of a package. However, they also have access to C<$self> lexical (object instance) and all object fields created up to that point. =end original これらは、パッケージのコンパイル中に実行される C ブロックに 似ていますが、C<$self> レキシカル変数(オブジェクト実体)および その時点までに作成されたすべてのオブジェクトフィールドにもアクセスできます。 =head2 Lifetime (生存期間) =begin original After the construction phase, object is ready to be used. =end original 構築フェーズの後、オブジェクトは使用可能な状態になります。 =begin original Using C (C or C) on the object will return the name of the class, while C (C or C) will return the string C<'OBJECT'>. =end original オブジェクトに対して C (C または C) を使うと、クラスの名前が返されます; 一方、C(C または C) を 使うと、文字列 C<'OBJECT'> が返されます。 =head2 Destruction (破棄) =begin original Just like with other references, when object reference count reaches zero it will automatically be destroyed. =end original 他のリファレンスと同様に、オブジェクト参照カウントがゼロになると、 自動的に破棄されます。 =head1 TODO =begin original This feature is still experimental and very incomplete. The following list gives some overview of the kinds of work still to be added or changed: =end original この機能はまだ実験的であり、非常に不完全です。 次のリストは、追加または変更される作業の種類の概要を示しています。 =over 4 =item * Roles (ロール) =begin original Some syntax for declaring a role (likely a C keyword), and for consuming a role into a class (likely a C<:does()> attribute). =end original (C キーワードのような) ロールを宣言するための構文と、 (C<:does()> 属性のような) ロールをクラスに取り込むための構文。 =item * Parameters to ADJUST blocks (ADJUST ブロックへの引数) =begin original Some syntax for declaring that an C block can consume named parameters, which become part of the class constructor's API. This might be inspired by a similar plan to add named arguments to subroutine signatures. =end original C ブロックがクラスコンストラクタの API の一部となる 名前付き引数を消費できることを宣言するためのいくつかの構文。 これは、名前付き引数をサブルーチンシグネチャに追加する 同様の計画に触発された可能性があります。 class X { ADJUST (:$alpha, :$beta = 123) { ... } } my $obj = X->new(alpha => 456); =item * ADJUST blocks as true blocks (真のブロックとしての ADJUST ブロック) =begin original Currently, every ADJUST block is wrapped in its own CV that gets invoked with the full ENTERSUB overhead. It should be possible to use the same mechanism that makes all field initializer expressions appear within the same CV on ADJUST blocks as well, merging them all into a single CV per class. This will make it faster to invoke if a class has more than one of them. =end original 現在、すべての ADJUST ブロックは、完全な ENTERSUB オーバーヘッドで 呼び出される独自の CV にラップされています。 すべてのフィールド初期化式を ADJUST ブロックの同じ CV 内に表示する 同じ機構を使って、クラスごとにすべてを 単一の CV にマージすることも可能です。 これにより、クラスに複数の CV がある場合の呼び出しが高速になります。 =item * Accessor generator attributes (アクセサ生成属性) =begin original Attributes to request that accessor methods be generated for fields. Likely C<:reader> and C<:writer>. =end original フィールドに対してアクセサメソッドの生成を要求する属性。 C<:reader> および C<:writer> が考えられます。 class X { field $name :reader; } =begin original Equivalent to =end original 次と同等です: class X { field $name; method name { return $name; } } =item * Metaprogramming (メタプログラミング) =begin original An extension of the metaprogramming API (currently proposed by L) which adds knowledge of classes, methods, fields, ADJUST blocks, and other such class-related details. =end original (現在 L によって 提案されている) メタプログラミング API の拡張で、クラス、メソッド、 フィールド、ADJUST ブロック、およびその他のクラス関連の 詳細に関する知識を追加します。 =item * Extension Customisation (拡張のカスタマイズ) =begin original Ways in which out-of-core modules can interact with the class system, including an ability for them to provide new class or field attributes. =end original コア外モジュールがクラスシステムと相互作用する方法; これには、新しいクラスまたはフィールド属性を提供する機能が含まれます。 =back =head1 AUTHORS Paul Evans Bartosz Jarzyna =begin meta Translate: SHIRAKATA Kentaro Status: completed =end meta =cut