CONTENTS

NAME

Clone - recursively copy Perl datatypes

Build Status CPAN version

SYNOPSIS

use Clone 'clone';

my $data = {
   set => [ 1 .. 50 ],
   foo => {
       answer => 42,
       object => SomeObject->new,
   },
};

my $cloned_data = clone($data);

$cloned_data->{foo}{answer} = 1;
print $cloned_data->{foo}{answer};  # '1'
print $data->{foo}{answer};         # '42'

You can also add it to your class:

package Foo;
use parent 'Clone';
sub new { bless {}, shift }

package main;

my $obj = Foo->new;
my $copy = $obj->clone;

DESCRIPTION

This module provides a clone() method which makes recursive copies of nested hash, array, scalar and reference types, including tied variables and objects.

clone() takes a scalar argument and duplicates it. To duplicate lists, arrays or hashes, pass them in by reference, e.g.

my $copy = clone (\@array);

# or

my %copy = %{ clone (\%hash) };

EXAMPLES

Cloning Blessed Objects

package Person;
sub new {
    my ($class, $name) = @_;
    bless { name => $name, friends => [] }, $class;
}

package main;
use Clone 'clone';

my $person = Person->new('Alice');
my $clone = clone($person);

# $clone is a separate object with the same data
push @{$person->{friends}}, 'Bob';
print scalar @{$clone->{friends}};  # 0

Handling Circular References

Clone properly handles circular references, preventing infinite loops:

my $a = { name => 'A' };
my $b = { name => 'B', ref => $a };
$a->{ref} = $b;  # circular reference

my $clone = clone($a);
# Circular structure is preserved in the clone

Cloning Weakened References

use Scalar::Util 'weaken';

my $obj = { data => 'important' };
my $container = { strong => $obj, weak => $obj };
weaken($container->{weak});

my $clone = clone($container);
# Both strong and weak references are preserved correctly

Cloning Tied Variables

use Tie::Hash;
tie my %hash, 'Tie::StdHash';
%hash = (a => 1, b => 2);

my $clone = clone(\%hash);
# The tied behavior is preserved in the clone

LIMITATIONS

PERFORMANCE

Clone is implemented in C using Perl's XS interface, making it very fast for most use cases.

Benchmarking your specific use case is recommended for performance-critical applications.

CAVEATS

SEE ALSO

Storable's dclone() is a flexible solution for cloning variables, albeit slower for average-sized data structures. Simple and naive benchmarks show that Clone is faster for data structures with 3 or fewer levels, while dclone() can be faster for structures 4 or more levels deep.

Other modules that may be of interest:

Clone::PP - Pure Perl implementation of Clone

Scalar::Util - For weaken() and other scalar utilities

Data::Dumper - For debugging and inspecting data structures

SUPPORT

COPYRIGHT

Copyright 2001-2026 Ray Finch. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Ray Finch <rdf@cpan.org>

Breno G. de Oliveira <garu@cpan.org>, Nicolas Rochelemagne <atoomic@cpan.org> and Florian Ragwitz <rafl@debian.org> perform routine maintenance releases since 2012.