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

Greetings wise monks,

This time I need help with Moose.
Currently I'm writing some sort of "Addon/Plugin" thingy for a project.

The start.pl starts, creates an instance of the Addon-"Controller" class which looks into the AddOn Folder and returns a list of valid AddOns.
The user then selects an AddOn (not really a "plugin/addon" but a programm).
The AddOn-Controller then includes the corresponding Class, creates an instance of it and runs a (defined/required) subroutine. The other AddOns/classes are ignored and not loaded.

Or at least that's what should happen.
The original version of the script included the selected class via require but then I wanted to improve the system and found Moose.

Moose looked great and I wanted to try it out.
I tried many things & tips I found via Google but I can't include the selected class while ignoring the other ones.
If I include them in a static way the class, which I include last, overrides the start subs in the other classes (no sureprise).

So in short:
Can somebody give me a hint how I can do some sort of dynamic inheritance with Moose? Is it possible?

The Cookbook & documentation on CPAN gave me the impression that it's possible but I couldn't implement it into my own programm.

Thank you for your help/advice in advance,
Shinama

EDIT SAYS:
Another problem solved, see Re^2: Dynamic inheritance with Moose?
Thanks again for your help :)
---------------------------------------------------
"You read too much and understand too little."
Moiraine Damodred (The Wheel of Time)

Replies are listed 'Best First'.
Re: Dynamic inheritance with Moose?
by derby (Abbot) on Aug 06, 2009 at 15:55 UTC

    Module::Pluggable::Object and Class:MOP should be a good start. There's an example of doing this in the new Catalyst book. Something along the lines of this (not-tested code) should work

    my $mp = Module::Pluggable::Object->new( search_path => [ "/path/to/classes" ] ); my @classes = $mp->plugins; # choose which class from classes Class::MOP::load_class( $class ); return $class->new;
    Depending on how your search_path is, you may need to muck with the class name before calling new (like strip off the leading path).

    -derby
      Hello derby,

      thanks for your advice.
      I used the hint Anonymous Monk gave me and it worked but I'll look into your solution as well. Perhaps I'll need it one day and it's never a bad idea in perl to know more than one way to do it :D

      Thanks again for your help!

      Greetings from Germany,
      Shinama
      ---------------------------------------------------
      "You read too much and understand too little."
      Moiraine Damodred (The Wheel of Time)
Re: Dynamic inheritance with Moose?
by Anonymous Monk on Aug 06, 2009 at 14:36 UTC
    You can still build a system with Moose and dynamically include other modules via require. I don't see why you would want to use "dynamic inheritance," however -- simple aggregation should get the job done and those classes could inherit from a base class. Better yet -- try Moose::Role.

    If you have more questions, please consider posting some code for reference.

      Hello Anonymous,

      It now works, thanks to Moose::Role. I tried Moose::Role before but it didn't work. I think I made some errors with the namespaces of the files...
      I thought about using a "simple" require but I can't stand the thought of using something that's not included in the Framework. I mean where's the point in using it if you just write the code like you did before?

      Another error I made was calling make_immutable on the controller-class which results in an error because inheritance is not working after calling it (no sureprise, if I think about it ;( ).

      Here's the part of my programm which includes the other class (as a role) and then calles the corresponding config_module sub in the inherited class.

      sub load_config { my $self = shift; my $to_load = shift; with "LaborBelege::AddOns::$to_load\::main"; $self->config_module(); }
      Thanks for your advice, it finally got me on the right track.

      Greetings from Germany,
      Shinama
      ---------------------------------------------------
      "You read too much and understand too little."
      Moiraine Damodred (The Wheel of Time)