title: What's the best way to deep copy a hash of hashes 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>Before I start coding this myself and reinventing the wheel, how do you copy a hash of hashes without duplicating the hashrefs?</p>
  
  <p>I'm reading a hash of hash of hashes via <a href="http://search.cpan.org/perldoc/Config::General" rel="nofollow noreferrer">Config::General</a>. i.e., the data structure is:</p>
  
  <pre><code>my %config = ( group =&gt; { item1 =&gt; { foo =&gt; 'value',
                                       bar =&gt; 'value',
                                     },
                            item2 =&gt; { foo =&gt; 'value',
                                       bar =&gt; 'value',
                                     },
                            item3 =&gt; { foo =&gt; 'value',
                                       bar =&gt; 'value',
                                     },
                          },
               );
  </code></pre>
  
  <p>I then pull my group from the config by dereferencing it and change the contents at runtime prior to rewriting the config file:</p>
  
  <pre><code>my %group = %{$config{'group'}};
  </code></pre>
  
  <p>The problem is that I need to check to see if changes were made and make associated changes to the system's file structure. I can't do this by checking:</p>
  
  <pre><code>if ($group{'item1'}{'foo'} ne $config{'group'}{'item1'}{'foo'}) {
      ### Stuff!
  }
  </code></pre>
  
  <p>as <code>$group{'item1'}</code> and <code>$config{'group'}{'item1'}</code> are both the exact same hashref.</p>
  
  <p>Now while it should be trivial to simply re-parse the config file, and compare the parsed copy from the disk against the edited version just before saving to disk, I'm sure there's a way to to a nested dereference of a complex data structure, copying the contents of the hash refs and not simply copying the references themselves. A cursory examination on CPAN doesn't turn anything up. What am I missing?</p>
  
  <p><strong>Benchmark</strong></p>
  
  <p>Got my answer:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use Benchmark qw(:all) ;
  use Storable qw(dclone);
  use Clone qw(clone);
  
  my %config = ( group =&gt; { item1 =&gt; { foo =&gt; 'value',
                                       bar =&gt; 'value',
                                     },
                            item2 =&gt; { foo =&gt; 'value',
                                       bar =&gt; 'value',
                                     },
                            item3 =&gt; { foo =&gt; 'value',
                                       bar =&gt; 'value',
                                     },
                          },
               );
  
  my $ref = $config{'group'};
  
  timethese(100000, {
    'Clone' =&gt; sub { my %group = %{ clone $ref }},
    'Storable' =&gt; sub {  my %group = %{ dclone $ref }},
  });
  </code></pre>
  
  <p>results in:</p>
  
  <pre>
  Benchmark: timing 100000 iterations of Clone, Storable...
     Clone:  2 wallclock secs ( 2.26 usr +  0.01 sys =  2.27 CPU) @ 44052.86/s (n=100000)
  Storable:  5 wallclock secs ( 4.71 usr +  0.02 sys =  4.73 CPU) @ 21141.65/s (n=100000)
  </pre>