Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2023-03-30 13:49 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.