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.
I know that I can use Storable, Clone, Clone::More, Clone::Fast, Data::Dumper, etc. What’s the current best practice?
Cloneis much faster thanStorable::dclone, but the latter supports more data types.Clone::FastandClone::Moreare pretty much equivalent if memory serves me right, but less feature complete than even Clone, andScalar::Util::Clonesupports 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.
If you have no specific performance needs I would just use Storable’s dclone.
I wouldn’t use
Data::Dumperfor this simply because it’s so cumbersome and roundabout. It’s probably going to be very slow too.For what it’s worth, if you ever want customizable cloning then
Data::Visitorprovides hooking capabilities and fairly feature complete deep cloning is the default behavior.