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

发布于 2024-07-25 22:42:29 字数 1434 浏览 12 评论 0原文

您好,

作为我上一个关于驼鹿的问题的后续,我'现在我们遇到了一个新问题。 我有一个 Moose 类,它使用 方案 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 参数。

Greetings,

As a followup to my previous question about Moose, I've now run into a new problem. I've got a Moose class which uses Recipe 12 in order to extend a non-Moose parent class. Here it is:

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
                    );        
}

This works great. I also have a subclass of this class which uses MooseX::Declare. However, because I am now overriding the default Moose constructor, my subclass emits this warning:

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

Since MooseX::Declare calls make_immutable automatically behind the scenes, I haven't been able to figure out how to get it to turn on the inline_constructor => 0 parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在梵高的星空下 2024-08-01 22:42:29

感谢 IRC 上的一些人,我能够破解这个问题。 声明类 mutable 足以关闭 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 );
 }

Thanks to some folks on IRC I was able to crack this one. Declaring the class mutable was sufficient to turn off the auto_make_immutable flag in MooseX::Declare, so I could then do it manually. (Of course this also works with a non-MX::Declare class.)

Revised version:

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 );
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文