|Problems? Is your data what you think it is?|
Do you think it's kosher to "re-bless" a Moose object?by sundialsvc4 (Abbot)
|on Nov 17, 2011 at 14:29 UTC||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.”