1Data::Dmp(3) User Contributed Perl Documentation Data::Dmp(3)
2
3
4
6 Data::Dmp - Dump Perl data structures as Perl code
7
9 This document describes version 0.241 of Data::Dmp (from Perl
10 distribution Data-Dmp), released on 2021-06-24.
11
13 use Data::Dmp; # exports dd() and dmp()
14 dd [1, 2, 3]; # prints "[1,2,3]"
15 $var = dmp({a => 1}); # -> "{a=>1}"
16
17 Print truncated dump (capped at
18 "$Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS" characters):
19
20 use Data::Dmp qw(dd_ellipsis dmp_ellipsis);
21 dd_ellipsis [1..100];
22
24 Data::Dmp is a Perl dumper like Data::Dumper. It's compact (only about
25 200 lines of code long), starts fast and does not use any non-core
26 modules except Regexp::Stringify when dumping regexes. It produces
27 compact single-line output (similar to Data::Dumper::Concise). It
28 roughly has the same speed as Data::Dumper (usually a bit faster for
29 smaller structures) and faster than Data::Dump, but does not offer the
30 various formatting options. It supports dumping objects, regexes,
31 circular structures, coderefs. Its code is first based on Data::Dump: I
32 removed all the parts that I don't need, particularly the pretty
33 formatting stuffs) and added some features that I need like proper
34 regex dumping and coderef deparsing.
35
37 $Data::Dmp::OPT_PERL_VERSION
38 String, default: 5.010.
39
40 Set target Perl version. If you set this to, say 5.010, then the dumped
41 code will keep compatibility with Perl 5.10.0. This is used in the
42 following ways:
43
44 • passed to Regexp::Stringify
45
46 • when dumping code references
47
48 For example, in perls earlier than 5.016, feature.pm does not
49 understand:
50
51 no feature ':all';
52
53 so we replace it with:
54
55 no feature;
56
57 $Data::Dmp::OPT_REMOVE_PRAGMAS
58 Bool, default: 0.
59
60 If set to 1, then pragmas at the start of coderef dump will be removed.
61 Coderef dump is produced by B::Deparse and is of the form like:
62
63 sub { use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; $a <=> $b }
64
65 If you want to dump short coderefs, the pragmas might be distracting.
66 You can turn turn on this option which will make the above dump become:
67
68 sub { $a <=> $b }
69
70 Note that without the pragmas, the dump might be incorrect.
71
72 $Data::Dmp::OPT_DEPARSE
73 Bool, default: 1.
74
75 Can be set to 0 to skip deparsing code. Coderefs will be dumped as
76 "sub{"DUMMY"}" instead, like in Data::Dump.
77
78 $Data::Dmp::OPT_STRINGIFY_NUMBERS
79 Bool, default: 0.
80
81 If set to true, will dump numbers as quoted string, e.g. 123 as "123"
82 instead of 123. This might be helpful if you want to compute the hash
83 of or get a canonical representation of data structure.
84
85 $Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS
86 Int, default: 70.
87
88 Used by "dd_ellipsis" and "dmp_ellipsis".
89
91 [1..10]:
92 Rate/s Precision/s Data::Dump Data::Dumper Data::Dmp
93 Data::Dump 25040 100 -- -64.4% -75.4%
94 Data::Dumper 70280 130 180.6+-1.2% -- -30.9%
95 Data::Dmp 101744 34 306.3+-1.6% 44.78+-0.28% --
96
97 [1..100]:
98 Rate/s Precision/s Data::Dump Data::Dumper Data::Dmp
99 Data::Dump 3141.2 4.7 -- -74.5% -75.1%
100 Data::Dumper 12308 81 291.8+-2.6% -- -2.3%
101 Data::Dmp 12597 13 301.04+-0.73% 2.35+-0.68% --
102
103 Some mixed structure:
104 Rate/s Precision/s Data::Dump Data::Dmp Data::Dumper
105 Data::Dump 7619 24 -- -68.8% -77.9%
106 Data::Dmp 24427.8 8 220.6+-1% -- -29.0%
107 Data::Dumper 34410 53 351.6+-1.6% 40.86+-0.22% --
108
110 dd
111 Usage:
112
113 dd($data, ...); # returns $data
114
115 Exported by default. Like "Data::Dump"'s "dd" (a.k.a. "dump"), print
116 one or more data to STDOUT. Unlike "Data::Dump"'s "dd", it always
117 prints and return the original data (like XXX), making it convenient to
118 insert into expressions. This also removes ambiguity and saves one
119 "wantarray()" call.
120
121 dmp
122 Usage:
123
124 my $dump = dmp($data, ...);
125
126 Exported by default. Return dump result as string. Unlike
127 "Data::Dump"'s "dd" (a.k.a. "dump"), it never prints and only return
128 the dump result.
129
130 dd_ellipsis
131 Usage:
132
133 dd_ellipsis($data, ...); # returns data
134
135 Just like "dd", except will truncate its output to
136 "$Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS" characters if dump is
137 too long. Note that truncated dump will probably not be valid Perl
138 code.
139
140 dmp_ellipsis
141 Usage:
142
143 my $dump = dd_ellipsis($data, ...); # returns data
144
145 Just like "dmp", except will truncate dump result to
146 "$Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS" characters if dump is
147 too long. Note that truncated dump will probably not be valid Perl
148 code.
149
151 When to use Data::Dmp? How does it compare to other dumper modules?
152 Data::Dmp might be suitable for you if you want a relatively fast pure-
153 Perl data structure dumper to eval-able Perl code. It produces compact,
154 single-line Perl code but offers little/no formatting options.
155 Data::Dmp and Data::Dump module family usually produce Perl code that
156 is "more eval-able", e.g. it can recreate circular structure.
157
158 Data::Dump produces visually nicer output (some alignment, use of range
159 operator to shorten lists, use of base64 for binary data, etc) but no
160 built-in option to produce compact/single-line output. It's more
161 suitable for debugging. It's also relatively slow. I usually use its
162 variant, Data::Dump::Color, for console debugging.
163
164 Data::Dumper is a core module, offers a lot of formatting options (like
165 disabling hash key sorting, setting verboseness/indent level, and so
166 on) but you usually have to configure it quite a bit before it does
167 exactly like you want (that's why there are modules on CPAN that are
168 just wrapping Data::Dumper with some configuration, like
169 Data::Dumper::Concise et al). It does not support dumping Perl code
170 that can recreate circular structures.
171
172 Of course, dumping to eval-able Perl code is slow (not to mention the
173 cost of re-loading the code back to in-memory data, via eval-ing)
174 compared to dumping to JSON, YAML, Sereal, or other format. So you need
175 to decide first whether this is the appropriate route you want to take.
176 (But note that there is also Data::Dumper::Limited and Data::Undump
177 which uses a format similar to Data::Dumper but lets you load the
178 serialized data without eval-ing them, thus achieving the speed
179 comparable to JSON::XS).
180
181 Is the output guaranteed to be single line dump?
182 No. Some things can still produce multiline dump, e.g. newline in
183 regular expression.
184
186 Please visit the project's homepage at
187 <https://metacpan.org/release/Data-Dmp>.
188
190 Source repository is at <https://github.com/perlancar/perl-Data-Dmp>.
191
193 Please report any bugs or feature requests on the bugtracker website
194 <https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Dmp>
195
196 When submitting a bug or request, please include a test-file or a patch
197 to an existing test-file that illustrates the bug or desired feature.
198
200 Data::Dump and other variations/derivate works in Data::Dump::*.
201
202 Data::Dumper and its variants.
203
204 Data::Printer.
205
206 YAML, JSON, Storable, Sereal, and other serialization formats.
207
209 perlancar <perlancar@cpan.org>
210
212 This software is copyright (c) 2021, 2020, 2017, 2016, 2015, 2014 by
213 perlancar@cpan.org.
214
215 This is free software; you can redistribute it and/or modify it under
216 the same terms as the Perl 5 programming language system itself.
217
218
219
220perl v5.34.0 2021-07-22 Data::Dmp(3)