Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Data::Dumper in a nutshell?

by munchie (Monk)
on Feb 08, 2002 at 00:00 UTC ( [id://143992]=perlquestion: print w/replies, xml ) Need Help??

munchie has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explain to me in plain english what Data::Dumper does? I'm confused by the readme that comes with the module. I'd also like to see some basic techniques and what they do.

> munchie, the number munchin newb

Replies are listed 'Best First'.
(Ovid) Re: Data::Dumper in a nutshell?
by Ovid (Cardinal) on Feb 08, 2002 at 00:42 UTC

    What Data::Dumper usually does is dump your data in a nice, legible format. There are actually quite a few uses for the module, but most programmers start using it with something like the following:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my $foo = 'test'; my @array = qw/ this is an array /; my %hash = ( one => 'une', two => 'deux', three => \&some_subroutine, four => { uh => 'oh', name => [qw/Publius Ovidius Naso/] } ); my $bar; print Dumper $foo, \@array; print Dumper \%hash; print Dumper $bar;

    Thus, you can use Data::Dumper to quickly 'dump' the contents of your variables. Yes, you can print a scalar, but printing out complex data structures (like the hash, above) can be tedious. Data::Dumper frees you from the hard work and makes debugging a breeze. If you do CGI work, try dumping the CGI object sometime. It's most informative :)

    Once you get used to using Data::Dumper, reread the documentation. It will be easier to understand.

    Cheers,
    Ovid

    Update: For the record, this is the output of the above code:

    $VAR1 = 'test'; $VAR2 = [ 'this', 'is', 'an', 'array' ]; $VAR1 = { 'one' => 'une', 'three' => sub { "DUMMY" }, 'two' => 'deux', 'four' => { 'uh' => 'oh', 'name' => [ 'Publius', 'Ovidius', 'Naso' ] } }; $VAR1 = undef;

    I don't see how munchie could have gotten the output listed below.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Data::Dumper in a nutshell?
by trs80 (Priest) on Feb 08, 2002 at 00:59 UTC
    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.
Re: Data::Dumper in a nutshell?
by Sweeper (Pilgrim) on Feb 08, 2002 at 06:41 UTC
    Another use of Data::Dumper is persistent storage. If you want to reuse your variable a few days later in another program, you apply Data::Dumper to the variable, and you print the result into a file, or you store it into a database.

    Then, in the next program, a few days later, you just do the file (or you eval the result of the database query), and your second program can use the value computed by the first program.

    There are other modules to get persistency: Storable, Freezethaw, but their output is not human-readable.

      For persistant storge, when a database is too much i use Storable.pm. Its such a funky bit of gear.

      you just do the file (or you eval the result of the database query), and your second program can use the value computed by the first program.

      ... or discover that some black hat has replaced your file with one containing system('rm -rf /')

      Data::Dumper is not a good method of doing persistence, because of the need to evaluate the stored file as actual Perl code using do or eval your code is only as secure as your file storage.

      If you must have a Human Readable persistent storage then you probaby want to look at Data::DumpXML or Data::Denter

      with the former you might want to consider my talk from last years yapc::Europe about a security implication of deserializing Perl objects into your code.

      /J\

Re: Data::Dumper in a nutshell?
by strat (Canon) on Feb 08, 2002 at 15:47 UTC
    A problem I had with using Data::Dumper for persistance was that it took so much RAM when used with a big datastructure because it created the whole outputstring in RAM. (Storable is a little bit better). In the moment I'm just working at a program that takes about 500-800MB RAM (under Win2k or Solaris 32 bit) with one main datastructure (LDAP-LDIF-like datastructure). If I want to print such a Datastructure with Data::Dumper to a file, there always comes an Out-of-memory-Error.

    But for small datastructures, it really is a great help!

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: Data::Dumper in a nutshell?
by munchie (Monk) on Feb 08, 2002 at 00:59 UTC
    Hmm, when I run it, it prints:
    $VAR1 = 'test'; $VAR2 = qw/ this is an array /; $VAR1 = ( one => 'une', two => 'deux', three => \&some_subroutine, four => { uh => 'oh', name => [qw/Publius Ovidius Naso/] } ); $VAR1 = undef;
    > munchie, the number munchin newb

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://143992]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-03-28 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found