http://qs321.pair.com?node_id=11102872


in reply to YAML::Tiny can't serialize an object reference. Can I unbless a Moo object into a hashref and yammily serialize that?

I would consider unblessing a bad idea. My recommendation would be to provide a method on your class to provide a hashref representation of your object, and use that in the value you send to YAML::Tiny. Moo just uses normal hashref objects, so the method can be pretty simple and just do a shallow clone, without re-blessing the result.

sub as_hashref { return { %{ $_[0] } }; }

This gives you an easy place to change how the object is serialized if you need it in the future, but doesn't over-complicate things. The more "correct" way to do this would involve using an explicit list of attributes and using their accessors, but it isn't really a problem to rely on it being a normal hashref based object. You are breaking encapsulation, but it's your object, so you can choose if that matters to you.

While Moo can work with objects that aren't based on hashrefs or use other non-standard storage, that isn't the default and is something you'd have to explicitly enable with a MooX module.