使用MooseX :: Declare关闭内联构造函数

时间:2009-06-29 20:50:49

标签: perl oop moose mop

问候,

作为关于Moose的previous question的后续跟进,我现在遇到了一个新问题。我有一个使用Recipe 12的Moose类来扩展非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参数。

1 个答案:

答案 0 :(得分:11)

感谢IRC的一些人,我能够解决这个问题。声明类mutable足以关闭auto_make_immutable中的MooseX::Declare标记,因此我可以手动执行此操作。 (当然这也适用于非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 );
 }