Working with multi-level designers in Moose

Hi,

I am learning Moose , and I am trying to write a CGI :: Application with Moose, which is complicated by the fact that the CGI application is not based on Moose.

In my other subclasses of the CGI-App, I like to have a parent class with the setup method that looks at the character table of the child class and sets runmodes automatically. I believe I can use Moose metaclasses to achieve the same element in a cleaner way. 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 weren’t configured correctly is because the CGI-App constructor is called setup , and Moose::Object sticks to its own constructor in my class. I tried to solve this problem using the method modifier:

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

It gives me

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

I have a feeling, however, that I am doing it completely wrong, and Musa has a much better way to achieve what I want, which I have not yet discovered.

+6
oop perl moose mop
source share
1 answer

Have you looked at Moose :: Cookbook :: Basics :: DateTime_ExtendingNonMooseParent and MooseX :: NonMoose ?

Update: I am not very familiar with Moose and various methods. I was unable to assemble the modules using MooseX::Declare and MooseX::NonMoose together. However, here is something that works:

Main application 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; 

Exit

 C:\Temp\f> t Content-Type: text/html; charset=ISO-8859-1 it works! 
+9
source share

All Articles