Parimala@123 has asked for the wisdom of the Perl Monks concerning the following question:

Iam in the process of understanding this script. I just now started leaning perl language, But confusion with Data::Dumper and print Dumper modules.Please explain this code.
#!/usr/local/bin/perl -w use strict; use Data::Dumper; use Net::FTP; use lib '/export/home/pvuser/lib'; use Eirteic; # # my $er = Eirteic->start; # # Initialise Library # if( @ARGV > 0 ) { # Arg we testing or for real ? $er->{test} = 1; $er->{debug} = 1; } $er->log( "Getting Customer Groups" ); $er->getCustomerGroups(); if( $er->{debug} > 1 ) { print "Usage:\n"; print Dumper %usage; print "Latency:\n"; print Dumper %latency; print "Jitter:\n"; print Dumper %jitter; print "Site:\n"; print Dumper %site; }

Replies are listed 'Best First'.
Re: Use of Data::Dumper
by moritz (Cardinal) on Jan 15, 2009 at 09:24 UTC
    Please read Writeup Formatting Tips and re-format your node to be readable (hint: use <code>...</code> tags).

    The Data::Dumper for hashes is much more readable when you dump a reference to a hash (for example \%site) instead of the hash itself.

Re: Use of Data::Dumper
by johngg (Canon) on Jan 15, 2009 at 11:46 UTC

    The Data::Dumper->Dump() (and ->Dumpxs(), identical functionality but implemented in C so faster) methods can provide more readable output as they allow you to name your variables and allow you to show that a hash really is a hash, not a hash reference. Here are a couple of examples showing the differences as applied to a hash and a hash reference.

    use strict; use warnings; use Data::Dumper; my %hash = ( name => q{Fred}, age => 27, ); print q{-} x 30, qq{\n}; print Dumper( %hash ); print q{-} x 30, qq{\n}; print Dumper( \ %hash ); print q{-} x 30, qq{\n}; print Data::Dumper->Dump( [ \ %hash ], [ qw{ *hash } ] ); print q{=} x 30, qq{\n}; my $hashRef = { name => q{Mary}, age => 25, }; print Dumper( $hashRef ); print q{-} x 30, qq{\n}; print Data::Dumper->Dump( [ $hashRef ], [ qw{ hashRef } ] ); print q{-} x 30, qq{\n};

    Here is the output.

    ------------------------------ $VAR1 = 'name'; $VAR2 = 'Fred'; $VAR3 = 'age'; $VAR4 = 27; ------------------------------ $VAR1 = { 'name' => 'Fred', 'age' => 27 }; ------------------------------ %hash = ( 'name' => 'Fred', 'age' => 27 ); ============================== $VAR1 = { 'name' => 'Mary', 'age' => 25 }; ------------------------------ $hashRef = { 'name' => 'Mary', 'age' => 25 }; ------------------------------

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Use of Data::Dumper
by toolic (Bishop) on Jan 15, 2009 at 13:36 UTC
    One common usage of the Dumper function from the core Data::Dumper module is to quickly display the contents of hash variables for debugging purposes, as your code seems to indicate. For example, if your %usage is a simple hash, it is much simpler to view its contents using:
    print Dumper(\%usage);

    than to use a loop:

    for (keys %usage) { print "$_ = $usage{$_}\n" }

    The real power is if you have a more complicated data structure, such as a hash-of-arrays, a hash-of-hashes, or any arbitrarily deep structure (see perldsc). You don't need to know what the structure is: Dumper figures it out for you. There is no need for nesting loops just to print out the contents.

    This is just one usage of the module (the one that your code uses). There are many more purposes and features which you can read about in Data::Dumper.

Re: Use of Data::Dumper
by ww (Archbishop) on Jan 15, 2009 at 11:49 UTC

    Perhaps "modules" (emphasis supplied) is merely a typo, but if not, then this clarification:

    For your purposes, Dumper is a function within Data::Dumper; not a different module.

    See the doc moritz cited, Data::Dumper.

Re: Use of Data::Dumper
by targetsmart (Curate) on Jan 15, 2009 at 13:04 UTC
    If you are very new to perl, I would suggest you to read the book 'learning perl by Randal L Schwartz and Tom Christiansen'.
    because without the knowledge of basics, you couldn't write /understand Perl programs.
Re: Use of Data::Dumper
by bradcathey (Prior) on Jan 15, 2009 at 14:07 UTC

    First of all, welcome to the Monastery. Hang around her long enough and you'll learn how to write Perl code the right way. It's helped me.

    Two tips: I use Data::Dumper for checking variable values in Web environments, often live sites in emergencies. So, 1) I use:

    print "Content-type: html/text\n\n";

    to show the values, but still print the page to the browser. If I really want to stay below the radar, I open up a file on the server and dump the values there.

    Nothing revolutionary, but nice to know when starting out with the module.

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot