Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found