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

Hi monks , I am new to Dumper, My question is , Is there away to format the output of the Dumper? I get somthing like :
$VAR1 = { 'Blue' => 1, 'COW' => 3 };
I need the dumper to print somthing like
Blue 1 COW 3
Is there a way to do it without having to re format the output ? thanks

Replies are listed 'Best First'.
Re: Dump using Dumper
by jdporter (Chancellor) on Mar 23, 2004 at 05:36 UTC

    No, that's not what Dumper is for. The output of Dumper is specifically designed to be eval-uable as perl code.

    But it's easy to get output like what you want. For example, you could write a function like the following:

    sub dump_hash(\%) { my $hr = shift; for ( sort keys %$hr ) { print "$_ $hr->{$_}\n"; } } # use it like so: my %hash = ( ...whatever... ); dump_hash(%hash);

    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Dump using Dumper
by DamnDirtyApe (Curate) on Mar 23, 2004 at 05:34 UTC

    I don't think Data::Dumper will do this for you, but this oughta do the trick:

    my $hashref = { 'Blue' => 1, 'COW' => 3 }; print "$_\t$hashref->{$_}\n" for keys %$hashref;

    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Dump using Dumper
by davido (Cardinal) on Mar 23, 2004 at 07:12 UTC
    I couldn't leave well enough alone; not satisfied with my previous response. Here is a more complete solution:

    use strict; use warnings; use Data::Dumper; my %hash = qw/Blue 1 Cow 3/; sub PrettyDump { local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 1; local $Data::Dumper::Quotekeys = 0; my $dump = Dumper(@_); $dump =~ s/[{}]\s+|[',]|=>|^\s+//mg; return $dump; } print PrettyDump(\%hash); __OUTPUT__ Blue 1 COW 3

    The substitution regex is pretty lame in this solution. And it's only designed to work with non-complex datastructures. A more robust solution would be a lot more careful to deal with things like commas, quotes, etc. And though it's probably easier to write your own hash-dumper than to implement a solution that strips away unwanted stuff from the output of Data::Dumper, I thought it was fun to tinker with this solution anyway.



Re: Dump using Dumper
by davido (Cardinal) on Mar 23, 2004 at 05:34 UTC
    You can manipulate the formatting of Data::Dumper's output. It may not be exactly the format you're wanting though, but it may be closer than the default is. The POD for that module explains how to:

    • Control the style of indentation.
    • Specify a string to be prefixed to every line of output.
    • Change the prefix of variable names in output.
    • Adjust output to a more "terse" format (maybe close to what you want).

    Update: Here is an example of how close you can get with Data::Dumper:

    use strict; use warnings; use Data::Dumper; my %hash = qw/this 1 that 2 those 3 them 4/; $Data::Dumper::Terse++; $Data::Dumper::Quotekeys = 0; $Data::Dumper::Indent = 1; print Dumper \%hash; __OUTPUT__ { those => '3', them => '4', that => '2', this => '1' }

    Without the formatting overrides, here is what you would have gotten:

    $VAR1 = { 'those' => '3', 'them' => '4', 'that' => '2', 'this' => '1' };


Re: Dump using Dumper
by zentara (Archbishop) on Mar 23, 2004 at 16:19 UTC
    It's alot easier using YAML.
    #!/usr/bin/perl use YAML; my %hash = qw/Blue 1 Cow 3/; print Dump(\%hash);
    --- #YAML:1.0 Blue: 1 Cow: 3
    The secret is to put a \ in front of %hash. :-)

    I'm not really a human, but I play one on earth. flash japh