Data::Dumper in a nutshell for debugging/learning.

In some ways I see Data::Dumper as a debugging tool, yet it has many uses beyond.

I most frequently use the exported Dumper function. You feed it a data structure (array,hash,etc) or a complex data structure (hashes and arrays multiple levels deep in any combination, objects etc.) and it will return the complete contents to you. This makes object debugging much easier for me because often times the issue is in the fact that an attribute of object didn't get set correctly. With Dumper you can do this:
use Data::Dumper; $hashref = { key1 => { more => 'less' }, key2 => { help => 'stop' }, key3 => { wow => [ 'list_element_0', '1' , '2' ] }, }; print Dumper($hashref);

That will print what you already know since you created the structure, but what if you are getting back something that you are not sure of the contents on? This is where it comes in handy for both debugging and sometimes better understanding the contents in an object.
Like so ( i am using code from an earlier post by artist ):
use Data::Dumper; use Parse::RecDescent; my $grammer = q{ record: id name id: m[(.{6})] { print "id => $1 " if $1 } name: m[(.*)] { print "name => $1 "; } }; my $parser = Parse::RecDescent->new($grammer); print Dumper($parser);

That will show you the complete data structure of a newly created object.

I feel that the docs for Data::Dumper don't really make it clear how you would apply this module for someone that isn't doing full time development. Once you see how it is applied it will most likely become a very valuable debug utility for anyone working with hidden (dynamic) data structures, which should be most everyone I hope.

In reply to Re: Data::Dumper in a nutshell? by trs80
in thread Data::Dumper in a nutshell? by munchie

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.