深度克隆对象和 Log4perl
我使用 Storable 的 dclone() 来创建各种复杂对象的副本,这些对象是附加 Log4perl 记录器的自日志记录。 Storable 在遇到记录器对象时会抱怨,因为它包含 CODE 引用。当我打开序列化代码引用的选项时,我
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.
在克隆过程中收到不祥的警告,并且克隆对象的记录器不再起作用。
我正在克隆的对象由其他对象组成,记录器可能附加到该对象、其组件、它们的子组件等。
理想情况下,我希望克隆过程忽略任何记录器对象。
1)是否有任何 Perl 模块(更)适合这项任务?理想情况下,我希望能够控制递归复制,以便仅复制不是记录器的对象或数据片段。
2) ...或者我最好从我的对象中取出对象的自记录功能(呜呜!)并创建不附加到对象的记录器?
任何建议或见解将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用Storable 的钩子控制序列化和反序列化。我自己没有使用过这些钩子,但它们应该可以解决问题。
您也许能够将对象的数据复制到一个简单的散列(没有日志位),对该散列调用
Storable::freeze
,并将其作为序列化形式返回;然后,在解冻钩中,您只需反转该过程并连接您的记录材料即可。一些实验可能会产生一个更漂亮的解决方案,但是这种“冻结对象的散列化版本”方法应该可行,并且它将为您提供一个起点。您还可以检测挂钩中的克隆,并执行需要通过日志记录完成的任何操作。
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.