在Perl中制作数据结构的深层副本的最佳方法是什么?

问题描述:

给出一个数据结构(例如哈希散列),为立即使用而制作一个深层副本的干净/推荐方式是什么?假设合理的情况下,数据不是特别大,不存在复杂的循环,并且可读性/可维护性等.不惜一切代价,比速度更重要.

Given a data structure (e.g. a hash of hashes), what's the clean/recommended way to make a deep copy for immediate use? Assume reasonable cases, where the data's not particularly large, no complicated cycles exist, and readability/maintainability/etc. are more important than speed at all costs.

我知道我可以使用可存储克隆,克隆::更多, Data :: Dumper 等.目前的最佳做法是什么?

I know that I can use Storable, Clone, Clone::More, Clone::Fast, Data::Dumper, etc. What's the current best practice?

CloneStorable::dclone快得多,但后者支持更多数据类型.

Clone is much faster than Storable::dclone, but the latter supports more data types.

Clone::FastClone::More几乎等效,但是功能甚至比Clone都要少,而Scalar::Util::Clone支持的甚至更少,但是IIRC对于某些结构来说是最快的.

Clone::Fast and Clone::More are pretty much equivalent if memory serves me right, but less feature complete than even Clone, and Scalar::Util::Clone supports even less but IIRC is the fastest of them all for some structures.

关于可读性,它们应该都一样工作,它们实际上是可以互换的.

With respect to readability these should all work the same, they are virtually interchangeable.

如果您没有特定的性能需求,我将只使用Storable的dclone.

If you have no specific performance needs I would just use Storable's dclone.

我不会使用Data::Dumper只是因为它是如此繁琐和环岛.可能还会很慢.

I wouldn't use Data::Dumper for this simply because it's so cumbersome and roundabout. It's probably going to be very slow too.

对于它的价值,如果您想进行可自定义的克隆,则Data::Visitor提供了挂钩功能,并且相当完整的深度克隆功能是默认行为.

For what it's worth, if you ever want customizable cloning then Data::Visitor provides hooking capabilities and fairly feature complete deep cloning is the default behavior.