MooseX::Clone(3pm)

πŸ“¦
version 0.06 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ MooseX::Clone ✨

okay bestie, so this is MooseX::Clone, a page from the User Contributed Perl Documentation section of the manual, and it's literally about fine-grained cloning support for Moose objects πŸ’– yes we're making copies, yes they're good copies, no cap.


πŸŽ€ VERSION πŸŽ€

version 0.06


🌸 SYNOPSIS 🌸

package Bar;
use Moose;

with qw(MooseX::Clone);

has foo => (
    isa => "Foo",
    traits => [qw(Clone)], # this attribute will be recursively cloned
);

package Foo;
use Moose;

# this API is used/provided by MooseX::Clone
sub clone {
    my ( $self, %params ) = @_;

    # ...
}

# used like this:

my $bar = Bar->new( foo => Foo->new );

my $copy = $bar->clone( foo => [ qw(Args for Foo::clone) ] );

πŸ¦‹ DESCRIPTION πŸ¦‹

so like, out of the box Moose only gives you very barebones cloning support, and it does that on purpose β€” to maximize flexibility. it's giving minimalist. it's giving "we trust you".

this role provides a clone method that takes all that low level cloning support already chilling in Moose, and adds selective deep cloning based on introspection on top of it. the way that works is: attributes with the Clone trait handle cloning of data within the object, typically delegating to the attribute value's own clone method. obsessed honestly πŸ’…


πŸ‘‘ TRAITS πŸ‘‘

Clone

by default, Moose objects are cloned like this:

bless { %$old }, ref $old;

by specifying the Clone trait for certain attributes, custom behavior kicks in and the value's own clone method will be invoked.

by extending this trait you can create custom cloning for certain attributes. yes, you. go off.

by creating clone methods for your objects (e.g. by composing MooseX::Compile) you can make them interact with this trait 🌈

NoClone

specifies attributes that should be skipped entirely while cloning. not invited to the copy party. not on the list. bye πŸ™ˆ


πŸ’… METHODS πŸ’…

  • clone %params β€” returns a clone of the object.

all attributes which do the MooseX::Clone::Meta::Attribute::Trait::Clone role will handle cloning of that attribute. all other fields are plainly copied over, just like in clone_object in Class::MOP::Class.

attributes whose init_arg is in %params and who do the Clone trait will get that argument passed to the clone method (dereferenced). if the attribute does not self-clone then the param is used normally by clone_object in Class::MOP::Class, that is it will simply shadow the previous value, and does not have to be an array or hash reference. periodt.


🧚 TODO 🧚

refactor to work in term of a metaclass trait so that meta->clone_object will still do the right thing. it's on the vision board bestie 😭


πŸ“ THANKS πŸ“

clkao made the food required to write this module 🫢 iconic behavior, truly the main character of snack provision.


πŸ’œ AUTHOR πŸ’œ

XXXX XXX'XX (Yuval Kogman) <nothingmuch@woobling.org>


this software is copyright (c) 2008 by XXXX XXX'XX (Yuval Kogman).

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


okay that's the whole page, no notes πŸ’– this was rendered with perl v5.34.0 and dated 2022-06-15 β€” ty for reading bestie, go clone something responsibly πŸŒΈπŸ’…βœ¨

πŸ“¦ where this page comes from