I seem to recall seeing somewhere a clone() method for Moose objects. I've looked in Moose and Moose::Manual::Construction but don't see anything like this. I've also had a poke around in Super Search but have found nothing promising.

In my current code, I writing this type of thing:

my $cloned_object = ref($existing_object)->new();

This works fine for what I'm working on at the moment because the classes in question have no required attributes and taking the defaults is exactly what I want in this instance.

I don't have any requirement to do anything different at the moment; however, it occurs to me that I might do in the future, perhaps something like:

my $cloned_object = ref($object)->new(%objects_attrs_and_values);

which might be better as something like:

my $cloned_object = $existing_object->clone();

So, is there such a thing as clone() or did I just imagine it? If it exists, please point me to documentation; if not, any related thoughts on this are welcome.

— Ken


In reply to Moose 'clone' Constructor by kcott

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.