What's the best way to make a deep copy of a data structure in Perl?

后端 未结 4 493
我寻月下人不归
我寻月下人不归 2020-11-28 09:52

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 parti

相关标签:
4条回答
  • 2020-11-28 10:19

    My impression is that Storable::dclone() is somewhat canonical.

    0 讨论(0)
  • 2020-11-28 10:34

    Try to use fclone from Panda::Lib which seems the fastest one (written in XS)

    0 讨论(0)
  • 2020-11-28 10:35

    Clone is probably what you want for that. At least, that's what all the code I've seen uses.

    0 讨论(0)
  • 2020-11-28 10:37

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

    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.

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题