title: Copying a hashref in Perl
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/388187/whats-the-best-way-to-make-a-deep-copy-of-a-data-structure-in-perl">What&#39;s the best way to make a deep copy of a data structure in Perl?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm apparently misunderstanding something about how hashrefs work in Perl, and am here looking to correct that.</p>
  
  <p>I need to get a copy of a hashref that I can fiddle with without modifying the original.  According to <a href="http://www.misc-perl-info.com/perl-hashes.html#copyph" rel="noreferrer">my research</a>, copying a hashref is just as simple as using the equals operator:</p>
  
  <pre><code>my $hashref_copy = $hashref;</code></pre>
  
  <p>But as far as I can tell, all that does is make <code>$hashref_copy</code> a pointer to the original object.  Consider this toy bit of code:</p>
  
  <pre><code>
  my $hashref = {data => "fish"};
  
  my $hashref_copy = $hashref;
  $hashref_copy->{data} = "chips";
  
  print "$hashref->{data}\n";
  </code></pre>
  
  <p>If <code>$hashref_copy</code> were really an independent copy, I'd expect this code to print "fish".  Instead, it prints "chips".</p>
  
  <p>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.</p>
  
  <p>Where am I going wrong?  And what do I need to do to make modifications to <code>$hashref_copy</code> <i>not</i> show up in the original <code>$hashref</code>?</p>