Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / MooseX / Clone / Meta / Attribute / Trait / Copy.pm
CommitLineData
3fea05b9 1#!/usr/bin/perl
2
3package MooseX::Clone::Meta::Attribute::Trait::Copy;
4use Moose::Role;
5
6use Carp qw(croak);
7
8use namespace::clean -except => 'meta';
9
10with qw(MooseX::Clone::Meta::Attribute::Trait::Clone::Base);
11
12sub Moose::Meta::Attribute::Custom::Trait::Copy::register_implementation { __PACKAGE__ }
13
14sub clone_value {
15 my ( $self, $target, $proto, %args ) = @_;
16
17 return unless $self->has_value($proto);
18
19 my $clone = exists $args{init_arg} ? $args{init_arg} : $self->_copy_ref($self->get_value($proto));
20
21 $self->set_value( $target, $clone );
22}
23
24sub _copy_ref {
25 my ( $self, $value ) = @_;
26
27 if ( not ref $value ) {
28 return $value;
29 } elsif ( ref $value eq 'ARRAY' ) {
30 return [@$value];
31 } elsif ( ref $value eq 'HASH' ) {
32 return {%$value};
33 } else {
34 croak "The Copy trait is for arrays and hashes. Use the Clone trait for objects";
35 }
36}
37
38__PACKAGE__
39
40__END__
41
42=pod
43
44=head1 NAME
45
46MooseX::Clone::Meta::Attribute::Trait::Copy - Simple copying of arrays and
47hashes for L<MooseX::Clone>
48
49=head1 SYNOPSIS
50
51 has foo => (
52 isa => "ArrayRef",
53 traits => [qw(Copy)],
54 );
55
56=head1 DESCRIPTION
57
58Unlike the C<Clone> trait, which does deep copying of almost anything, this
59trait will only do one additional level of copying of arrays and hashes.
60
61This is both simpler and faster when you don't need a real deep copy of the
62entire structure, and probably more correct.
63
64=cut
65
66