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