Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Data::Dumper in reference to Hashes

by Anonymous Monk
on Jan 27, 2007 at 22:00 UTC ( #596908=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am reading the "Not Exactly a Hash Tutorial" and see the use of Data::Dumper. First of all, what is it and how is it used in reference to hashes? I read the CPAN document but it was filled with jargon and expressions that were not understandable to a beginner.

Replies are listed 'Best First'.
Re: Data::Dumper in reference to Hashes
by Joost (Canon) on Jan 27, 2007 at 22:08 UTC
    for people (like me) who had to look it up: here's a link to "Not Exactly a Hash Tutorial"

    Basically, Data::Dumper takes a perl datastructure and turns it into a string containing perl code that, when eval()ed (that is, run as code), returns an equivalent data structure. This is useful for many reasons, but a very simple use is to print out a data-structure to see if it matches what you think it is.


    use Data::Dumper qw(Dumper); # impor the Dumper() subroutine my %hash = ( a => 1, b => 2, c => 3 ); print Dumper(\%hash); # note the \ backslash; Dumper() takes referenc +es as arguments
    $VAR1 = { 'c' => 3, 'a' => 1, 'b' => 2 };
Re: Data::Dumper in reference to Hashes
by GrandFather (Sage) on Jan 27, 2007 at 22:35 UTC

    Data::Dumper (and the similar Data::Dump::Streamer) are used to look at the contents of variables - generally for diagnostic purposes. They can also be used to serialise a data structure so that it can be persisted to disk (saved and restored later). Data::Dump::Streamer does a better job of handling edge cases than Data::Dumper. Consider:

    use strict; use warnings; use Data::Dumper; use Data::Dump::Streamer; my %hash = ( this => ['one', 2, 3.1], that => undef, ); print Dumper (\%hash); Dump (\%hash);


    $VAR1 = { 'that' => undef, 'this' => [ 'one', 2, '3.1' ] }; $HASH1 = { that => undef, this => [ 'one', 2, 3.1 ] };

    DWIM is Perl's answer to Gödel
Re: Data::Dumper in reference to Hashes
by chargrill (Parson) on Jan 27, 2007 at 22:12 UTC

    I don't know, the first sentence in the Data::Dumper page seems pretty clear to me:

    Data::Dumper - stringified perl data structures, suitable for both printing and eval

    Stringified may not be a real word, but it means "turned into a string" (which according to the Oxford Dictionary, is "a linear sequence of characters, words, or other data").

    Perl Data Structure is pretty straightforward - it's a structured hunk of data in perl.

    Suitable for printing means you can print it.

    Suitable for eval means you can copy and paste it as valid perl code.

    Try a few things:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash = ( 'test' => 'one', 'next' => 'two' ); print Dumper \%hash; __OUTPUT__ $VAR1 = { 'next' => 'two', 'test' => 'one' };

    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: Data::Dumper in reference to Hashes
by Herkum (Parson) on Jan 28, 2007 at 05:59 UTC

    In Perl, a hash can hold a hierarchy of data and can be a mix of different data types. It can a simple key => value list or a or could be something really complicated.

    Because a hash can get complicated, you need a way of getting a picture of the data structure. Data::Dumper allows you to dump out this data structure. Now you can see the what and the how of the structure.

    Example: I was trying to get data from $hash_ref->{shoppingcart} and the data was in $hash_ref->{shopping_cart}.

    Note: All the '', {} and [] characters you see do mean something. If you are a beginner it can be hard to understand what is all those symbols mean. Trust me, they are very important and you will need to learn them.

Re: Data::Dumper in reference to Hashes
by brayshakes (Acolyte) on Jan 27, 2007 at 22:55 UTC
    So, could I use Data::Dumper to print the variables in question when errors are returned, or print the lines?
      You can use Data::Dumper to print out a stringified representation of data whenever you want, error condition or not. Here is an answer to another SoPW with an example of Data::Dumper being used to visualise the data that the program has constructed. Note that the output produced is valid Perl code which could be printed to a file, read back in by another script and evaled to re-create the data.

      I hope this clarifies things.



Re: Data::Dumper in reference to Hashes
by planetscape (Chancellor) on Jan 29, 2007 at 14:19 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://596908]
Approved by chargrill
[jdporter]: RonW - likely a reference to Matt Trout

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-08-18 20:28 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.