From: Aaron Crane Date: Mon, 23 May 2016 15:31:23 +0000 (+0100) Subject: Use Trailingcomma option when available (RT#114609) X-Git-Tag: v2.023~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4aab360553cca9fc8b7be05bd6134928cf016ce3;p=p5sagit%2FData-Dumper-Concise.git Use Trailingcomma option when available (RT#114609) --- diff --git a/Changes b/Changes index cab1163..4cf3b08 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Data-Dumper-Concise - convert distribution from Module::Install to Distar; fixes RT#120856 - every module has a $VERSION now (RT#116427) + - add support for Trailingcomma option (RT#114609, Aaron Crane) 2.022 Mar 21 2014 - Fix repo metadata diff --git a/lib/Data/Dumper/Concise.pm b/lib/Data/Dumper/Concise.pm index 09ec2f7..543f8ce 100644 --- a/lib/Data/Dumper/Concise.pm +++ b/lib/Data/Dumper/Concise.pm @@ -13,6 +13,7 @@ BEGIN { @ISA = qw(Exporter) } sub DumperObject { my $dd = Data::Dumper->new([]); + $dd->Trailingcomma(1) if $dd->can('Trailingcomma'); $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1); } @@ -43,6 +44,7 @@ is equivalent to: local $Data::Dumper::Deparse = 1; local $Data::Dumper::Quotekeys = 0; local $Data::Dumper::Sortkeys = 1; + local $Data::Dumper::Trailingcomma = 1; warn Dumper($var); } @@ -58,7 +60,7 @@ Data::Dumper::Concise will give you: use warnings; use strict 'refs'; 'fleem'; - } + }, } instead of the default Data::Dumper output: @@ -71,6 +73,11 @@ instead of the default Data::Dumper output: (note the tab indentation, oh joy ...) +(The trailing comma on the last element of an array or hash is enabled by a new +feature in Data::Dumper version 2.159, which was first released in Perl 5.24. +Using Data::Dumper::Concise with an older version of Data::Dumper will still +work, but you won't get those commas.) + If you need to get the underlying L object just call C. Also try out C which takes a C as the first argument to diff --git a/t/concise.t b/t/concise.t index 20e3512..5836b8e 100644 --- a/t/concise.t +++ b/t/concise.t @@ -11,6 +11,7 @@ my $dd = Data::Dumper->new([]) ->Deparse(1) ->Quotekeys(0) ->Sortkeys(1); +$dd->Trailingcomma(1) if $dd->can('Trailingcomma'); foreach my $to_dump ( [ { foo => "bar\nbaz", quux => sub { "fleem" } } ], @@ -26,6 +27,8 @@ foreach my $to_dump ( local $Data::Dumper::Deparse = 1; local $Data::Dumper::Quotekeys = 0; local $Data::Dumper::Sortkeys = 1; + no warnings 'once'; # in case Trailingcomma option is unknown in this DD + local $Data::Dumper::Trailingcomma = 1; Data::Dumper::Dumper(@$to_dump); }; @@ -36,4 +39,7 @@ foreach my $to_dump ( my $out = DumperF { "arr: $_[0] str: $_[1]" } [qw(wut HALP)], "gnarl"; -is($out, qq{arr: [\n "wut",\n "HALP"\n]\n str: "gnarl"\n}, 'DumperF works!'); +like($out, qr{^arr: \[\n "wut",\n "HALP",?\n\]\n str: "gnarl"\n\z}, 'DumperF works!'); + +like(Dumper([1..3]), qr/,\s*]\s*$/, 'trailing comma enabled') + if $dd->can('Trailingcomma'); diff --git a/t/sugar.t b/t/sugar.t index 2ccc009..c5abd70 100644 --- a/t/sugar.t +++ b/t/sugar.t @@ -40,7 +40,7 @@ DWARN: { DWARN_CODEREF: { my $foo = ['warn', 'friend']->$Dwarn; - is $warned_string,qq{[\n "warn",\n "friend"\n]\n}, 'Dwarn warns lists'; + like $warned_string,qr{^\[\n "warn",\n "friend",?\n\]\n\z}, 'Dwarn warns lists'; ok eq_array($foo, ['warn','friend']), 'Dwarn passes lists through correctly'; } @@ -48,7 +48,7 @@ DWARN_CODEREF: { DWARNF: { my @foo = DwarnF { "arr: $_[0] str: $_[1]" } [qw(wut HALP)], "gnarl"; - is($warned_string, qq{arr: [\n "wut",\n "HALP"\n]\n str: "gnarl"\n}, 'DumperF works!'); + like($warned_string, qr{^arr: \[\n "wut",\n "HALP",?\n\]\n str: "gnarl"\n\z}, 'DumperF works!'); ok eq_array($foo[0], ['wut','HALP']) && $foo[1] eq 'gnarl', 'DwarnF passes lists through correctly'; } @@ -57,12 +57,12 @@ DWARNN: { if ($loaded) { my $x = [1]; my $foo = DwarnN $x; - is $warned_string, qq{\$x => [\n 1\n]\n}, 'DwarnN warns'; + like $warned_string, qr{^\$x => \[\n 1,?\n\]\n\z}, 'DwarnN warns'; ok eq_array($foo, [1]), 'DwarnN passes through correctly'; DwarnN [1]; - is $warned_string, qq{(anon) => [\n 1\n]\n}, 'DwarnN warns'; + like $warned_string, qr{^\(anon\) => \[\n 1,?\n\]\n\z}, 'DwarnN warns'; } } @@ -70,6 +70,6 @@ DDIE: { eval { DdieS [ 'k', 'bar' ]; }; - is $@, qq{[\n "k",\n "bar"\n]\n}, 'DwarnD dies output correctly'; + like $@, qr{^\[\n "k",\n "bar",?\n\]\n\z}, 'DwarnD dies output correctly'; }