Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Do you think it's kosher to "re-bless" a Moose object?

by sundialsvc4 (Monsignor)
on Nov 17, 2011 at 14:29 UTC ( #938625=perlquestion: print w/ replies, xml ) Need Help??
sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:

In my application, I have to deal with objects (Tests) which can come in a few more-specific flavors (such as Essay and MultipleChoice).   Each of these are subclasses of Test.   When I load the descriptor record from the database and set about the process of populating the object, one of the things that I can determine from the database-row is exactly what kind (subtype...) of Test it is.

What I am considering doing, in response to this, is to do (say...) bless $self,"Test::MultipleChoice"; ... where $self is at that point always blessed as the parent-class, Test.

But my obvious concern is, “Moose Magic.”   Would it work?   The magickal things that happen in a Moose constructor would not happen when re-blessing, and so I suspect that the answer is “no.”

Another Way To Do It™ would probably be to use Moose traits.   (And, in retrospect, maybe that is the much-better way to do it?)   But once again, will it work?   And, does that preclude me from using make_immutable?

Yet Another Way To Do It™ is with a separate “factory” object (or simply, package, that knows how to read the record, figure out what kind of Test to instantiate, instantiates that object and then hands a copy of the database-row to it.

The downside, perhaps, is that I can no longer say, “object, go load() yourself...”   And what if the user is making a new Test and at some point along the way decides that it is to be a MultipleChoice test?   (That might be an argument for Traits, if the Traits list can be changed on-the-fly.)

I don’t have anyone to impress here, but I will be living with this design for a very long time.   And so, I’d gratefully receive the Wisdom of the Monks.   “Several paths diverge in the wood” here, “and this will make all the difference.”

Comment on Do you think it's kosher to "re-bless" a Moose object?
Re: Do you think it's kosher to "re-bless" a Moose object?
by Anonymous Monk on Nov 17, 2011 at 14:43 UTC
    Channelling mst from #moose:

    Better to apply a role to an instance, or if you really must change the class use ->meta->rebless_instance. But applying a role at runtime is generally better. And if you have the data early enough, $class->with_traits(...)->new using MooseX::Traits would be ideal.

      It would be very helpful indeed if you could elaborate a bit on just what you mean by “early enough” ...

        It would be very helpful indeed if you could elaborate a bit on just what you mean by “early enough” ...

        He means that if you know what traits you want applied before the instance is created, then MooseX::Traits is likely useful (since you use it prior to constructing an instance). But if you don't know what traits you want to apply until after, then you can just use runtime role application (there is a cookbook for that I believe).

        -stvn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-09-02 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls