Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Pretty Printing with Data::Dumper

by Anonymous Monk
on Sep 09, 2005 at 03:35 UTC ( #490421=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

With this simple code:
perl -MData::Dumper -e '@arr = qw (a b c); print Dumper \@arr'

Is there a way to print the resulf of Data::Dumper in horizontal way? Like this:
$VAR1 = ['a','b','c'];
instead of
$VAR1 = [ 'a', 'b', 'c' ];
Since it makes life easier to compare the result. I hope this is a reasonable question. Thanks

Replies are listed 'Best First'.
Re: Pretty Printing with Data::Dumper
by Ovid (Cardinal) on Sep 09, 2005 at 03:58 UTC
    $Data::Dumper::Indent = 0;

    You also might want to try my Data::Dumper::Simple. I would only advise it for debugging (not production), but it has the nice effect of printing the variable name(s).


    New address of my CGI Course.

      While I'm not its author, I also recommend Data::Dumper::Simple. It's a very nice tool that saves you the mental hassle of wondering what the heck $VAR1 really was. It's also a drop-in replacement for Data::Dumper, meaning that you can just change use Data::Dumper to use Data::Dumper::Simple.

      Thanks for the tool Ovid. :)

Re: Pretty Printing with Data::Dumper
by pg (Canon) on Sep 09, 2005 at 04:13 UTC

    If you open up the source code, you see two places that determines this:

    $Indent = 2 unless defined $Indent;


    if ($Indent > 0) { $s->{xpad} = " "; $s->{sep} = "\n"; }

    $s->{sep} is where that line break comes from. If you change $s->{sep} to "". You got this:

    $VAR1 = [ 'a', 'b', 'c' ];

    The line break is gone, but you got whole bunch of blanks. Those blanks are caused by the Indent. So change Indent to 0. Now you get:

    $VAR1 = ['a','b','c'];

    That's what you asked for. But with setting Indent to 0, is it still needed to set $s->{"sep"} to ""? No, as that block will only be executed if Indent is greater than 0, which is no longer true. So the only thing you need to do is to set Indent to 0.

    Do you need to change the source code? No, You can set Indent from your program as Ovid has showed you.

    use Data::Dumper; use strict; use warnings; $Data::Dumper::Indent = 0; my $a = ["a", "b", "c"]; print Dumper($a);
Re: Pretty Printing with Data::Dumper
by demerphq (Chancellor) on Sep 09, 2005 at 11:34 UTC

    Depends what you want to do. And ill just chime in with yet another dumper to use... :-)

    D:\>perl -MData::Dumper -e "@arr = qw (a b c); print Data::Dumper->new +([\@arr])->Indent(0)->Dump" $VAR1 = ['a','b','c']; D:\>perl -MData::Dump::Streamer -e "@arr = qw (a b c); Dump(\@arr)->In +dent(0)->Out()" $ARRAY1 = [ 'a', 'b', 'c' ]; D:\>perl -MData::Dump::Streamer -e "@arr = (qw (a b),(qw(c)) x 4); Dum +p(\@arr)->Names('*arr')->Indent(0)->Out()" @arr = ( 'a', 'b', ( 'c' ) x 4 );

    And yet another.

    D:\>perl -MData::Dump=dump -e "@arr = (qw (a b),(qw(c)) x 4); print du +mp(\@arr)" ["a", "b", "c", "c", "c", "c"]

Re: Pretty Printing with Data::Dumper
by osunderdog (Deacon) on Sep 09, 2005 at 11:41 UTC

    Since the output of Data::Dumper is valid perl code, what about passing the output of Data::Dumper through Perl::Tidy?

    Hazah! I'm Employed!

Re: Pretty Printing with Data::Dumper
by halley (Prior) on Sep 09, 2005 at 12:11 UTC
    # Pretty() filters Data::Dumper output to join up short related lines. # This makes it a lot easier to read a typical hash structure. # Set $Data::Dumper::Fill like you would set $Text::Wrap::columns. # sub Pretty { my @src = split(/\n/, join('', @_)); my @dst = (); my $f = $Data::Dumper::Fill || 72; my $i = 0; while ($i <= $#src) { my $l = $src[$i]; if (not $l =~ /[\[\{\(]\s*$/) { push(@dst, $l); $i++; next; } my ($p) = ($l =~ /^(\s+)/); my $j = $i+1; while ($j <= $#src) { my $n = $src[$j]; my ($q) = ($n =~ /^(\s+)/); $n =~ s/^\s+/ /; if (length($l) + length($n) >= $f) { $l = $src[$i]; last; } $l .= $n; if ($q and $p and $q eq $p) { $i = $j; last; } $j++; } push(@dst, $l); $i++; } return join("\n", @dst) . "\n"; }
    I was going to add something like this to Data::Dumper so it would run if Indent==4, but the real Data::Dumper is now native code, and this kind of indent helper should be threaded into its main loop instead of as a post-process. Example output:

    [ e d @ h a l l e y . c c ]

Re: Pretty Printing with Data::Dumper
by planetscape (Chancellor) on Sep 09, 2005 at 18:24 UTC
Re: Pretty Printing with Data::Dumper
by ambrus (Abbot) on Sep 09, 2005 at 09:37 UTC

    When I think of dumping something, my image is not that I put it in a nice-looking arranged way. Try looking for a module that doesn't have the word dump in its name.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://490421]
Front-paged by monkfan
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2017-03-30 04:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.