类似于关于迭代子例程引用的问题,并且作为回答关于OO调度表的这个问题的结果,我想知道如何在引用中调用方法引用,而不是先删除它,或者甚至是否可能.
例如:
package Class::Foo;
use 5.012; #Yay autostrict!
use warnings;
# a basic constructor for illustration purposes....
sub new {
my $class = shift;
return bless {@_}, $class;
}
# some subroutines for flavor...
sub sub1 { say 'in sub 1'; return shift->{a} }
sub sub2 { say 'in sub 2'; return shift->{b} }
sub sub3 { say 'in sub 3'; return shift->{c} }
# and a way to dynamically load the tests we're running...
sub sublist {
my $self = shift;
return [
$self->can('sub1'),
$self->can('sub3'),
$self->can('sub2'),
];
}
package main;
sub get_index { ... } # details of how we get the index not important
my $instance = Class::Foo->new(a => 1, b => 2, c => 3);
my $subs = $instance->sublist();
my $index = get_index();
# <-- HERE
Run Code Online (Sandbox Code Playgroud)
那么,在HERE,我们可以做到:
my $ref = $subs->[$index];
$instance->$ref();
Run Code Online (Sandbox Code Playgroud)
但是如果不首先删除引用,我们将如何做到这一点呢?
编辑:
更改了代码示例,以便人们不会对实现细节感到困惑(叹息,尽我所能).这与我给出的第一个链接之间的重要区别是该函数应该作为方法调用,而不是作为直接子例程调用.
编辑2:
正如所写,你可以逃脱
$tests->[$index]();
Run Code Online (Sandbox Code Playgroud)
因为你问题中的方法没有使用$self.
你可以$instance明确地通过,但这很笨拙。更好的方法是用闭包来模拟委托:
sub sublist {
my $self = shift;
my $sublist;
for (qw/ sub1 sub3 sub2 /) {
my $meth = $_;
push @$sublist => sub { $self->$meth() };
}
return $sublist;
}
Run Code Online (Sandbox Code Playgroud)
如果您喜欢简洁,请使用
sub sublist {
my $self = shift;
return [ map { my $meth = $_; sub { $self->$meth() } }
qw/ sub1 sub3 sub2 / ];
}
Run Code Online (Sandbox Code Playgroud)
随意叫一个还是
$tests->[$index]();
Run Code Online (Sandbox Code Playgroud)
但现在这些方法得到了调用者。
通过获取子引用can似乎是不必要的复杂性。如果运行时确定的要调用的方法名称列表就可以了,那么您可以大大简化代码:
sub sublist {
my $self = shift;
return [ qw/ sub1 sub3 sub2 / ];
}
Run Code Online (Sandbox Code Playgroud)
下面,我们将它们全部称为测试目的,但您也可以了解如何仅调用其中一个:
foreach my $method (@$subs) {
my $x = $instance->$method();
say "$method returned $x";
}
Run Code Online (Sandbox Code Playgroud)
输出:
在子 1 中 sub1 返回 1 在子 3 中 sub3 返回 3 在子2中 sub2 返回 2