in reply to Moose 'clone' Constructor

Others have already pointed out how you can use the metaobject protocol to clone objects with Moose. If you know that the object in question is a Moose object, this is a reasonable solution. I want to mention my module Object::Util though. It contains a bunch of utility methods which can be used with Moose objects and other objects too.

use Object::Util; my $cloned_object = $existing_object->$_clone(%extra_parameters);

Object::Util will first check if the existing object has a clone method, and if so, will just call that. After that, it falls back to using the metaobject protocol if it detects the object is Moose- or Mouse-based. Finally as a last resort, if the object is a blessed hashref, it simply does a shallow copy of the hashref and blesses the new hashref.

Object::Util has a bunch of other cool things too, like:

$cloned_object->$_extend({ my_new_method => sub { my $self = shift; print "yay!\n"; }, }); $cloned_object->my_new_method(); # says "yay!"

Note that the methods provided by Object::Util are coderefs in scalar variables, so you call $object->$_method() instead of $object->method(). This is a way to make the methods available for all objects of all classes without polluting UNIVERSAL.

Replies are listed 'Best First'.
Re^2: Moose 'clone' Constructor
by kcott (Archbishop) on Jul 05, 2018 at 04:18 UTC

    G'day tobyink,

    ++ Thanks for pointing me to that module: I hadn't encountered it previously.

    I found the way you've used coderefs interesting. I suspect I may have seen something similar in the past but probably wasn't paying close enough attention. I'm certainly familiar with:

    $ perl -E 'my $x = sub { say "@_" }; $x->("fred")' fred

    And, as expected, this didn't work:

    $ perl -E 'my $x = sub { say "@_" }; $x("fred")' syntax error at -e line 1, near "$x(" Execution of -e aborted due to compilation errors.

    However, I've never written code like:

    $ perl -E 'my $x = sub { say "@_" }; "XYZ"->$x("fred")' XYZ fred

    I'll have to look into that further. I've read the "Object::Util - Rationale" section. Can you point me to any documentation that explains this in more detail?

    — Ken

        ++ Many thanks. That shines a light on all the grey areas, including (from my earlier post):

        "XYZ"->$x("fred")

        being exactly equivalent to

        $x->("XYZ", "fred")

        — Ken

      And, as expected, this didn't work:

      but this does

      perl -E 'my $x = sub { say "@_" }; &$x("fred")'

      as to "XYZ"->$x("fred"), I would not have expected it to work, becuase "XYZ" is not blessed to anything, but seeing that it does, it behaves like i expected, with $thing->method($arg) calling &method as method($thing,$arg)

        G'day huck,

        Dereferencing the coderef (&$x) I understand. @_ holding the invocant and any other arguments I understand.

        "I would not have expected it to work, ..."

        Me, neither. That's the bit I'm trying to understand.

        — Ken