0

I am using Storable's dclone() to create copies of various complex objects, which are self-logging with Log4perl loggers attached. Storable complains when it encounters the logger object because it contains CODE refs. When I turn on the option to serialize code refs, I get the ominous warning

Useless use of a constant (???) in void context at (eval 1668) line 16.
Useless use of a constant (???) in void context at (eval 1668) line 10.

during the cloning process, and the cloned object's logger no longer functions.

The objects that I'm cloning are composed of other objects, and the loggers may be attached to the object, its components, their subcomponents, etc..

Ideally I would like the cloning process to ignore any logger objects.

1) Are there any Perl modules that would be (more) suitable for this task? Ideally I would like to be able to control the recursive copying so that only objects or pieces of data that weren't loggers were copied.

2) ...or would I be better off taking out the self-logging capabilities of the objects (sob!) from my objects and create loggers that aren't attached to the objects?

Any advice or insight would be appreciated.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40

1 Answers1

0

You can control serialization and deserialization with Storable's hooks. I haven't used the hooks myself but they should do the trick.

You might be able to copy your object's data to a simple hash (without the logging bits), call Storable::freeze on that hash, and return that as the serialized form; then, in the thawing hook you'd just reverse that process and wire up your logging stuff. A bit of experimentation might yield a prettier solution but this "freeze a hash-ified version of the object" approach should work and it will give you a starting point.

You can also detect cloning in the hooks and do whatever needs to be done with the logging.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Part of the problem is that I couldn't get Storable's hooks working when I was experimenting with possible solutions yesterday. I have implemented hooks now to check for logger objects and delete them if they exist... it seems like a bit of a nasty hacky solution, but maybe I'll just put up with it. :S – i alarmed alien Mar 24 '11 at 18:50
  • I don't know if "nasty" applies but it is a bit of hack. OTOH, the hooks are there for a reason so it is a hack but not a kludge. – mu is too short Mar 24 '11 at 19:27
  • Better a hack than a kludge, I guess! ;^) – i alarmed alien Mar 25 '11 at 04:07
  • 1
    A *hack* is somewhat respectable, a *kludge* is something you don't talk about in polite company. – mu is too short Mar 25 '11 at 04:38