Re: Data::Dumper not indenting per perldocs



"u" == usenet <usenet@xxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.



Relevant Pages

  • Re: Bug Or Feature?
    ... you're trying to dereference undef?) ... If you try to use the value of a non-existent hash or array element as if it was a reference to a hash or array then an anonymous hash or array will be created for you. ...
    (perl.beginners)
  • Re: reference to a hash
    ... different versions of the same hash? ... No, when Perl passes a hash, it converts it into a list. ... foo(%h); ...
    (perl.beginners)
  • Re: Spliting values and reversing a hash
    ... elected to use strings rather than arrays since I think you said you ... my %h; #original hash ... 'Cookie Dough' => '3', ...
    (comp.lang.perl.misc)
  • Re: hash de-ref?
    ... Mai 2005 21.27 schrieb Matthew Sacks: ... > b) $loggedin is used as a hash key ... print Dumper %hash; ... Just read Paul Kraus's Confusion - we're not alone :-) ...
    (perl.beginners)
  • RE: Manipulating complex structures...
    ... Deal with hashes that are values of hashes as hash references, ... not sure whether the reference is to an array or a hash, ... #the number of spaces to indent. ... my $input = shift; ...
    (perl.beginners)