-2

Among the reasons I could think of not implementing a function in Perl's core are:

  1. The feature is rarely needed (avoid core bloat).

  2. The feature is easy to implement (the user is expected to do it quickly and correctly).

  3. The feature is extremely complicated to implement (avoid core bloat).

Wanting a deep copy to prevent unwanted data corruption (e.g. when some object method returns some nested structure being a "original"), I cannot find any of the reasons above to apply. That makes me wonder why there is no deep copy function in Perl core.

U. Windl
  • 3,480
  • 26
  • 54

1 Answers1

2

https://perldoc.perl.org/Storable - dclone is the thing you are looking for.

Note that freezing an object structure and immediately thawing it actually achieves a deep cloning of that structure:

dclone(.) = thaw(freeze(.))

Storable provides you with a dclone interface which does not create that intermediary scalar but instead freezes the structure in some internal memory space and then immediately thaws it out.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Ivan Dimitrov
  • 138
  • 1
  • 10
  • Agreed, `Storable` in in Perl core, but `dclone` is hardly documented rather than "There is a clone module available on CPAN which implements deep cloning natively, (...) It is aimed to replace Storable's dclone() some day.". – U. Windl Aug 27 '21 at 14:43
  • https://www.oreilly.com/library/view/perl-cookbook/1565922433/ch11s13.html Here it is documented a little bit more, but overall I can agree with you, it should be more documented, but with Perl losing popularity I highly doubt it. – Ivan Dimitrov Aug 27 '21 at 15:03
  • From the docs referenced "Writing your own code to deep-copy structures is laborious and rapidly becomes tiresome." seems to be an argument against suspected reason #2. What the docs do not explain is whether references within the structure to be copied are preserved, or whether they are "flattened" (duplicating sub-structures). Actually I implemented something similar simple, but cannot present it here as the question is "closed" already. – U. Windl Aug 30 '21 at 06:06