【发布时间】:2011-10-06 02:00:31
【问题描述】:
我有一个角色声明它需要一个方法(使用requires)。
我试图通过直接在类的符号表中定义它来安装该方法。
但是,在某些情况下它有效,而在其他情况下则无效。
在下文中,WORKS 表示没有报告错误,DOESN'T WORK 表示我收到错误:'arole' requires the method 'finddepth' to be implemented by 'aclass'
package arole;
use Moose::Role;
requires 'finddepth';
package anexporter;
sub import {
no strict 'refs';
*{ "aclass::finddepth" } = sub {};
}
package anexporter2;
sub import {
eval "sub aclass::finddepth {}";
}
package aclass;
use Moose;
# WORKS:
# sub finddepth { }
# WORKS:
# BEGIN { *{finddepth} = sub {} };
# DOESN'T WORK:
# use File::Find qw(finddepth);
# DOESN'T WORK:
# BEGIN { anexporter->import };
# WORKS:
# BEGIN { no strict 'refs'; *{ "aclass::finddepth" } = sub { }};
# WORKS:
# BEGIN { anexporter2->import };
with 'arole';
1;
【问题讨论】: