Data::Dumper not indenting per perldocs

U

usenet

According to perldoc Data::Dumper:

$Data::Dumper::Indent or $OBJ->Indent([NEWVAL])
Controls the style of indentation. It can be set to 0,
1, 2 or 3.... Style 2 (the default) outputs a very
readable form which takes into account the length of
hash keys (so the hash value lines up).

However, when I run this program:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Indent = 2; #no diff if I comment this out

my %hash = qw{ this_is_a_long_hash_key long
short_key, short };

print Dumper \%hash;

__END__

I do not observe that the hash values line up:

$VAR1 = {
'this_is_a_long_hash_key' => 'long',
'short_key,' => 'short'
};

Why are the values not aligned? Thanks!

(Perl 5.8.4 on AIX 5.3 in a plain tty)
 
U

Uri Guttman

u> According to perldoc Data::Dumper:
u> $Data::Dumper::Indent or $OBJ->Indent([NEWVAL])
u> Controls the style of indentation. It can be set to 0,
u> 1, 2 or 3.... Style 2 (the default) outputs a very
u> readable form which takes into account the length of
u> hash keys (so the hash value lines up).

u> use Data::Dumper;
u> $Data::Dumper::Indent = 2; #no diff if I comment this out

since style 2 is the default, of course there won't be any difference.

u> my %hash = qw{ this_is_a_long_hash_key long
u> short_key, short };

u> print Dumper \%hash;

u> I do not observe that the hash values line up:

u> $VAR1 = {
u> 'this_is_a_long_hash_key' => 'long',
u> 'short_key,' => 'short'
u> };

u> Why are the values not aligned? Thanks!

i would call that a doc bug. it seems to only want to align the keys and
then use a fixed spacing after the =>. i tried this variant and it shows
that more clearly.

my %hash = (
this_is_a_long_hash_key => 'long',
subhash => { x => 2, anotherverylongkey => 3 },
short_key => 'short' ) ;

print Dumper \%hash;

$VAR1 = {
'this_is_a_long_hash_key' => 'long',
'short_key' => 'short',
'subhash' => {
'anotherverylongkey' => 3,
'x' => 2
}
};

This is perl, v5.8.6 built for sun4-solaris

when it is set to 1 i get this:

$VAR1 = {
'this_is_a_long_hash_key' => 'long',
'short_key' => 'short',
'subhash' => {
'anotherverylongkey' => 3,
'x' => 2
}
};

so you can see that with indent = 2 that the subhash is indented beyond
the 'subhash' key. i think that is what they meant by hash values -
deeper hashes. scalars just get printed with a space after =>.

when i go deeper this pattern keeps up:

$VAR1 = {
'this_is_a_long_hash_key' => 'long',
'short_key' => 'short',
'subhash' => {
'anotherverylongkey' => {
'y' => 4
},
'x' => 2
}
};

it is accounting for key length but only when deeper nesting happens. so
i would say the docs could be clearer on this point.

when i want fancier output than data::dumper i will roll my own
sometimes. or you can write a postprocessing filter to clean up the
indent to your taste.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top