MooseX:...:Cloning(3)

πŸ“¦
version 0.35 Β· 2 versions
, in fedora 43

πŸ’– MooseX::Extended::Manual::Cloning πŸ’–

okay bestie, gather round, because we're talking about MooseX::Extended::Manual::Cloning β€” the manual page that gives you the whole rundown on MooseX::Extended's optional attribute cloning. yes, that's the whole vibe. it's giving "overview energy" and we're obsessed. ✨

version: 0.35. that's the one, no cap.


πŸŽ€ CLONING SUPPORT πŸŽ€

so here's the tea, bestie: MooseX::Extended offers optional, EXPERIMENTAL support for attribute cloning β€” but the way it works is lowkey different from how you usually see this done. πŸ’…

the easy mode? just pass the clone => 1 argument to your attribute. that's it. it'll get cloned with Storable's dclone function every single time you read or write that attribute. and if it's a reference, it gets cloned too β€” which means your object is effectively immutable. iconic behavior honestly. πŸ”₯

but wait, there's more, because of course there is. if clone => 1 isn't your thing, you can also pass:

  • a code reference, or
  • the name of a method that you'll use to clone the object.

either way, each one receives three arguments: $self, $attribute_name, $value_to_clone. three lil guys. that's the whole contract. 🫢

here's a full example, straight from the test suite β€” not me gatekeeping, it's literally right here:

package My::Class {
    use MooseX::Extended types => [qw(NonEmptyStr HashRef InstanceOf)];

    param name => ( isa => NonEmptyStr );

    param payload => (
        isa    => HashRef,
        clone  => 1,  # uses Storable::dclone
        writer => 1,
    );

    param start_date => (
        isa   => InstanceOf ['DateTime'],
        clone => sub ( $self, $name, $value ) {
            return $value->clone;
        },
    );

    param end_date => (
        isa    => InstanceOf ['DateTime'],
        clone  => '_clone_end_date',
    );

    sub _clone_end_date ( $self, $name, $value ) {
        return $value->clone;
    }

    sub BUILD ( $self, @ ) {
        if ( $self->end_date < $self->start_date ) {
            croak("End date must not be before start date");
        }
    }
}
⚠️ Warning, bestie, listen up: this is a very useful technique, but cloning can be very expensive. 😭 if you're having performance issues, profile your code and see if removing the safety of cloning can help. don't just rawdog it and hope, okay? profile first. πŸ™ˆ

πŸ‘‘ AUTHOR πŸ‘‘

Curtis "Ovid" Poe <curtis.poe@gmail.com>


This software is Copyright (c) 2022 by Curtis "Ovid" Poe.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)

and that's a wrap, my love πŸ’œ this was MooseX::Extended::Manual::Cloning, served up from perl v5.42.0, dated 2025-07-25. go forth and clone responsibly (or don't clone at all if your profiler is screaming at you). mwah! πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from