Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Data::Dumper not indenting per perldocs (http://www.velocityreviews.com/forums/t901643-data-dumper-not-indenting-per-perldocs.html)

usenet@DavidFilmer.com 01-30-2007 02:35 AM

Data::Dumper not indenting per perldocs
 
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)


Uri Guttman 01-30-2007 03:20 AM

Re: Data::Dumper not indenting per perldocs
 
>>>>> "u" == usenet <usenet@DavidFilmer.com> writes:

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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org


All times are GMT. The time now is 08:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57