This is a well known problem with Data::Dumper. Whether it gets these things right is a matter of the order the values occur in. There are two problems involved in your example. The first is that Data::Dumper does a depth first traversal of the data structure. However in this case this is a trivial aspect. The second and in this case critical factor is that Data::Dumper does a single pass over the data structure and as such can't determine ahead of time that the thing that is referenced by $a->[0] is actually the scalar in $hash->{type}. The problem could be worked around by having Data::Dumper use the Array::RefElem module to make $hash->{$type} an alias to ${$array->[0]} and then it would all work out. Actually I have worked on a patch for Data::Dumper to do this, probably I'll pick it up again.

Suffice it to say that DD is not suitable for data structures that are self referential _and_ where those references involve references to scalars that are part of composite objects like hashes or arrays which it also has to dump, even in Purity mode. It simply cannot tell the difference between a reference to an element and a reference to an anoymous scalar, and doesnt know how to fix the problem once it has come up. If you want that then use Storable. Or you could have a whirl with the extremely alpha (and due for a total rewrite Real Soon Now) Data::BFDump. Unfortunately the later has problem on other platforms not to mention a few buggy areas. There is also Data::XDumper which I understand is quite good, but i've not had a look at it recently. AFAIK, YAML and Data::Dump are completely unsuited to this type of data structure, but they too may have moved on since I last time I tested them on cases like this. (YAML advertises that it cannot handle my $x; $x=\$x; at all, which Data::Dumper has no problem with under Purity. Data::Dump when I last checked could be induced to infinte recurse by the correctly chosen data structure)

The first record of this in Perl lore that I know of is by merlyn, and I mentioned it here a while back (a few times, do super search fo DD and my name, theres lots of stuff there :-) . Its been my on and off project for some time to produce a dumper that can handle these things better than DD, but lately its been a whole lot more off than on.
:-(

HTH


---
demerphq

Everybody remember the Gandhi quote?

    First they ignore you, then they laugh at you, then they fight you, then you win.

Gentlemen and ladies, this newest leaked memo from Microsoft confirms that we are advancing through GandhiCon Three.



In reply to Re: Data::Dumper scalar refs undef by demerphq
in thread Data::Dumper scalar refs undef by bsb

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.