Storable's dclone does what you are after, but may be heavier than you need, since it does a full freeze and thaw to and from memory. The advantage to dclone is it supports hooks to define serialization behavior. But often that's not needed. In cases where you don't need that flexibility, Clone provides a clone function that is somewhat lighter weight, and for a typical data structure will be faster.

In some cases rather than cloning a data structure you can get away with using local to override certain elements within the structure in a way where the changes unroll at end of dynamic scope. An example of this would be passing a structure in as an argument list, and needing to massage those args without having the changes propagate back to the caller. In that case, localizing a key or an element before fiddling with its value is a good approach that avoids a full clone.


Dave


In reply to Re^3: Unable to make shallow copy of Moo hash ref attribute by davido
in thread Unable to make shallow copy of Moo hash ref attribute by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.