Re: Data::Dumper not indenting per perldocs
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Mon, 29 Jan 2007 22:20:10 -0500
"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
.
- References:
- Data::Dumper not indenting per perldocs
- From: usenet
- Data::Dumper not indenting per perldocs
- Prev by Date: Data::Dumper not indenting per perldocs
- Next by Date: Re: Tar on Windows XP
- Previous by thread: Data::Dumper not indenting per perldocs
- Next by thread: FAQ 1.3 Which version of Perl should I use?
- Index(es):
Relevant Pages
|