Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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


Comment on Re: changing moose object classes on the go
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2015-07-31 01:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls