Notice that Randal's code supports copying only structures composed of arrays and hashes. You really do not want to break object abstractions by copying their guts - it is bound to lead to problems sooner or later. Anyway, in the most general sense, you cannot always correctly copy objects, handles, code, etc.

Code is a problem, since there may be persistent state associated with it - how do you duplicate it? Handles almost always have state that cannot be duplicated to an independent copy.

Even if you assume that your structure does not contain anything but pure Perl objects, those object can contain references to code and handles. Moreover, the class implementations for the objects may be keeping class-wide state, which means the only valid way of copying the object implementation (from the class point of view) is to use a class-specific copy constructor, if the class allows this by implementing one.

This, of course, does not disagree with the other posters' suggestions - you can always take the risk and just deep-copy and re-bless anything you find. Just watch out for the consequences.


In reply to Re: deep_copy objects question by GreyGlass
in thread deep_copy objects question by JAMBOID

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.