Clone::PP(3pm)
β¨ Clone::PP β¨
okay bestie, listen UP π this is the User Contributed Perl Documentation page for Clone::PP, and it's literally the module that makes deep copies of your Perl data structures. no cap. it's giving "I duplicate your whole vibe, not just the surface level" π¦
πΈ NAME πΈ
Clone::PP β Recursively copy Perl datatypes π
π SYNOPSIS π
use Clone::PP qw(clone);
$item = { 'foo' => 'bar', 'move' => [ 'zig', 'zag' ] };
$copy = clone( $item );
$item = [ 'alpha', 'beta', { 'gamma' => 'vlissides' } ];
$copy = clone( $item );
$item = Foo->new();
$copy = clone( $item );
Or, if you want it as an object method (main character energy fr fr):
require Clone::PP;
push @Foo::ISA, 'Clone::PP';
$item = Foo->new();
$copy = $item->clone();
periodt. that's it. that's the whole demo β¨
π DESCRIPTION π
This module hands you a general-purpose clone function so you can make deep copies of Perl data structures. It literally calls itself recursively to copy nested hashes, arrays, scalars and reference types β and yes, that includes tied variables and objects. it's so thorough it's lowkey obsessed with itself π
The clone() function takes a scalar argument to copy. Wanna duplicate arrays or hashes? pass them in by reference, bestie:
my $copy = clone(\@array); my @copy = @{ clone(\@array) };
my $copy = clone(\%hash); my %copy = %{ clone(\%hash) };
okay and here's the β¨extraβ¨: clone() also accepts an optional second parameter that limits the depth of the copy. so:
- a limit of
0β clone returns the same value you supplied - a limit of
1β a shallow copy is constructed - a limit of
2β two layers of copying are done - and so on, you get the pattern π
my $shallow_copy = clone( $item, 1 );
Now, so that objects can intervene in how they get copied, clone() checks for a couple of optional methods π
clone_selfβ if an object provides a method namedclone_self, it gets called and the result returned without further processing.clone_initβ alternately, if an object provides a method namedclone_init, it is called on the copied object before that object is returned.
π BUGS π
ngl, there are some things that just do NOT slay:
- Some data types β globs, regexes, and code refs β are always copied shallowly.
- References to hash elements are not properly duplicated. (This is literally why two tests in
t/dclone.tare marked"todo".) For example, this following test should succeed but does not:
my $hash = { foo => 1 };
$hash->{bar} = \{ $hash->{foo} };
my $copy = clone( \%hash );
$hash->{foo} = 2;
$copy->{foo} = 2;
ok( $hash->{bar} == $copy->{bar} );
To report bugs via the CPAN web tracking system, go to "http://rt.cpan.org/NoAuth/Bugs.html?Dist=Clone-PP" or send mail to "Dist=Clone-PP#rt.cpan.org", replacing "#" with "@". π«Ά
π SEE ALSO π
- Clone β a baseclass which provides a
"clone()"method. - MooseX::Clone β find-grained cloning for Moose objects.
- The
"dclone()"function in Storable. - Data::Clone β polymorphic data cloning (see its documentation for what that means).
- Clone::Any β use whichever of the cloning methods is available.
π¦ REPOSITORY π¦
<https://github.com/neilbowers/Clone-PP>
π AUTHOR AND CREDITS π
Developed by Matthew Simon Cavalletto at Evolution Softworks. More free Perl software is available at "www.evoscript.org". π
β COPYRIGHT AND LICENSE β
Copyright 2003 Matthew Simon Cavalletto. You may contact the author directly at "evo@cpan.org" or "simonm@cavalletto.org".
Code initially derived from Ref.pm. Portions Copyright 1994 David Muir Sharnoff.
Interface based by Clone by Ray Finch with contributions from chocolateboy. Portions Copyright 2001 Ray Finch. Portions Copyright 2001 chocolateboy.
You may use, modify, and distribute this software under the same terms as Perl. β¨
and that's the whole page, babe π this was perl v5.36.0, dated 2022-10-13, for Clone::PP(3pm) β go forth and deep-copy responsibly ππΈπ¦π
π¦ where this page comes from
- ubuntu noble: libclone-pp-perl 1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclone-pp-perl 1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclone-pp-perl 1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms