=head1 NAME attributes - get/set subroutine or variable attributes J<< ja; attributes - 変数若しくは関数の属性を取得/設定 >> =head1 SYNOPSIS sub foo : method ; my ($x,@y,%z) : Bent = 1; my $s = sub : method { ... }; use attributes (); # optional, to get subroutine declarations my @attrlist = attributes::get(\&foo); use attributes 'get'; # import the attributes::get subroutine my @attrlist = get \&foo; =head1 DESCRIPTION Subroutine declarations and definitions may optionally have attribute lists associated with them. (Variable C declarations also may, but see the warning below.) Perl handles these declarations by passing some information about the call site and the thing being declared along with the attribute list to this module. In particular, the first example above is equivalent to the following: J<< ja; 関数の宣言と定義ではそれに関連した属性のリストを任意で持つことが できます. (変数の C 宣言もできますが, 後述の警告も参照して ください.) Perl はこれらの宣言を, 呼び出す場所と属性リストと ともに宣言されようとしているものの情報をこのモジュールに渡すことで これらの宣言を処理します. 特に, 上の最初の例は次のものと等価です: >> use attributes __PACKAGE__, \&foo, 'method'; The second example in the synopsis does something equivalent to this: J<< ja; 2番目の例は次と等価です: >> use attributes (); my ($x,@y,%z); attributes::->import(__PACKAGE__, \$x, 'Bent'); attributes::->import(__PACKAGE__, \@y, 'Bent'); attributes::->import(__PACKAGE__, \%z, 'Bent'); ($x,@y,%z) = 1; Yes, that's a lot of expansion. J<< ja; ここでは多くの展開がなされます. >> B: attribute declarations for variables are still evolving. The semantics and interfaces of such declarations could change in future versions. They are present for purposes of experimentation with what the semantics ought to be. Do not rely on the current implementation of this feature. J<< ja; B<警告>: 変数に対する属性の宣言はまだ進歩の途上にあります. これらの宣言の意味とインターフェースは今後のバージョンで 変更される可能性があります. これらは今後くる意味を体験する のを目的として提供されています. この機能の現在の実装には 頼らないでください. >> There are only a few attributes currently handled by Perl itself (or directly by this module, depending on how you look at it.) However, package-specific attributes are allowed by an extension mechanism. (See L<"Package-specific Attribute Handling"> below.) J<< ja; 現在 Perl 自身(若しくは見方によってはこのモジュールが直接)で 処理される属性はわずかです. しかし, あるパッケージ用の属性という ものが拡張メカニズムとして使うことができます. (後述の L<"Package-specific Attribute Handling">を参照してください.) >> The setting of subroutine attributes happens at compile time. Variable attributes in C declarations are also applied at compile time. However, C variables get their attributes applied at run-time. This means that you have to I the run-time component of the C before those attributes will get applied. For example: J<< ja; 関数の属性の設定はコンパイル時に起きます. C 宣言での変数属性も コンパイル時に適用されます. しかし, C 変数は実行時に属性の 適用が行われます. これは属性が適用される前に C の実行時構成に I<到達>しなければならないことを意味します. 例えば: >> my $x : Bent = 42 if 0; will neither assign 42 to $x I will it apply the C attribute to the variable. J<< ja; は $x に 42 を代入することI<も>変数に C 属性が適用される こともありません. >> An attempt to set an unrecognized attribute is a fatal error. (The error is trappable, but it still stops the compilation within that C.) Setting an attribute with a name that's all lowercase letters that's not a built-in attribute (such as "foo") will result in a warning with B<-w> or C. J<< ja; 知らない属性の設定は致命的なエラーとなります. (このエラーは トラップできますが, その C 野中でコンパイルは停止します.) 全てが小文字からなるけれど組み込みの属性ではない名前(例えば”foo") を設定すると B<-w> 若しくは C では 警告を発生します. >> =head2 Built-in Attributes J<< ja; 組み込みの属性 >> The following are the built-in attributes for subroutines: J<< ja; 以下のものは関数用の組み込み属性です >> =over 4 =item locked B<5.005 threads only! The use of the "locked" attribute currently only makes sense if you are using the deprecated "Perl 5.005 threads" implementation of threads.> J<< ja; B<5.005 スレッドのみです! "locked" 属性の使用は今だと 廃止された "Perl 5.005 スレッド" によるスレッド実装を 使っているときにだけ意味を持ちます.> >> Setting this attribute is only meaningful when the subroutine or method is to be called by multiple threads. When set on a method subroutine (i.e., one marked with the B attribute below), Perl ensures that any invocation of it implicitly locks its first argument before execution. When set on a non-method subroutine, Perl ensures that a lock is taken on the subroutine itself before execution. The semantics of the lock are exactly those of one explicitly taken with the C operator immediately after the subroutine is entered. J<< ja; この属性を設定するのはその関数若しくはメソッドが複数のスレッドから 呼び出されるときにだけ意味があります. メソッド関数(つまり後述の B 属性を持っているもの)に対して設定したときには Perl はその呼び出し時には実行の前に最初に引数を暗黙にロック することを保証します. ロックの意味は関数に入った直後に C 演算子で明示的に行うのとちょうど同じです. >> =item method J<< ja; method >> Indicates that the referenced subroutine is a method. This has a meaning when taken together with the B attribute, as described there. It also means that a subroutine so marked will not trigger the "Ambiguous call resolved as CORE::%s" warning. J<< ja; 参照された関数はメソッドであると提示します. これは先に説明した B 属性と一緒に使うときに意味があります. これでマーク された関数は "Ambiguous call resolved as CORE::%s" (CORE::%s として処理される曖昧な呼び出し)警告は発生させません. >> =item lvalue Indicates that the referenced subroutine is a valid lvalue and can be assigned to. The subroutine must return a modifiable value such as a scalar variable, as described in L. J<< ja; 参照された関数は左辺値(lvalue)として有効であり代入可能である ことを示します. 関数は L で説明されているように 変更することのできる値, 例えばスカラー変数等を返さなければ なりません. >> =back For global variables there is C attribute: see L. J<< ja; グローバル変数のために C 属性があります: L を参照してください. >> =head2 Available Subroutines J<< ja; 提供されている関数 >> The following subroutines are available for general use once this module has been loaded: J<< ja; 以下の関数は一般的な使用のためにこのモジュールをロード したときに提供されます. >> =over 4 =item get This routine expects a single parameter--a reference to a subroutine or variable. It returns a list of attributes, which may be empty. If passed invalid arguments, it uses die() (via L) to raise a fatal exception. If it can find an appropriate package name for a class method lookup, it will include the results from a C_ATTRIBUTES> call in its return list, as described in L<"Package-specific Attribute Handling"> below. Otherwise, only L will be returned. =item reftype This routine expects a single parameter--a reference to a subroutine or variable. It returns the built-in type of the referenced variable, ignoring any package into which it might have been blessed. This can be useful for determining the I value which forms part of the method names described in L<"Package-specific Attribute Handling"> below. =back Note that these routines are I exported by default. =head2 Package-specific Attribute Handling B: the mechanisms described here are still experimental. Do not rely on the current implementation. In particular, there is no provision for applying package attributes to 'cloned' copies of subroutines used as closures. (See L for information on closures.) Package-specific attribute handling may change incompatibly in a future release. When an attribute list is present in a declaration, a check is made to see whether an attribute 'modify' handler is present in the appropriate package (or its @ISA inheritance tree). Similarly, when C is called on a valid reference, a check is made for an appropriate attribute 'fetch' handler. See L<"EXAMPLES"> to see how the "appropriate package" determination works. The handler names are based on the underlying type of the variable being declared or of the reference passed. Because these attributes are associated with subroutine or variable declarations, this deliberately ignores any possibility of being blessed into some package. Thus, a subroutine declaration uses "CODE" as its I, and even a blessed hash reference uses "HASH" as its I. The class methods invoked for modifying and fetching are these: =over 4 =item FETCH_I_ATTRIBUTES This method receives a single argument, which is a reference to the variable or subroutine for which package-defined attributes are desired. The expected return value is a list of associated attributes. This list may be empty. =item MODIFY_I_ATTRIBUTES This method is called with two fixed arguments, followed by the list of attributes from the relevant declaration. The two fixed arguments are the relevant package name and a reference to the declared subroutine or variable. The expected return value is a list of attributes which were not recognized by this handler. Note that this allows for a derived class to delegate a call to its base class, and then only examine the attributes which the base class didn't already handle for it. The call to this method is currently made I the processing of the declaration. In particular, this means that a subroutine reference will probably be for an undefined subroutine, even if this declaration is actually part of the definition. =back Calling C from within the scope of a null package declaration C for an unblessed variable reference will not provide any starting package name for the 'fetch' method lookup. Thus, this circumstance will not result in a method call for package-defined attributes. A named subroutine knows to which symbol table entry it belongs (or originally belonged), and it will use the corresponding package. An anonymous subroutine knows the package name into which it was compiled (unless it was also compiled with a null package declaration), and so it will use that package name. =head2 Syntax of Attribute Lists An attribute list is a sequence of attribute specifications, separated by whitespace or a colon (with optional whitespace). Each attribute specification is a simple name, optionally followed by a parenthesised parameter list. If such a parameter list is present, it is scanned past as for the rules for the C operator. (See L.) The parameter list is passed as it was found, however, and not as per C. Some examples of syntactically valid attribute lists: switch(10,foo(7,3)) : expensive Ugly('\(") :Bad _5x5 locked method Some examples of syntactically invalid attribute lists (with annotation): switch(10,foo() # ()-string not balanced Ugly('(') # ()-string not balanced 5x5 # "5x5" not a valid identifier Y2::north # "Y2::north" not a simple identifier foo + bar # "+" neither a colon nor whitespace =head1 EXPORTS =head2 Default exports None. =head2 Available exports The routines C and C are exportable. =head2 Export tags defined The C<:ALL> tag will get all of the above exports. =head1 EXAMPLES Here are some samples of syntactically valid declarations, with annotation as to how they resolve internally into C invocations by perl. These examples are primarily useful to see how the "appropriate package" is found for the possible method lookups for package-defined attributes. =over 4 =item 1. Code: package Canine; package Dog; my Canine $spot : Watchful ; Effect: use attributes (); attributes::->import(Canine => \$spot, "Watchful"); =item 2. Code: package Felis; my $cat : Nervous; Effect: use attributes (); attributes::->import(Felis => \$cat, "Nervous"); =item 3. Code: package X; sub foo : locked ; Effect: use attributes X => \&foo, "locked"; =item 4. Code: package X; sub Y::x : locked { 1 } Effect: use attributes Y => \&Y::x, "locked"; =item 5. Code: package X; sub foo { 1 } package Y; BEGIN { *bar = \&X::foo; } package Z; sub Y::bar : locked ; Effect: use attributes X => \&X::foo, "locked"; =back This last example is purely for purposes of completeness. You should not be trying to mess with the attributes of something in a package that's not your own. =head1 SEE ALSO L and L for details on the basic declarations; L for the obsolescent form of subroutine attribute specification which this module replaces; L for details on the normal invocation mechanism. =cut