Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Use Moose in Controller and Model

by sman (Beadle)
on Jan 24, 2010 at 23:27 UTC ( [id://819361]=perlquestion: print w/replies, xml ) Need Help??

sman has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

I am experimenting Moose in my catalyst app. I found that in the following three cases, only the first one works ,whereas the last two fail. Do I use it correctly?

Thanks.
#[1] ok package MyApp::Schema::Result::Item; use Moose; extends 'DBIx::Class::Core'; #[2] error package MyApp::Model::MD; use Moose; use base 'Catalyst::Model::DBIC::Schema'; #[3] error package MyApp::Controller::Item; use Moose; use base qw/Catalyst::Controller::FormBuilder/;

Replies are listed 'Best First'.
Re: Use Moose in Controller and Model
by thunders (Priest) on Jan 25, 2010 at 04:59 UTC

    I think the problems you are experiencing stem from not inheriting from Moose::Object. You can warn out the values in @ISA for the three different samples to verify.

    From the Moose documentation:

    extends (@superclasses)

    This function will set the superclass(es) for the current class.

    This approach is recommended instead of use base, because use base actually pushes onto the class's @ISA, whereas extends will replace it. This is important to ensure that classes which do not have superclasses still properly inherit from Moose::Object.

      Thanks. This is a test I did. I am not sure if I am right or wrong but I guess in the second case the superclass of Item becomes Moose::Object, not Catalyst::Controller::FormBuilder, and that's why Catalyst is complaining about. Am I right?
      [1] use Moose; BEGIN { extends 'Catalyst::Model::DBIC::Schema' }; print "$_\n", foreach @MyApp::Controller::Item::ISA; <result> Catalyst::Controller::FormBuilder [2] use Moose; use base qw/Catalyst::Controller::FormBuilder/; print "$_\n", foreach @MyApp::Controller::Item::ISA; <result> Moose::Object Catalyst::Controller::FormBuilder
Re: Use Moose in Controller and Model
by chromatic (Archbishop) on Jan 25, 2010 at 02:57 UTC
    Do I use it correctly?

    What does the Moose documentation say? (The first approach.)

    Why would you expect the other versions to work?

      Thanks for your reply. I guess this might be a dumb question to you, but I really want to figure it out. Would you mind telling me which document you are referring to?
        Would you mind telling me which document you are referring to?

        Presumably the same documentation in which you found the extends example: Moose, Moose::Manual.

Re: Use Moose in Controller and Model
by stvn (Monsignor) on Jan 25, 2010 at 18:08 UTC

    Well, without error messages I have no idea what your real problem is. I do have some suspicions though, which I have detailed below. Next time you post something like this, include the errors you are getting (no matter how large) it will go a long way in helping us help you solve your problem.

    package MyApp::Schema::Result::Item; use Moose; extends 'DBIx::Class::Core';
    You have already been told that this approach is not advised, you should be using DBIx::Class as it was intended.

    package MyApp::Model::MD; use Moose; use base 'Catalyst::Model::DBIC::Schema';
    This doesn't make much sense here because you are using Moose then using base. I think what you really want here is:
    package MyApp::Model::MD; use Moose; BEGIN { extends 'Catalyst::Model::DBIC::Schema' };
    This is exactly the code I am using in my catalyst apps right now. If this still doesn't work, then I suspect it is because of how you are (ab)using DBIx::Class in MyApp::Schema::Result::Item and you should change that.

    package MyApp::Controller::Item; use Moose; use base qw/Catalyst::Controller::FormBuilder/;
    Again you have this mix of Moose and base, what you really want here is:
    package MyApp::Controller::Item; use Moose; BEGIN { extends qw/Catalyst::Controller::FormBuilder/ };

    -stvn
      Hi stvn,

      You are awesome and right about what happening to my code. After replacing this code:
      use Moose; use base 'Catalyst::Model::DBIC::Schema';
      with either:
      use Moose; BEGIN { extends 'Catalyst::Model::DBIC::Schema' };
      or
      use Moose; extends 'Catalyst::Model::DBIC::Schema';
      (same for Controller), the problem solved. Moose can work well with my Catalyst app now.

      Thanks you all.
Re: Use Moose in Controller and Model
by Anonymous Monk on Jan 25, 2010 at 04:28 UTC
    sman, could you provide more information specifically on the error?
      Sorry for not attaching the error message. Here it comes. I got this one when I embed 'use Moose;' in the beginning of MyApp::Controller::User.pm
      DBIx::Class::Core Couldn't load class (MyApp) because: Couldn't instantiate component "M +yApp::Controller::User", "Inconsistent hierarchy during C3 merge of c +lass 'MyApp::Controller::User': merging failed on parent 'Moose::Obje +ct' at /usr/lib/perl5/5.10.0/mro.pm line 23."Compilation failed in re +quire at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-mult +i/Class/MOP.pm line 98. at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Clas +s/MOP.pm line 102 Class::MOP::__ANON__('Couldn\'t instantiate component "MyApp::Cont +roller::Use...') called at /usr/local/lib/perl5/site_perl/5.10.0/Try/ +Tiny.pm line 66 Try::Tiny::try('CODE(0xa1c796c)', 'CODE(0xa1c4554)') called at /us +r/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP. +pm line 107 Class::MOP::load_first_existing_class('MyApp') called at /usr/loca +l/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP.pm lin +e 118 Class::MOP::load_class('MyApp') called at /usr/local/lib/perl5/sit +e_perl/5.10.0/Catalyst/ScriptRole.pm line 61 Catalyst::ScriptRole::_run_application('Catalyst::Script::Server=H +ASH(0x9dce9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/Cata +lyst/Script/Server.pm line 144 Catalyst::Script::Server::__ANON__() called at /usr/local/lib/perl +5/site_perl/5.10.0/Catalyst/Restarter/Forking.pm line 20 Catalyst::Restarter::Forking::_fork_and_start('Catalyst::Restarter +::Forking=HASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/ +5.10.0/Catalyst/Restarter.pm line 119 Catalyst::Restarter::_handle_events('Catalyst::Restarter::Forking= +HASH(0xa0e0fc4)', 'File::ChangeNotify::Event=HASH(0xa0f7eec)') called + at /usr/local/lib/perl5/site_perl/5.10.0/Catalyst/Restarter.pm line +96 Catalyst::Restarter::_restart_on_changes('Catalyst::Restarter::For +king=HASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/5.10. +0/Catalyst/Restarter.pm line 84 Catalyst::Restarter::run_and_watch('Catalyst::Restarter::Forking=H +ASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/5.10.0/Cata +lyst/Script/Server.pm line 178 Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x9dc +e9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-th +read-multi/Class/MOP/Method/Wrapped.pm line 48 Class::MOP::Method::Wrapped::__ANON__('Catalyst::Script::Server=HA +SH(0x9dce9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/i386- +linux-thread-multi/Class/MOP/Method/Wrapped.pm line 89 Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x9dc +e9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/Catalyst/Scri +ptRunner.pm line 20 Catalyst::ScriptRunner::run('Catalyst::ScriptRunner', 'MyApp', 'Se +rver') called at script/app_cinema_server.pl line 8
      And this one is from adding 'use Moose;' in MyApp::Model::MD.pm

      Thanks.
      Couldn't load class (MyApp) because: Couldn't instantiate component "M +yApp::Model::MD", "Attribute (schema_class) is required at /usr/local +/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP/Class.p +m line 365 Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x +a82fc04)', 'HASH(0xad3e564)') called at /usr/local/lib/perl5/site_per +l/5.10.0/i386-linux-thread-multi/Class/MOP/Class.pm line 352 Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0xa82fc04)' +, 'HASH(0xad3e564)') called at /usr/local/lib/perl5/site_perl/5.10.0/ +Moose/Meta/Class.pm line 205 Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0xa82fc04) +', 'HASH(0xad3e564)') called at /usr/local/lib/perl5/site_perl/5.10.0 +/Moose/Object.pm line 25 Moose::Object::new('MyApp::Model::MD', 'MyApp', 'HASH(0xa8e0244)') + called at /usr/local/lib/perl5/site_perl/5.10.0/MooseX/Traits/Plugga +ble.pm line 131 MooseX::Traits::Pluggable::new_with_traits('MyApp::Model::MD', 'My +App') called at /usr/local/lib/perl5/site_perl/5.10.0/CatalystX/Compo +nent/Traits.pm line 145 CatalystX::Component::Traits::COMPONENT('MyApp::Model::MD', 'MyApp +', 'HASH(0xa6ac42c)') called at /usr/local/lib/perl5/site_perl/5.10.0 +/Catalyst.pm line 2310 eval {...} called at /usr/local/lib/perl5/site_perl/5.10.0/Catalys +t.pm line 2310 Catalyst::setup_component('MyApp', 'MyApp::Model::MD') called at / +usr/local/lib/perl5/site_perl/5.10.0/Catalyst.pm line 2227 Catalyst::setup_components('MyApp') called at /usr/local/lib/perl5 +/site_perl/5.10.0/Catalyst.pm line 1112 Catalyst::setup('MyApp') called at /var/www/html/App-Cinema/script +/../lib/App/Cinema.pm line 24 require App/Cinema.pm called at /usr/local/lib/perl5/site_perl/5.1 +0.0/i386-linux-thread-multi/Class/MOP.pm line 98 Class::MOP::__ANON__() called at /usr/local/lib/perl5/site_perl/5. +10.0/Try/Tiny.pm line 42 eval {...} called at /usr/local/lib/perl5/site_perl/5.10.0/Try/Tin +y.pm line 39 Try::Tiny::try('CODE(0xa0f59c4)', 'CODE(0xa1c76dc)') called at /us +r/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP. +pm line 107 Class::MOP::load_first_existing_class('MyApp') called at /usr/loca +l/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP.pm lin +e 118 Class::MOP::load_class('MyApp') called at /usr/local/lib/perl5/sit +e_perl/5.10.0/Catalyst/ScriptRole.pm line 61 Catalyst::ScriptRole::_run_application('Catalyst::Script::Server=H +ASH(0x9dce9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/Cata +lyst/Script/Server.pm line 144 Catalyst::Script::Server::__ANON__() called at /usr/local/lib/perl +5/site_perl/5.10.0/Catalyst/Restarter/Forking.pm line 20 Catalyst::Restarter::Forking::_fork_and_start('Catalyst::Restarter +::Forking=HASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/ +5.10.0/Catalyst/Restarter.pm line 119 Catalyst::Restarter::_handle_events('Catalyst::Restarter::Forking= +HASH(0xa0e0fc4)', 'File::ChangeNotify::Event=HASH(0xa1ce7f4)') called + at /usr/local/lib/perl5/site_perl/5.10.0/Catalyst/Restarter.pm line +96 Catalyst::Restarter::_restart_on_changes('Catalyst::Restarter::For +king=HASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/5.10. +0/Catalyst/Restarter.pm line 84 Catalyst::Restarter::run_and_watch('Catalyst::Restarter::Forking=H +ASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/5.10.0/Cata +lyst/Script/Server.pm line 178 Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x9dc +e9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-th +read-multi/Class/MOP/Method/Wrapped.pm line 48 Class::MOP::Method::Wrapped::__ANON__('Catalyst::Script::Server=HA +SH(0x9dce9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/i386- +linux-thread-multi/Class/MOP/Method/Wrapped.pm line 89 Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x9dc +e9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/Catalyst/Scri +ptRunner.pm line 20 Catalyst::ScriptRunner::run('Catalyst::ScriptRunner', 'MyApp', 'Se +rver') called at script/app_cinema_server.pl line 8"Compilation faile +d in require at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thre +ad-multi/Class/MOP.pm line 98. at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Clas +s/MOP.pm line 102 Class::MOP::__ANON__('Couldn\'t instantiate component "MyApp::Mode +l::MD", "At...') called at /usr/local/lib/perl5/site_perl/5.10.0/Try/ +Tiny.pm line 66 Try::Tiny::try('CODE(0xa0f59c4)', 'CODE(0xa1c76dc)') called at /us +r/local/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP. +pm line 107 Class::MOP::load_first_existing_class('MyApp') called at /usr/loca +l/lib/perl5/site_perl/5.10.0/i386-linux-thread-multi/Class/MOP.pm lin +e 118 Class::MOP::load_class('MyApp') called at /usr/local/lib/perl5/sit +e_perl/5.10.0/Catalyst/ScriptRole.pm line 61 Catalyst::ScriptRole::_run_application('Catalyst::Script::Server=H +ASH(0x9dce9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/Cata +lyst/Script/Server.pm line 144 Catalyst::Script::Server::__ANON__() called at /usr/local/lib/perl +5/site_perl/5.10.0/Catalyst/Restarter/Forking.pm line 20 Catalyst::Restarter::Forking::_fork_and_start('Catalyst::Restarter +::Forking=HASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/ +5.10.0/Catalyst/Restarter.pm line 119 Catalyst::Restarter::_handle_events('Catalyst::Restarter::Forking= +HASH(0xa0e0fc4)', 'File::ChangeNotify::Event=HASH(0xa1ce7f4)') called + at /usr/local/lib/perl5/site_perl/5.10.0/Catalyst/Restarter.pm line +96 Catalyst::Restarter::_restart_on_changes('Catalyst::Restarter::For +king=HASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/5.10. +0/Catalyst/Restarter.pm line 84 Catalyst::Restarter::run_and_watch('Catalyst::Restarter::Forking=H +ASH(0xa0e0fc4)') called at /usr/local/lib/perl5/site_perl/5.10.0/Cata +lyst/Script/Server.pm line 178 Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x9dc +e9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/i386-linux-th +read-multi/Class/MOP/Method/Wrapped.pm line 48 Class::MOP::Method::Wrapped::__ANON__('Catalyst::Script::Server=HA +SH(0x9dce9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/i386- +linux-thread-multi/Class/MOP/Method/Wrapped.pm line 89 Catalyst::Script::Server::run('Catalyst::Script::Server=HASH(0x9dc +e9bc)') called at /usr/local/lib/perl5/site_perl/5.10.0/Catalyst/Scri +ptRunner.pm line 20 Catalyst::ScriptRunner::run('Catalyst::ScriptRunner', 'MyApp', 'Se +rver') called at script/app_cinema_server.pl line 8

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://819361]
Approved by lidden
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found