I need a serializer that can be instructed to replace some inner objects on the fly just before serializing then using a callback.

Storable has the STORABLE_freeze and STORABLE_thaw hooks but they are not per-object but per-class and besides that, they are methods of the class, not something that can be controlled by the code calling the serializer.

I was thinking of extending Storable, but frankly, its code its one of the worst examples of code rot I have ever seen and so, my question.

Sereal may be worth looking at. It doesn't have the hooks you're looking for, but based on my experience (porting Sereal::Encoder to PHP) it's relatively clean code if you want to extend it. (Albeit mostly XS.)

It's also an actively developed framework and format, so you might have a good chance of getting your extensions adopted into the Sereal::{Encoder,Decoder} modules.

Storable has produced some very interesting .. ahem .. problems for me that I never quite figured out. I would freeze something, then thaw it again, and it came out corrupted. Some did, some didn’t. I don’t know if it was the Perl version, the system (an AS/400), or what. But what I did find, that worked out equally well for me (given that storage-space was not a concern), was good ol’ JSON, stored in a memo-field in a database. (Which had the added bonus of being human-readable.) I’ve worked with plenty of legacy systems since then which seem to have no problems with it, but I’ve become gun-shy.