Use Trailingcomma option when available (RT#114609)
[p5sagit/Data-Dumper-Concise.git] / lib / Data / Dumper / Concise.pm
1 package Data::Dumper::Concise;
2
3 use 5.006;
4
5 our $VERSION = '2.023';
6
7 require Exporter;
8 require Data::Dumper;
9
10 BEGIN { @ISA = qw(Exporter) }
11
12 @EXPORT = qw(Dumper DumperF DumperObject);
13
14 sub DumperObject {
15   my $dd = Data::Dumper->new([]);
16   $dd->Trailingcomma(1) if $dd->can('Trailingcomma');
17   $dd->Terse(1)->Indent(1)->Useqq(1)->Deparse(1)->Quotekeys(0)->Sortkeys(1);
18 }
19
20 sub Dumper { DumperObject->Values([ @_ ])->Dump }
21
22 sub DumperF (&@) {
23   my $code = shift;
24   return $code->(map Dumper($_), @_);
25 }
26
27 =head1 NAME
28
29 Data::Dumper::Concise - Less indentation and newlines plus sub deparsing
30
31 =head1 SYNOPSIS
32
33   use Data::Dumper::Concise;
34
35   warn Dumper($var);
36
37 is equivalent to:
38
39   use Data::Dumper;
40   {
41     local $Data::Dumper::Terse = 1;
42     local $Data::Dumper::Indent = 1;
43     local $Data::Dumper::Useqq = 1;
44     local $Data::Dumper::Deparse = 1;
45     local $Data::Dumper::Quotekeys = 0;
46     local $Data::Dumper::Sortkeys = 1;
47     local $Data::Dumper::Trailingcomma = 1;
48     warn Dumper($var);
49   }
50
51 So for the structure:
52
53   { foo => "bar\nbaz", quux => sub { "fleem" } };
54
55 Data::Dumper::Concise will give you:
56
57   {
58     foo => "bar\nbaz",
59     quux => sub {
60         use warnings;
61         use strict 'refs';
62         'fleem';
63     },
64   }
65
66 instead of the default Data::Dumper output:
67
68   $VAR1 = {
69    'quux' => sub { "DUMMY" },
70    'foo' => 'bar
71   baz'
72   };
73
74 (note the tab indentation, oh joy ...)
75
76 (The trailing comma on the last element of an array or hash is enabled by a new
77 feature in Data::Dumper version 2.159, which was first released in Perl 5.24.
78 Using Data::Dumper::Concise with an older version of Data::Dumper will still
79 work, but you won't get those commas.)
80
81 If you need to get the underlying L<Dumper> object just call C<DumperObject>.
82
83 Also try out C<DumperF> which takes a C<CodeRef> as the first argument to
84 format the output.  For example:
85
86   use Data::Dumper::Concise;
87
88   warn DumperF { "result: $_[0] result2: $_[1]" } $foo, $bar;
89
90 Which is the same as:
91
92   warn 'result: ' . Dumper($foo) . ' result2: ' . Dumper($bar);
93
94 =head1 DESCRIPTION
95
96 This module always exports a single function, Dumper, which can be called
97 with an array of values to dump those values.
98
99 It exists, fundamentally, as a convenient way to reproduce a set of Dumper
100 options that we've found ourselves using across large numbers of applications,
101 primarily for debugging output.
102
103 The principle guiding theme is "all the concision you can get while still
104 having a useful dump and not doing anything cleverer than setting Data::Dumper
105 options" - it's been pointed out to us that Data::Dump::Streamer can produce
106 shorter output with less lines of code. We know. This is simpler and we've
107 never seen it segfault. But for complex/weird structures, it generally rocks.
108 You should use it as well, when Concise is underkill. We do.
109
110 Why is deparsing on when the aim is concision? Because you often want to know
111 what subroutine refs you have when debugging and because if you were planning
112 to eval this back in you probably wanted to remove subrefs first and add them
113 back in a custom way anyway. Note that this -does- force using the pure perl
114 Dumper rather than the XS one, but I've never in my life seen Data::Dumper
115 show up in a profile so "who cares?".
116
117 =head1 BUT BUT BUT ...
118
119 Yes, we know. Consider this module in the ::Tiny spirit and feel free to
120 write a Data::Dumper::Concise::ButWithExtraTwiddlyBits if it makes you
121 happy. Then tell us so we can add it to the see also section.
122
123 =head1 SUGARY SYNTAX
124
125 This package also provides:
126
127 L<Data::Dumper::Concise::Sugar> - provides Dwarn and DwarnS convenience functions
128
129 L<Devel::Dwarn> - shorter form for Data::Dumper::Concise::Sugar
130
131 =head1 SEE ALSO
132
133 We use for some purposes, and dearly love, the following alternatives:
134
135 L<Data::Dump> - prettiness oriented but not amazingly configurable
136
137 L<Data::Dump::Streamer> - brilliant. beautiful. insane. extensive. excessive. try it.
138
139 L<JSON::XS> - no, really. If it's just plain data, JSON is a great option.
140
141 =head1 AUTHOR
142
143 mst - Matt S. Trout <mst@shadowcat.co.uk>
144
145 =head1 CONTRIBUTORS
146
147 frew - Arthur Axel "fREW" Schmidt <frioux@gmail.com>
148
149 =head1 COPYRIGHT
150
151 Copyright (c) 2010 the Data::Dumper::Concise L</AUTHOR> and L</CONTRIBUTORS>
152 as listed above.
153
154 =head1 LICENSE
155
156 This library is free software and may be distributed under the same terms
157 as perl itself.
158
159 =cut
160
161 1;