在 Perl 中制作数据结构的深层副本的最佳方法是什么? [英] What's the best way to make a deep copy of a data structure in Perl?

查看:23
本文介绍了在 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.

我知道我可以使用 Storable克隆、克隆::更多、Clone::Fast, 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.

这篇关于在 Perl 中制作数据结构的深层副本的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆