I'm working on a class that is a test-bed of sorts, and I want to explore some good way to provide for "plug in" behavior.

In Perl6, you have a but keyword that will dynamically apply a role to an instance, without having to name a new class for it and create the instance of that type.

I saw some clues in the manual the Moose might do this even though it doesn't offer syntactic sugar for it. Is there a good way to do this directly (as opposed to messing with MOP directly)?

That is, can Moose add a role ("traits") to an instance after it has been created, as opposed to having to declare a derived class that is based on the original class and adds the role as well? I'm thinking that one "plug in" might need to add roles to several collaborating objects, as well as do more mundane things with those object's APIs.

Catalyst, for example, lets you specify some extensions as arguments to new. Along the lines I'm thinking, the BUILD routine can slurp them in.


In reply to Extensions via Moose 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.