Well yesterday I meditated about ways to implement this in Perl...

While I don't know the Moose guts, I remember that Moose was said to stay compatible to classical OO, mostly just adding syntactic sugar. ²

So I suppose that the normal search-path isn't manipulated in Moose.

This led me to 2 possibilities to implement this (rather JS-ish) requirement.

> by creating a new anonymous class with the object's previous class as superclass and with the roles applied,

Thats the first approach I thought about. But I'm worried about side effects. How transparent is this new class in the middle, does the instance still officially belong to the superclass?

FWIW my approach to implement the OP's state machine would certainly be to openly create sub-classes for each state with a common superclass defining the interface.

Like this state-changes could simply be realized with reblessings.

Different roles could still be applied to these "state"-classes, can't see any need to avoid inheritance here.

The other approach to have individual methods per instance could defining an attribute hash instance_methods which holds names => sub {} for each instance-method.

However calling this indiviual methods would imply the need to inherit equally named interface methods in the class to delegate the call.¹

Alternative an AUTOLOAD mechanism comes into mind, but these methods would have the lowest priority in the search path then.

BUT I can't see any need to implement a state machine this way.

IMHO the euphoria for mixins in Ruby was taken a bit to far.

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE

¹) such that  $obj->my_meth() does something like $self->{instance_methods}{my_meth}->(@_).

²) plz correct me if I'm wrong!


In reply to Re^3: a State machine with Roles - possible? (class or instance) by LanX
in thread a State machine with Roles - possible? by mascip

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.