This is a more specific approach to something I was asking earlier, on how to extend classes in Moose. I've had quite a bit of discussion here and on #moose IRC, and have done a lot of reading too.

Here is a sample class to be used to illustrate the possible solutions and show the specific issues of the problem:

use 5.10.1; use utf8; package C; # main class with user-facing API use Moose; use MooseX::Method::Signatures; method foo (Str $x) { say "Called original foo($x)."; } method build_c2 { return C::C2->new; } has c2 => ( is => 'rw', handles => [ 'bar' ], builder => 'build_c2', lazy => 1 ); method build_c3 { return C::C3->new; } has c3 => ( is => 'rw', handles => [ 'baz' ], builder => 'build_c3', lazy => 1 ); # ================== package C::C2; # a helper class use Moose; use MooseX::Method::Signatures; method bar (Str $x) { say "Called original bar($x)."; } # ================== package C::C3; # a helper class use Moose; use MooseX::Method::Signatures; method baz (Str $x) { say "Called original baz($x)."; } # ================== package main; my $c= C->new; $c->foo (1); $c->bar (2); $c->baz (3);
Basically, a "complex" class as I dubbed it has its work factored out into various helper objects. Instances of several classes collaborate under the hood of the main object. Each class solves a well-defined part of the whole problem, and this is "good" and better code than throwing everything into one object, mingling the state data and methods in one big pot.

Now, lets say we have extensions to C. In order to work, an extension needs to hook the methods foo, bar, and baz. By "hook" I mean it can override or use before/after/around modifiers of some kind.

As you can see, the methods are spread out over three different classes (albeit in a simple way). I've also made the attributes "rw' for ease in solving the problem.

Approach 1: run-time patching (traits)

A call to install an extension can apply a run-time role to $c, and also apply roles to $c->c2 and $c->c3. But that is evil "run time" patching...

Approach 2: apply before creating

Using the proper pedigreed MooseX::Traits and calling $c= C->with_traits(qw/ext1 ext2/)->new will apply the role ext1 and ext2 to C, and then call new. But that only fixes foo. How does it make C-with-roles apply the roles to C2 and C3 as well? For a single extension, it can override the builders. That's why this example shows two roles: they have to play together. Only after all roles are applied can it know what to create.

How would you do that?


In reply to Test bed example: How to acheive plug-in for complex object? by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.