1Clone(3) User Contributed Perl Documentation Clone(3)
2
3
4
6 Clone - recursively copy Perl datatypes
7
9 use Clone 'clone';
10
11 my $data = {
12 set => [ 1 .. 50 ],
13 foo => {
14 answer => 42,
15 object => SomeObject->new,
16 },
17 };
18
19 my $cloned_data = clone($data);
20
21 $cloned_data->{foo}{answer} = 1;
22 print $cloned_data->{foo}{answer}; # '1'
23 print $data->{foo}{answer}; # '42'
24
25 You can also add it to your class:
26
27 package Foo;
28 use parent 'Clone';
29 sub new { bless {}, shift }
30
31 package main;
32
33 my $obj = Foo->new;
34 my $copy = $obj->clone;
35
37 This module provides a "clone()" method which makes recursive copies of
38 nested hash, array, scalar and reference types, including tied
39 variables and objects.
40
41 "clone()" takes a scalar argument and duplicates it. To duplicate
42 lists, arrays or hashes, pass them in by reference, e.g.
43
44 my $copy = clone (\@array);
45
46 # or
47
48 my %copy = %{ clone (\%hash) };
49
51 Storable's "dclone()" is a flexible solution for cloning variables,
52 albeit slower for average-sized data structures. Simple and naive
53 benchmarks show that Clone is faster for data structures with 3 or
54 fewer levels, while "dclone()" can be faster for structures 4 or more
55 levels deep.
56
58 Copyright 2001-2019 Ray Finch. All Rights Reserved.
59
60 This module is free software; you can redistribute it and/or modify it
61 under the same terms as Perl itself.
62
64 Ray Finch "<rdf@cpan.org>"
65
66 Breno G. de Oliveira "<garu@cpan.org>" and Florian Ragwitz
67 "<rafl@debian.org>" perform routine maintenance releases since 2012.
68
69
70
71perl v5.32.1 2021-01-27 Clone(3)