简体   繁体   English

处理Moose中的多继承构造函数

[英]Dealing with multiple-inherited constructors in Moose

Greetings, 问候,

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. 我正在学习Moose,而我正在尝试用Moose编写一个CGI :: Application子类,由于CGI-App不是基于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. 在我的其他CGI-App子类中,我喜欢使用一个setup方法的父类,该方法查看子类的符号表并自动设置runmodes。 I figure I can use Moose's metaclass facilities to achieve the same thing in a cleaner way. 我想我可以使用Moose的元类设施以更清洁的方式实现同​​样的目的。 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";
    }
}

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. 我意识到我的runmodes没有正确设置的原因是因为CGI-App构造函数调用了setup ,而Moose::Object在我的类中坚持自己的构造函数。 I tried to solve this with a method modifier: 我尝试用方法修饰符解决这个问题:

around new { 
    $self = $orig->( @_ );
    $self->CGI::Application::new( @_ );
}

This gives me 这给了我

Can't call method "BUILDARGS" on unblessed reference at ...Moose/Object.pm line 21.

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. 然而,我有一种感觉,我是以完全错误的方式解决这个问题,而且Moose有更好的设施来实现我想要的,我还没有发现。

Have you already looked at Moose::Cookbook::Basics::DateTime_ExtendingNonMooseParent and MooseX::NonMoose ? 你已经看过Moose :: Cookbook :: Basics :: DateTime_ExtendingNonMooseParentMooseX :: NonMoose

Update: I am not very familiar with Moose and assorted techniques. 更新:我对Moose和各种技术不太熟悉。 I was not able to get the modules to compile using MooseX::Declare and MooseX::NonMoose together. 我无法使用MooseX::DeclareMooseX::NonMoose一起编译模块。 However, here is something that seems to work: 但是,这里似乎有用:

Application Base Class 应用程序基类

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"

Derived Class 派生类

package My::Login;
use Moose;
extends 'My::App';

sub rm_main { 'it works!' }

"My::Login"

Script 脚本

#!/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;

Output 产量

C:\Temp\f> t
Content-Type: text/html; charset=ISO-8859-1

it works!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM