Greetings brethren. Some of you may remember my comments and questions from last month about making effective use of multiple inheritance for run-time selection of mixin classes, in response to which I received some helpful input.
I've continued working on ideas drawn from those discussions and have assembled a reusable solution to this problem, and I'd like to run it by the monastery for feedback before posting it to CPAN.
The new package provides a class factory that creates new packages that inherit from multiple classes, and includes a method to allow mixin methods to redispatch to the base class functionality.
I've tentatively named it "Class::MixinFactory" in hopes of capturing the salient details -- it generates new classes by assembling mixins -- but would welcome alternate suggestions.
A simple use might look the following:
package MyClass; use Class::MixinFactory -hasafactory; sub new { ... } sub foo { return "Foo Bar" } package MyClass::Logging; sub foo { warn "Calling foo"; (shift)->NEXT('foo', @_) } package MyClass::UpperCase; sub foo { uc( (shift)->NEXT('foo', @_) ) } package main; my $class = MyClass->class( 'Logging', 'UpperCase' ); print $class->new()->foo(); # Calls MyClass::Logging::foo, MyClass::UpperCase::foo, MyClass::foo
I've posted a draft of the documentation online.
There are a number of related modules on CPAN, but none with quite the same mix of functionality:
Feedback would be welcome; some of the questions I'm considering are:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Stacking Mixin Classes at Run-time with Class::MixinFactory
by Anonymous Monk on Nov 16, 2004 at 18:18 UTC | |
by simonm (Vicar) on Nov 16, 2004 at 19:26 UTC | |
by Anonymous Monk on Nov 16, 2004 at 20:55 UTC | |
|
Re: Stacking Mixin Classes at Run-time with Class::MixinFactory
by simonm (Vicar) on Nov 29, 2004 at 23:00 UTC |