Think about Loose Coupling | |
PerlMonks |
Creating a Moose object and serializing itby daverave (Scribe) |
on Oct 23, 2010 at 00:42 UTC ( [id://866908]=perlquestion: print w/replies, xml ) | Need Help?? |
daverave has asked for the wisdom of the Perl Monks concerning the following question:
Disclosure: This question refers to a couple of previous questions posted at SO.
I am writing a module for a Moose object intended to be serialized. The module contains a method which implementation depends on one of the attributes the object is initialized with. One approach would be to write this method=d with an `if`, so each time the method is called it will decide which of the implementations it should follow. However, since after the object has been initialized, the method always works the same it seems to me it might be more elegant to make this decision only once, after initialization, and skip the if. It should be noted that the method I discuss is invoked intensively, so perhaps it would also be notably more efficient to skip the if. This is t what I came up with: I kind'a like this solution, but the problem is it uses coderef, which makes the object more difficult to serialize. I usually use `Storable`, but it seems not to like coderefs. I actually have two questions: 1. Do you find this coderef-using solution appropriate? Do you have any other ideas for coding the body of a method after object initialization? Perhaps this is all a huge overkill over a single `if`? 2. How would you recommend serializing Moose objects in general and this object in particular? I've been looking into `MooseX::Storage` but didn't find it very helpful. Specifically, it doesn't support coderefs.
Thanks,
Back to
Seekers of Perl Wisdom
|
|