Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: changing moose object classes on the go

by blue_cowdawg (Prior)
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
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 studying the Monastery: (5)
As of 2014-08-31 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls