Hi all,

This problem is annoying and I'm not sure of the best approach.

Let's say I have a list of roles and an object and I want to serialize the 
object as XML.  Imagine the following (this is my actual use case):

  sub to_xml {
      my $self = shift;
      my $xml = ''; # string XML sucks. Just an example :)
      foreach my $role ($self->meta->roles) { # or whatever it's called
        next unless $role->meta->does('DoesXML');
        $xml .= $role->as_xml($self); # method available on the role itself
      }
      return $xml;
  }

In other words, tying methods and roles specifically to roles and their state 
might be useful.  Here's a better(?) example, also pseudo-code:

  package Thing;
  use Moose;
  with qw(
    DoesRobot
    DoesDrawable
  );
 
Now imagine that both the robot and drawable roles provide a draw() method, but 
the robot on tries to control a robotic arm with a pencil.  Canonically, you do 
something like this:

  package Thing;
  use Moose;
  with (
    DoesRobot => { excludes => 'draw', aliases => { draw => 'draw_with_arm' },
    'DoesDrawable'
  );

Now if another class tries to instantiate Thing->new, it doesn't really know 
what the draw() method has been renamed to for the Robot role.  You can get to 
it through reflection (I think), but that's annoying.  What's the easiest way 
to *not* worry about this method being renamed?  Is it something like this 
awful pseudo-code?

  $thing->meta->does_role('DoesRobot')->get_method('draw')->($thing);

Does that make sense?

Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://use.perl.org/~Ovid/journal/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6

Reply via email to