Possible Duplicate:
What's the best way to make a deep copy of a data structure in Perl?
I'm apparently misunderstanding something about how hashrefs work in Perl, and am here looking to correct that.
I need to get a copy of a hashref that I can fiddle with without modifying the original. According to my research, copying a hashref is just as simple as using the equals operator:
my $hashref_copy = $hashref;
But as far as I can tell, all that does is make $hashref_copy
a pointer to the original object. Consider this toy bit of code:
my $hashref = {data => "fish"};
my $hashref_copy = $hashref;
$hashref_copy->{data} = "chips";
print "$hashref->{data}\n";
If $hashref_copy
were really an independent copy, I'd expect this code to print "fish". Instead, it prints "chips".
So either 1) I'm misunderstanding something or 2) Perl is broken. I'm quite certain it isn't #2, in spite of what my ego would have me think.
Where am I going wrong? And what do I need to do to make modifications to $hashref_copy
not show up in the original $hashref
?