Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

changing moose object classes on the go

by tomgracey (Beadle)
on Mar 19, 2013 at 18:10 UTC ( #1024349=perlquestion: print w/ replies, xml ) Need Help??
tomgracey has asked for the wisdom of the Perl Monks concerning the following question:

Supposing I have a dog

my $dog = DOG->new; Then later I decide my dog is in fact a labrador. I could do bless $dog, 'LABRADOR';

which (i think) would be fine as LABRADOR inherits from DOG

but Moose has things like

has color => (is => 'rw', isa => 'Str', default => 'golden')

If I simply bless my $dog into LABRADOR then he won't be golden. How can I resolve this situation?

(I guess what I'm really asking is - is there a way to change the class of an object that already exists under Moose? Or perhaps this is not the done thing?)

Replies are listed 'Best First'.
Re: changing moose object classes on the go
by moritz (Cardinal) on Mar 19, 2013 at 18:40 UTC
    If I simply bless my $dog into LABRADOR then he won't be golden. How can I resolve this situation?

    By creating a new LABRADOR object, and using it instead of the old object. You can reuse the attributes from the original DOG object.

    Seriously, changing the type of an object is a an usual requirement, and might point to a flaw in your overall design. If a dog is a labrador, it's already born a labrador. Or phrased differently, time-dependent properties should not encoded in the type.

Re: changing moose object classes on the go
by blue_cowdawg (Monsignor) on Mar 19, 2013 at 18:45 UTC
        (I guess what I'm really asking is - is there a way to change the class of an object that already exists under Moose? Or perhaps this is not the done thing?)

    Pretty much the same sort of question I asked a while ago here. The quick answer is as follows:

    package Dog; use Moose; has 'breed' => ( is => 'ro',isa=>'String'); has 'color' => ( is => 'rw', isa=>'String'); sub BUILD { my $self =shift; my $class = "Dog::" . $self->breed(); $class->meta->rebless_instance($self); } 1;
    and somewhere in user space:
    | handwaving here.. use Dog; # updated this on 2013-04-24, error in original code my $dog = Dog->new(breed=>'Poodle'); | |


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Hi Peter

      Realise its a while ago and I forgot to thank you for this - basically ->rebless_instance($self) was the answer. Thanks enormously!

Re: changing moose object classes on the go
by Brutha (Friar) on Apr 08, 2013 at 13:19 UTC

    I had a similar problem. I receive a minimal abstract object and changed it into a specialized one to make life easier. What I did was creating a new class "Labrador" with the superclass "Dog" and a role "LabradorMethodsAndAtributes". Well, in my case it wasn't dogs.

    Shortened, adjusted, unrunable code:

    my $class_name = "Felidae::Tiger"; # Does the class already exist? return $class_name if is_class_loaded($class_name); # No, create it my $superclass = "Felidae"; my $role_name = "Felidae::Role::Tiger"; # create the Role my $role = Moose::Meta::Role->create($role_name); # A leopard does not have stripes but spots $role->add_attribute( "stripecount", ( is => 'rw', isa => 'Int', ... # builder, lazy etc ) ); my $my_class = Moose::Meta::Class->create( $class_name, superclasses => [$superclass], roles => [$role_name], ); my $garfield = $class_name->new($felidae_obj);

    Now you have an object which isa "Felidae" and a "Tiger" and does "stripecount".

    And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
    (Terry Pratchett, Small Gods)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024349]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2016-06-30 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (404 votes). Check out past polls.