Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning off inline constructors with MooseX::Declare

Tags:

oop

perl

moose

mop

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.

like image 811
friedo Avatar asked Jun 29 '09 20:06

friedo


1 Answers

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 );
 }
like image 105
friedo Avatar answered Sep 29 '22 18:09

friedo