September 2012 Archives

Deep Cloning

I've been using Storable's dclone for years, but there's a module on CPAN called Clone that is said to be much faster. However, it doesn't seem to work.


use strict;
use warnings;
use Clone qw(clone);
use Data::Printer;
use 5.010;

my $a = { blue => '#0000ff', one => 1 };

say "A";
p $a; 

my $b = clone($a);

say "B";
p $b; 

$b->{car} = 'Ford';

say "B";
p $b; 

say "A";
p $a; 

Given the above code, I'd expect $a to retain it's structure, but $b to have the added new element of car = Ford. That's what happens when I use dclone, but for some reason it doesn't happen using  clone. Am I doing something wrong? 

About JT Smith

user-pic My little part in the greater Perl world.