Subject: | Patch: enable new Trailingcomma option when available |
The latest version of Data::Dumper (currently available only as part of Perl 5.24, but it may at some point get an updated dual-life release) has a new Trailingcomma option that emits an extra comma after the last element of an array or hash that would otherwise end a line.
The attached patch enables this option in Data::Dumper::Concise when possible, though everything remains unchanged when run under older versions.
Subject: | 0001-Use-Trailingcomma-option-when-available.patch |
From b6521606d2ae9124d0edbb12d2101398a1285a3b Mon Sep 17 00:00:00 2001
From: Aaron Crane <arc@cpan.org>
Date: Mon, 23 May 2016 16:31:23 +0100
Subject: [PATCH] Use Trailingcomma option when available
As of Perl 5.24, Data::Dumper has a new option named "Trailingcomma" that
adds a comma to the end of a line that consists of the last element of an
array or hash. This patch enables that option when possible, without changing
anything for users whose Data::Dumper doesn't have that option.
---
lib/Data/Dumper/Concise.pm | 9 ++++++++-
t/concise.t | 8 +++++++-
t/sugar.t | 10 +++++-----
3 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/lib/Data/Dumper/Concise.pm b/lib/Data/Dumper/Concise.pm
index 42634a9..24ad20f 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<Dumper> object just call C<DumperObject>.
Also try out C<DumperF> which takes a C<CodeRef> 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';
}
--
2.8.2