在Moose中处理多重继承的构造函数 [英] Dealing with multiple-inherited constructors in Moose
问题描述
问候,
我正在学习驼鹿,我正在尝试编写 CGI :: Application 子类,由于CGI-App是不是基于Moose.
I'm learning Moose and I'm trying to write a CGI::Application subclass with Moose, which is made difficult by the fact that CGI-App is not based on Moose.
在我的其他CGI-App子类中,我希望有一个带有setup
方法的父类,该方法查看子类的符号表并自动设置运行模式.我认为我可以使用Moose的元类工具以更简洁的方式实现相同的目的.所以这是我父母班里的东西:
In my other CGI-App subclasses, I like to have a parent class with a setup
method that looks at the child class's symbol table and automatically sets up the runmodes. I figure I can use Moose's metaclass facilities to achieve the same thing in a cleaner way. So here is what I have in my parent class:
use MooseX::Declare;
class MyApp::CGI
extends Moose::Object
extends CGI::Application {
method setup {
$self->start_mode( 'main' );
my @methods = map { $_->name } $self->meta->get_all_methods;
$self->run_modes( map { /^rm_(.+)$/ => $_ }
grep { /^rm_/ }
@methods
);
}
}
...以及我的孩子班级:
...and in my child class:
use MooseX::Declare;
class MyApp::CGI::Login
extends MyApp::CGI {
method rm_main {
return "it works";
}
}
我意识到我的运行模式无法正确设置的原因是因为setup
由CGI-App构造函数调用,而Moose::Object
仍将自己的构造函数保留在我的类中.我试图用方法修饰符解决这个问题:
I realized that the reason my runmodes were not getting setup properly is because setup
is called by the CGI-App constructor, and Moose::Object
is sticking its own constructor in my class. I tried to solve this with a method modifier:
around new {
$self = $orig->( @_ );
$self->CGI::Application::new( @_ );
}
这给了我
Can't call method "BUILDARGS" on unblessed reference at ...Moose/Object.pm line 21.
但是,我有一种感觉,那就是我将以完全错误的方式进行操作,而Moose拥有更好的设施来实现我想要的目标,这是我尚未发现的.
I have a feeling, however, that I'm going about this in completely the wrong way, and Moose has a much better facility for achieving what I want, which I have not as yet discovered.
推荐答案
您是否已经查看过驼鹿::菜谱::基本:: DateTime_ExtendingNonMooseParent 和 MooseX :: NonMoose ?
更新:我对Moose和各种技术不太熟悉.我无法同时使用MooseX::Declare
和MooseX::NonMoose
来编译模块.但是,这似乎可行:
Update: I am not very familiar with Moose and assorted techniques. I was not able to get the modules to compile using MooseX::Declare
and MooseX::NonMoose
together. However, here is something that seems to work:
应用程序基础类
package My::App;
use Moose;
use MooseX::NonMoose;
extends 'CGI::Application';
sub setup {
my $self = shift;
$self->start_mode( 'main' );
$self->run_modes(
map { $_ = $_->name;
/^rm_ (?<rm>.+) $/x ? ( $+{rm} => $_ ) : ()
} $self->meta->get_all_methods
);
}
"My::App"
派生类
package My::Login;
use Moose;
extends 'My::App';
sub rm_main { 'it works!' }
"My::Login"
脚本
#!/usr/bin/perl
use strict;
use warnings;
# For testing on the command line
use FindBin qw( $Bin );
use lib $Bin;
use My::Login;
my $app = My::Login->new;
$app->run;
输出
C:\Temp\f> t
Content-Type: text/html; charset=ISO-8859-1
it works!
这篇关于在Moose中处理多重继承的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!