Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Moose 'clone' Constructor

by Arunbear (Prior)
on Jul 04, 2018 at 09:54 UTC ( [id://1217873]=note: print w/replies, xml ) Need Help??


in reply to Moose 'clone' Constructor

See Object instance construction and cloning, but essentially:
my $cloned_object = $existing_object->meta->clone_object($existing_obj +ect);

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

    G'day Arunbear,

    ++ What can I say: great minds link alike. :-)

    When I got home from work, Corion's reply was the only response at that time. I followed the link he'd provided, followed some more links from that, then hit "reply". As I was composing, I noticed I now had a reply from you and thought: "Oh good, more information, I'll look at that in a minute." — and when I did, I then thought: "Hmmm, that looks familiar."

    Anyway, I've only just found it, perhaps you've used it. I noticed there's an additional argument:

    $metaclass->clone_object($instance, %params)

    Is %params as straightforward as it seems? I'm thinking maybe issues with values that are references to data structures, as opposed to simpler (Str, Int, etc.) values.

    — Ken

      %params are the same kind of data as were passed to the constructor when $existing_object was created (but now you have a chance to override those if needed).

        OK, thanks. I may have been thinking along the lines of:

        has x => (..., default => 'abc'); has y => (..., default => sub { [qw{a b c}] });

        where that second line can't be written as:

        has y => (..., default => [qw{a b c}]);

        But, of course, in the constructor you use the values as is (without needing a coderef):

        ...->new(x => 'def', y => [qw{d e f}]);

        — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217873]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found