1Clone::PP(3) User Contributed Perl Documentation Clone::PP(3)
2
3
4
6 Clone::PP - Recursively copy Perl datatypes
7
9 use Clone::PP qw(clone);
10
11 $item = { 'foo' => 'bar', 'move' => [ 'zig', 'zag' ] };
12 $copy = clone( $item );
13
14 $item = [ 'alpha', 'beta', { 'gamma' => 'vlissides' } ];
15 $copy = clone( $item );
16
17 $item = Foo->new();
18 $copy = clone( $item );
19
20 Or as an object method:
21
22 require Clone::PP;
23 push @Foo::ISA, 'Clone::PP';
24
25 $item = Foo->new();
26 $copy = $item->clone();
27
29 This module provides a general-purpose clone function to make deep
30 copies of Perl data structures. It calls itself recursively to copy
31 nested hash, array, scalar and reference types, including tied
32 variables and objects.
33
34 The clone() function takes a scalar argument to copy. To duplicate
35 arrays or hashes, pass them in by reference:
36
37 my $copy = clone(\@array); my @copy = @{ clone(\@array) };
38 my $copy = clone(\%hash); my %copy = %{ clone(\%hash) };
39
40 The clone() function also accepts an optional second parameter that can
41 be used to limit the depth of the copy. If you pass a limit of 0, clone
42 will return the same value you supplied; for a limit of 1, a shallow
43 copy is constructed; for a limit of 2, two layers of copying are done,
44 and so on.
45
46 my $shallow_copy = clone( $item, 1 );
47
48 To allow objects to intervene in the way they are copied, the clone()
49 function checks for a couple of optional methods. If an object provides
50 a method named "clone_self", it is called and the result returned
51 without further processing. Alternately, if an object provides a method
52 named "clone_init", it is called on the copied object before it is
53 returned.
54
56 Some data types, such as globs, regexes, and code refs, are always
57 copied shallowly.
58
59 References to hash elements are not properly duplicated. (This is why
60 two tests in t/dclone.t that are marked "todo".) For example, the
61 following test should succeed but does not:
62
63 my $hash = { foo => 1 };
64 $hash->{bar} = \{ $hash->{foo} };
65 my $copy = clone( \%hash );
66 $hash->{foo} = 2;
67 $copy->{foo} = 2;
68 ok( $hash->{bar} == $copy->{bar} );
69
70 To report bugs via the CPAN web tracking system, go to
71 "http://rt.cpan.org/NoAuth/Bugs.html?Dist=Clone-PP" or send mail to
72 "Dist=Clone-PP#rt.cpan.org", replacing "#" with "@".
73
75 Clone - a baseclass which provides a "clone()" method.
76
77 MooseX::Clone - find-grained cloning for Moose objects.
78
79 The "dclone()" function in Storable.
80
81 Data::Clone - polymorphic data cloning (see its documentation for what
82 that means).
83
84 Clone::Any - use whichever of the cloning methods is available.
85
87 <https://github.com/neilbowers/Clone-PP>
88
90 Developed by Matthew Simon Cavalletto at Evolution Softworks. More
91 free Perl software is available at "www.evoscript.org".
92
94 Copyright 2003 Matthew Simon Cavalletto. You may contact the author
95 directly at "evo@cpan.org" or "simonm@cavalletto.org".
96
97 Code initially derived from Ref.pm. Portions Copyright 1994 David Muir
98 Sharnoff.
99
100 Interface based by Clone by Ray Finch with contributions from
101 chocolateboy. Portions Copyright 2001 Ray Finch. Portions Copyright
102 2001 chocolateboy.
103
104 You may use, modify, and distribute this software under the same terms
105 as Perl.
106
107
108
109perl v5.30.1 2020-01-29 Clone::PP(3)