Others have pointed out how to make the behaviour conform to your expectations; but I wanted to point out that ‘fix’ may not be the right term, since Data::Dumper is correctly indicating to you that your $hashref->{test1} and $hashref->{test2} are not just 2 references to similar hashes, but literally the same hashref. You can demonstrate this to yourself:

$hashref->{test1}{temp}[0] = 456; print $hashref->{test2}{temp}[0]; # => 456
This is the classical problem of deep versus shallow copies.

The reason that you don't get the desired behaviour when you eval the output of Dumper is that the eval happens “all at once”—when the ‘inner’ reference to $VAR1->{test1}{temp}[0] is being evaluated, $VAR1 doesn't yet have the value that it will have when the eval completes, so all you're doing is auto-vivifying it as a reference to a hash-of-hashes-of-arrays. Accessing the 0th entry of that innermost array makes sure that it exists, but doesn't assign it a value, so it comes out undef. The documentation for Data::Dumper warns of this (but, in my opinion, not very clearly), and mentions the solution that ikegami already gave:

The default output of self-referential structures can be evaled, but the nested references to $VARn will be undefined, since a recursive structure cannot be constructed using one Perl statement. You should set the Purity flag to 1 to get additional statements that will correctly fill in these references. Moreover, if evaled when strictures are in effect, you need to ensure that any variables it accesses are previously declared.


In reply to Re: Data Dumper Question by JadeNB
in thread Data Dumper Question by clintonm9

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.