perl – 使用MooseX :: Declare关闭内联构造函数
发布时间:2020-12-15 21:47:35 所属栏目:大数据 来源:网络整理
导读:问候, 作为我previous question关于Moose的后续内容,我现在遇到了一个新问题.我有一个Moose类,它使用Recipe 12来扩展非Moose父类.这里是: package MyApp::CGI;### TODO: make this work with MooseX::Declare?use Moose;extends 'CGI::Application';sub new
问候,
作为我previous question关于Moose的后续内容,我现在遇到了一个新问题.我有一个Moose类,它使用Recipe 12来扩展非Moose父类.这里是: package MyApp::CGI; ### TODO: make this work with MooseX::Declare? use Moose; extends 'CGI::Application'; sub new { my $class = shift; my $obj = $class->SUPER::new( @_ ); return $class->meta->new_object( __INSTANCE__ => $obj,@_ ); } sub setup { my $self = shift; $self->start_mode( 'main' ); my @methods = map { $_->name } $self->meta->get_all_methods; $self->run_modes( map { /^rm_(.+)$/ => $_ } grep { /^rm_/ } @methods ); } 这非常有效.我还有一个使用MooseX :: Declare的类的子类.但是,因为我现在覆盖了默认的Moose构造函数,所以我的子类会发出以下警告: Not inlining 'new' for MyApp::CGI::Login since it is not inheriting the default Moose::Object::new If you are certain you don't need to inline your constructor,specify inline_constructor => 0 in your call to MyApp::CGI::Login->meta->make_immutable 由于MooseX :: Declare在幕后自动调用make_immutable,我无法弄清楚如何让它打开inline_constructor => 0参数. 解决方法
感谢IRC的一些人,我能够解决这个问题.声明类可变是足以关闭MooseX :: Declare中的auto_make_immutable标志,所以我可以手动完成. (当然这也适用于非MX :: Declare类.)
经过修改的版本: use MooseX::Declare; class MyApp::CGI extends CGI::Application is mutable { around new { my $obj = $self->SUPER::new( @_ ); return $self->meta->new_object( __INSTANCE__ => $obj,@_ ); } method setup { $self->start_mode( 'main' ); my @methods = map { $_->name } $self->meta->get_all_methods; $self->run_modes( map { /^rm_(.+)$/ => $_ } grep { /^rm_/ } @methods ); } __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |