Re: roles verify attribute contents when applied?

[email protected] (Dave Rolsky)
Newsgroups perl.moose
Message-ID <[email protected]>
On Thu, 12 Jan 2012, Mark Hedges wrote:

> I don't know if I'm going about this in the right way.  I want to 
> represent a machine.  Machines may be different hardware types.  It gets 
> a role for the hardware type that supplies methods that might be able to 
> powercycle it.  I am applying the hardware-specific role using 
> apply_all_roles to the instance rather than consuming a role in the 
> class. (Since it gets a different role based on which type it is, and 
> each one of these roles supplies the same named method.) I just wanted 
> to double-check when I apply the role to the object that it is the 
> correct type.

It seems to me that this would be better written like this:

   package Machine;
   use Moose;

   has power_cycler => (
       is => 'ro',
       does => 'PowerCycler',
       handles => [ 'restart' ],
   );

In other words, make a separate object that represents the type of power 
cycler (or whatever you call it) and let that handle restarting the 
machine.

You may need to set up a circular reference between the two objects, in 
which case this may be necessary:

   sub BUILD {
       my $self = shift;

       $self->power_cycler->machine($self0;
   }

You should make one of these a weak ref (probably the ref from cycler to 
machine) to precent memory leaks.


-dave

/*============================================================
http://VegGuide.org               http://blog.urth.org
Your guide to all that's veg      House Absolute(ly Pointless)
============================================================*/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.