From e1341b8b1b239ccca7f5ee7048dc6db7c6442e07 Mon Sep 17 00:00:00 2001 From: "Steven Haryanto (on PC)" Date: Wed, 8 Feb 2012 19:43:12 +0700 Subject: [PATCH 1/7] Configurable newline character --- lib/Data/Dump.pm | 32 +++++++++++++++++++------------- t/NL.t | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 t/NL.t diff --git a/lib/Data/Dump.pm b/lib/Data/Dump.pm index 088afa3..ee2363c 100644 --- a/lib/Data/Dump.pm +++ b/lib/Data/Dump.pm @@ -13,10 +13,11 @@ $VERSION = "1.21"; $DEBUG = 0; use overload (); -use vars qw(%seen %refcnt @dump @fixup %require $TRY_BASE64 @FILTERS $INDENT); +use vars qw(%seen %refcnt @dump @fixup %require $TRY_BASE64 $NL @FILTERS $INDENT); $TRY_BASE64 = 50 unless defined $TRY_BASE64; $INDENT = " " unless defined $INDENT; +$NL = "\n" unless defined $NL; sub dump { @@ -40,7 +41,7 @@ sub dump my $out = ""; if (%require) { for (sort keys %require) { - $out .= "require $_;\n"; + $out .= "require $_;$NL"; } } if (%refcnt) { @@ -48,12 +49,12 @@ sub dump for (@dump) { my $name = $_->[0]; if ($refcnt{$name}) { - $out .= "my \$$name = $_->[1];\n"; + $out .= "my \$$name = $_->[1];$NL"; undef $_->[1]; } } for (@fixup) { - $out .= "$_;\n"; + $out .= "$_;$NL"; } } @@ -66,28 +67,28 @@ sub dump $out .= ")" if $paren; if (%refcnt || %require) { - $out .= ";\n"; + $out .= ";$NL"; $out =~ s/^/$INDENT/gm; - $out = "do {\n$out}"; + $out = "do {$NL$out}"; } #use Data::Dumper; print Dumper(\%refcnt); #use Data::Dumper; print Dumper(\%seen); - print STDERR "$out\n" unless defined wantarray; + print STDERR "$out$NL" unless defined wantarray; $out; } *pp = \&dump; sub dd { - print dump(@_), "\n"; + print dump(@_), $NL; } sub ddx { my(undef, $file, $line) = caller; $file =~ s,.*[\\/],,; - my $out = "$file:$line: " . dump(@_) . "\n"; + my $out = "$file:$line: " . dump(@_) . $NL; $out =~ s/^/# /gm; print $out; } @@ -330,7 +331,7 @@ sub _dump my $klen_pad = 0; my $tmp = "@keys @vals"; if (length($tmp) > 60 || $tmp =~ /\n/ || $tied) { - $nl = "\n"; + $nl = $NL; # Determine what padding to add if ($kstat_max < 4) { @@ -381,7 +382,7 @@ sub _dump if ($class && $ref) { $out = "bless($out, " . quote($class) . ")"; } - if ($comment) { + if ($comment && $NL =~ /\n/) { $comment =~ s/^/# /gm; $comment .= "\n" unless $comment =~ /\n\z/; $comment =~ s/^#[ \t]+\n/\n/; @@ -472,8 +473,8 @@ sub format_list if ($comment || (@_ > $indent_lim && (length($tmp) > 60 || $tmp =~ /\n/))) { my @elem = @_; for (@elem) { s/^/$INDENT/gm; } - return "\n" . ($comment ? "$INDENT# $comment\n" : "") . - join(",\n", @elem, ""); + return $NL . ($comment && $NL =~ /\n/ ? "$INDENT# $comment\n" : "") . + join(",$NL", @elem, ""); } else { return join(", ", @_); } @@ -676,6 +677,11 @@ be valid Perl. How long must a binary string be before we try to use the base64 encoding for the dump output. The default is 50. Set it to 0 to disable base64 dumps. +=item $Data::Dump::NL + +Newline character. Defaults to "\n" but can be set to "" (for single-line dumps) +or multiple newlines (for double-spaced dumps). + =back diff --git a/t/NL.t b/t/NL.t new file mode 100644 index 0000000..e3366f6 --- /dev/null +++ b/t/NL.t @@ -0,0 +1,26 @@ +print "1..3\n"; + +use Data::Dump qw(dump); + +$a = { + a => "123456789012345678901234567890", + bb => "123456789012345678901234567890", +}; + +print +(dump($a) eq '{ + a => "123456789012345678901234567890", + bb => "123456789012345678901234567890", +}' ? "" : "not "), "ok 1\n"; + +$Data::Dump::NL = ""; +print +(dump($a) eq '{ a => "123456789012345678901234567890", bb => "123456789012345678901234567890" }' ? "" : "not "), "ok 2\n"; + +local $Data::Dump::NL = "\n\n"; +print +(dump($a) eq '{ + + a => "123456789012345678901234567890", + + bb => "123456789012345678901234567890", + +}' ? "" : "not "), "ok 3\n"; + From 40b83c6c18d7a98ef485f6fc781883d033e08b02 Mon Sep 17 00:00:00 2001 From: "Steven Haryanto (on PC)" Date: Wed, 15 Feb 2012 02:44:01 +0700 Subject: [PATCH 2/7] Remove mention of setting $NL to other than '' and "\n" [suggested by Gisle Aas] --- lib/Data/Dump.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Data/Dump.pm b/lib/Data/Dump.pm index ee2363c..45fd3ef 100644 --- a/lib/Data/Dump.pm +++ b/lib/Data/Dump.pm @@ -679,8 +679,8 @@ for the dump output. The default is 50. Set it to 0 to disable base64 dumps. =item $Data::Dump::NL -Newline character. Defaults to "\n" but can be set to "" (for single-line dumps) -or multiple newlines (for double-spaced dumps). +Newline character. Defaults to "\n" but can be set to "" (for single-line +dumps). =back From 019477e179ac2a4493f3d88e00df6c7a7257cb1e Mon Sep 17 00:00:00 2001 From: "Steven Haryanto (on PC)" Date: Wed, 15 Feb 2012 02:49:00 +0700 Subject: [PATCH 3/7] Add document that setting $NL to '' will cause comment to not show up [suggested by Gisle Aas] --- lib/Data/Dump.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Data/Dump.pm b/lib/Data/Dump.pm index 45fd3ef..e4a9b50 100644 --- a/lib/Data/Dump.pm +++ b/lib/Data/Dump.pm @@ -680,7 +680,7 @@ for the dump output. The default is 50. Set it to 0 to disable base64 dumps. =item $Data::Dump::NL Newline character. Defaults to "\n" but can be set to "" (for single-line -dumps). +dumps). Setting to "" will also cause comments to not show up. =back From 2c8e575a30b39fabc604469b31a79e373f5851f2 Mon Sep 17 00:00:00 2001 From: "Steven Haryanto (on PC)" Date: Wed, 29 Feb 2012 09:00:48 +0700 Subject: [PATCH 4/7] Use Test in test script NL.t --- t/NL.t | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/t/NL.t b/t/NL.t index e3366f6..033ed92 100644 --- a/t/NL.t +++ b/t/NL.t @@ -1,4 +1,7 @@ -print "1..3\n"; +use strict; +use Test; + +BEGIN { plan tests => 2 } use Data::Dump qw(dump); @@ -7,20 +10,8 @@ $a = { bb => "123456789012345678901234567890", }; -print +(dump($a) eq '{ - a => "123456789012345678901234567890", - bb => "123456789012345678901234567890", -}' ? "" : "not "), "ok 1\n"; +ok(dump($a), qq|{\n a => "123456789012345678901234567890",\n bb => "123456789012345678901234567890",\n}|); $Data::Dump::NL = ""; -print +(dump($a) eq '{ a => "123456789012345678901234567890", bb => "123456789012345678901234567890" }' ? "" : "not "), "ok 2\n"; - -local $Data::Dump::NL = "\n\n"; -print +(dump($a) eq '{ - - a => "123456789012345678901234567890", - - bb => "123456789012345678901234567890", - -}' ? "" : "not "), "ok 3\n"; +ok(dump($a), qq|{ a => "123456789012345678901234567890", bb => "123456789012345678901234567890" }|); From f8cd6833b2cdbadb713251509f776986f5796398 Mon Sep 17 00:00:00 2001 From: "Steven Haryanto (on PC)" Date: Wed, 29 Feb 2012 09:22:41 +0700 Subject: [PATCH 5/7] Add test: comment is removed when $NL set to '' --- t/NL.t | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/t/NL.t b/t/NL.t index 033ed92..05a4f98 100644 --- a/t/NL.t +++ b/t/NL.t @@ -1,17 +1,31 @@ use strict; use Test; -BEGIN { plan tests => 2 } +BEGIN { plan tests => 4 } use Data::Dump qw(dump); +use Data::Dump::Filtered qw(dump_filtered); -$a = { +# basic + +my $a = { a => "123456789012345678901234567890", bb => "123456789012345678901234567890", }; - ok(dump($a), qq|{\n a => "123456789012345678901234567890",\n bb => "123456789012345678901234567890",\n}|); +{ + local $Data::Dump::NL = ""; + ok(dump($a), qq|{ a => "123456789012345678901234567890", bb => "123456789012345678901234567890" }|); +} -$Data::Dump::NL = ""; -ok(dump($a), qq|{ a => "123456789012345678901234567890", bb => "123456789012345678901234567890" }|); +# comment +my $filter = sub { + my ($ctx, $oref) = @_; + return { comment=>"comment" }; +}; +ok(dump_filtered($a, $filter), qq|# comment\n{\n a => # comment\n "123456789012345678901234567890",\n bb => # comment\n "123456789012345678901234567890",\n}|); +{ + local $Data::Dump::NL = ""; + ok(dump_filtered($a, $filter), qq|{ a => "123456789012345678901234567890", bb => "123456789012345678901234567890" }|); +} From d5dc7006f1c524ef8b00e6f0427e866f5987c69e Mon Sep 17 00:00:00 2001 From: "Steven Haryanto (on PC)" Date: Wed, 29 Feb 2012 10:00:31 +0700 Subject: [PATCH 6/7] Remove comment in tied dump when $NL is '' --- lib/Data/Dump.pm | 2 +- t/tied.t | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/Data/Dump.pm b/lib/Data/Dump.pm index e4a9b50..5575dac 100644 --- a/lib/Data/Dump.pm +++ b/lib/Data/Dump.pm @@ -355,7 +355,7 @@ sub _dump } } $out = "{$nl"; - $out .= "$INDENT# $tied$nl" if $tied; + $out .= "$INDENT# $tied$nl" if $tied && $NL =~ /\n/; while (@keys) { my $key = shift @keys; my $val = shift @vals; diff --git a/t/tied.t b/t/tied.t index 620bdf3..bac288a 100644 --- a/t/tied.t +++ b/t/tied.t @@ -4,7 +4,7 @@ use strict; use Test qw(plan ok); use Data::Dump qw(dump); -plan tests => 4; +plan tests => 6; { package MyTie; @@ -54,6 +54,11 @@ ok(nl(dump(\%hash)), < "vd", } EOT +{ + local $Data::Dump::NL = " "; + local $Data::Dump::INDENT = ""; + ok(dump(\%hash), q|{ a => "va", b => "vb", c => "vc", d => "vd", }|); +} ok(nl(dump(\@array)), < Date: Wed, 29 Feb 2012 10:05:03 +0700 Subject: [PATCH 7/7] Update documentation in regard to setting $NL dan $INDENT --- lib/Data/Dump.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Data/Dump.pm b/lib/Data/Dump.pm index 5575dac..ea071b7 100644 --- a/lib/Data/Dump.pm +++ b/lib/Data/Dump.pm @@ -679,8 +679,12 @@ for the dump output. The default is 50. Set it to 0 to disable base64 dumps. =item $Data::Dump::NL -Newline character. Defaults to "\n" but can be set to "" (for single-line -dumps). Setting to "" will also cause comments to not show up. +Newline character. Defaults to "\n" but can be set to " " (a single space) for +single-line dumps. Removing newline from this variable will also cause comments +to not show up. + +If you want to generate nicer single-line dumps, also set $Data::Dump::INDENT to +"". =back