【发布时间】:2014-08-05 22:10:38
【问题描述】:
我有以下包和文件:
孩子.pm
package Child;
use Father; # this should automatically extends Father also
has 'name' => (is => 'rw', default => "Harry");
1;
父亲.pm
package Father;
use Moose;
sub import {
my ($class, @args) = @_;
my ($caller, $script) = caller;
my $package = __PACKAGE__;
{
no strict 'refs';
@{"${caller}::ISA"} = ($package, @{"${caller}::ISA"});
# tried this also
#eval {"package $caller; use Moose; extends qw($package);1;"}
}
}
1;
test.cgi
#!/usr/bin/perl
use Child;
my $child = Child->new;
print "child name: " . $child->name;
我想要包 Child 自动扩展包 Father。
我在Father的import函数中放了一段代码来推送到子模块ISA但是没有用。
如何做到这一点,让父模块在导入过程中扩展子模块。
【问题讨论】:
-
也许我没有按照您的想法进行操作,但是简单地使用
extends有什么问题吗? -
使用扩展“父亲”;我必须在使用 Moose 之前使用它;所以我试图保存这一行输入
-
我强烈反对你这样做。你省下来的打字线,在以后别人被你过分的聪明弄糊涂的时候,会得到几十倍的回报。