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) su开发者_JAVA技巧itable 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.
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.
精彩评论