1MooseX::Extended::ManuaUls:e:rClCoonnitnrgi(b3u)ted PerlMoDoosceuXm:e:nEtxatteinodned::Manual::Cloning(3)
2
3
4
6 MooseX::Extended::Manual::Cloning - An overview of MooseX::Extended
7 optional attribute cloning
8
10 version 0.35
11
13 "MooseX::Extended" offers optional, EXPERIMENTAL support for attribute
14 cloning, but differently from how we see it typically done. You can
15 just pass the "clone => 1" argument to your attribute and it will be
16 cloned with Storable's "dclone" function every time you read or write
17 that attribute, it will be cloned if it's a reference, ensuring that
18 your object is effectively immutable.
19
20 If you prefer, you can also pass a code reference or the name of a
21 method you will use to clone the object. Each will receive three
22 arguments: "$self, $attribute_name, $value_to_clone". Here's a full
23 example, taken from our test suite.
24
25 package My::Class {
26 use MooseX::Extended types => [qw(NonEmptyStr HashRef InstanceOf)];
27
28 param name => ( isa => NonEmptyStr );
29
30 param payload => (
31 isa => HashRef,
32 clone => 1, # uses Storable::dclone
33 writer => 1,
34 );
35
36 param start_date => (
37 isa => InstanceOf ['DateTime'],
38 clone => sub ( $self, $name, $value ) {
39 return $value->clone;
40 },
41 );
42
43 param end_date => (
44 isa => InstanceOf ['DateTime'],
45 clone => '_clone_end_date',
46 );
47
48 sub _clone_end_date ( $self, $name, $value ) {
49 return $value->clone;
50 }
51
52 sub BUILD ( $self, @ ) {
53 if ( $self->end_date < $self->start_date ) {
54 croak("End date must not be before start date");
55 }
56 }
57 }
58
59 Warning: Be aware that this is a very useful technique, but cloning can
60 be very expensive. If you have performance issues, profile your code
61 and see if removing the safety of cloning can help.
62
64 Curtis "Ovid" Poe <curtis.poe@gmail.com>
65
67 This software is Copyright (c) 2022 by Curtis "Ovid" Poe.
68
69 This is free software, licensed under:
70
71 The Artistic License 2.0 (GPL Compatible)
72
73
74
75perl v5.36.1 2023-06-2M6ooseX::Extended::Manual::Cloning(3)