Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: changing moose object classes on the go

by blue_cowdawg (Monsignor)
on Mar 19, 2013 at 18:45 UTC ( #1024358=note: print w/replies, xml ) Need Help??

in reply to changing moose object classes on the go

      (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

Replies are listed 'Best First'.
Re^2: changing moose object classes on the go
by tomgracey (Beadle) on Apr 24, 2013 at 09:55 UTC

    Hi Peter

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024358]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2017-12-16 10:14 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (449 votes). Check out past polls.