Data::Dumper not indenting per perldocs

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Jan 30, 2007.

  1. Guest

    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)
     
    , Jan 30, 2007
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "u" == usenet <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 30, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wana

    best way to search perldocs

    wana, Sep 27, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    103
    Tad McClellan
    Sep 28, 2004
  2. Replies:
    0
    Views:
    94
  3. Replies:
    33
    Views:
    481
    Tad McClellan
    Feb 26, 2006
  4. Replies:
    16
    Views:
    192
    Ted Zlatanov
    Jun 5, 2006
  5. Replies:
    4
    Views:
    254
    Paul Lalli
    Aug 1, 2007
Loading...

Share This Page