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 named clone_self, it gets called and the result returned without further processing.
  • clone_init β€” alternately, if an object provides a method named clone_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.t are 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 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