在声明的东西上调用.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:
这是输出的最后一行,我问。我怎样才能到达在一个类中定义的子程序?