宣言されたものに.WHY
を呼び出すと、その周囲に構築された特別なコメントが返されます。それはいいね。クラスで定義されたサブルーチンをどのように参照できますか?それは常に隠されていますか?私は、クラスの代わりにサブルーチンを提供するモジュールについて興味があります(その答えは "そうしないでください")。私はほとんど.WHY
の限界とそれを取ることができる範囲で遊んでいます。ここでPerl 6のクラスのサブルーチンで.WHYを呼び出すにはどうすればよいですか?
#| This is the outside bit
sub outside { 137 }
put &outside.WHY; # This works
#| Class Foo is an example
class Foo {
#| The bar method returns a number
method bar { 137 }
#| quux is a submethod
submethod quux { 137 }
#| qux is private
submethod !qux { 137 }
#| The baz method also returns a number
sub baz { 37 }
put &baz.WHY; # this works in this scope
}
put "---- With an object";
quietly {
my $object = Foo.new;
put "As object: ", $object.WHY;
# sub is not really a method?
put "As object - bar: ", $object.^find_method('bar').WHY;
# should this work? It *is* private after all
put "As object - qux: ", $object.^find_method('qux').WHY;
}
put "---- With class name";
quietly {
put Foo.WHY;
put "As class lookup: ", ::("Foo").WHY;
put "As class lookup (quux): " ~ Foo.^lookup('quux').WHY;
put "As class lookup (baz): " ~ Foo.^lookup('baz').WHY; # nope!
# this is the part where I need help
put "As :: lookup: ", ::("Foo")::("&baz").WHY; #nope
}
は出力です:
This is the outside bit
The baz method also returns a number
---- With an object
As object: Class Foo is an example
As object - bar: The bar method returns a number
As object - qux:
---- With class name
Class Foo is an example
As class lookup: Class Foo is an example
As class lookup (quux): quux is a submethod
As class lookup (baz):
As :: lookup:
それは私が求めている出力の最後の行です。クラス内で定義されたサブルーチンにはどうすれば到達できますか?