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:
- I've looked at mixin, Class::Mixin, and Spiffy, but they don't seem to have a configurable factory object or support run-time mixin selection.
- The Class::Mix and Class::Mutator modules do provide run-time class generation with multiple inheritance, but don't provide a configurable factory object or a redispatch technique.
- The Class::Factory module has a factory interface but doesn't support multiple inheritance.
- There are also a number of packages which are addressing a similar area of concern, but don't use mixins, including: Class::Role, Class::Roles, Class::Trait, and Aspect.
Feedback would be welcome; some of the questions I'm considering are:
- Does the package name seem appropriate?
- Does the public interface seem reasonable?
- Are there other modules out there that do this same thing?
- Does the documentation manage to explain why someone might want to do such a thing?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.