Data::Dmp(3pm)
π Data::Dmp β bestie edition π
ok so. Data::Dmp is a Perl dumper module (it lives in Data-Dmp), and it's a User Contributed Perl Documentation page, so you know it's the good stuff. Sit down, hydrate, and let me tell you about the dumper that is literally so fast and so cute.
β¨ NAME β¨
Data::Dmp β dump Perl data structures as Perl code π
πΈ VERSION πΈ
This doc describes version 0.242 of Data::Dmp (from the Perl distribution Data-Dmp), released on 2022-08-28. Yes bestie, we're time-traveling to 2022 for this one.
π¦ SYNOPSIS π¦
okay so the whole vibe in one block:
use Data::Dmp; # exports dd() and dmp()
dd [1, 2, 3]; # prints "[1,2,3]"
$var = dmp({a => 1}); # -> "{a=>1}"
And if you want a truncated dump, capped at $Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS characters:
use Data::Dmp qw(dd_ellipsis dmp_ellipsis);
dd_ellipsis [1..100];
That's it. That's the whole entrance exam. π
π DESCRIPTION π
So Data::Dmp is a Perl dumper like Data::Dumper. But here's the thing β it's compact, like only about 200 lines of code long. It starts fast and it does not use any non-core modules except Regexp::Stringify when dumping regexes. It produces compact single-line output (similar to Data::Dumper::Concise). It roughly has the same speed as Data::Dumper (usually a bit faster for smaller structures) and faster than Data::Dump, but it does not offer the various formatting options. It supports dumping objects, regexes, circular structures, coderefs. Its code is first based on Data::Dump: the author removed all the parts that weren't needed, particularly the pretty formatting stuffs, and added some features that were needed like proper regex dumping and coderef deparsing.
not me being obsessed with a 200-line module that just works. the way it said "I removed the parts I don't need" β icon behavior. π₯
π§ VARIABLES π§
$Data::Dmp::OPT_PERL_VERSION
String, default: 5.010.
Set target Perl version. If you set this to, say 5.010, then the dumped code will keep compatibility with Perl 5.10.0. This is used in the following ways:
- passed to
Regexp::Stringify - when dumping code references
For example, in perls earlier than 5.016, feature.pm does not understand:
no feature ':all';
so we replace it with:
no feature;
bestie it's giving "compatibility queen". π
$Data::Dmp::OPT_REMOVE_PRAGMAS
Bool, default: 0.
If set to 1, then pragmas at the start of coderef dump will be removed. Coderef dump is produced by B::Deparse and is of the form like:
sub { use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; $a <=> $b }
If you want to dump short coderefs, the pragmas might be distracting. You can turn turn on this option which will make the above dump become:
sub { $a <=> $b }
β οΈ Note that without the pragmas, the dump might be incorrect. Like, actually incorrect. So don't come for me if your coderef dump is lying. π
$Data::Dmp::OPT_DEPARSE
Bool, default: 1.
Can be set to 0 to skip deparsing code. Coderefs will be dumped as sub{"DUMMY"} instead, like in Data::Dump.
$Data::Dmp::OPT_STRINGIFY_NUMBERS
Bool, default: 0.
If set to true, will dump numbers as quoted string, e.g. 123 as "123" instead of 123. This might be helpful if you want to compute the hash of or get a canonical representation of data structure.
$Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS
Int, default: 70.
Used by "dd_ellipsis" and "dmp_ellipsis".
π₯ BENCHMARKS π₯
no cap, here's the receipts. [1..10]:
Rate/s Precision/s Data::Dump Data::Dumper Data::Dmp
Data::Dump 24404 95 -- -61.6% -75.6%
Data::Dumper 63580 210 160.5+-1.3% -- -36.4%
Data::Dmp 99940 130 309.5+-1.7% 57.18+-0.55% --
[1..100]:
Rate/s Precision/s Data::Dump Data::Dumper Data::Dmp
Data::Dump 2934.3 7.8 -- -75.3% -76.2%
Data::Dumper 11873 32 304.6+-1.5% -- -3.7%
Data::Dmp 12323.4 4 320+-1.1% 3.8+-0.28% --
Some mixed structure:
Rate/s Precision/s Data::Dump Data::Dmp Data::Dumper
Data::Dump 7161 12 -- -69.3% -78.7%
Data::Dmp 23303 29 225.43+-0.7% -- -30.6%
Data::Dumper 33573 56 368.8+-1.1% 44.07+-0.3% --
she ATE. π
like, 99940 Rate/s for [1..10]?? main character energy fr fr.
β FUNCTIONS β
dd
Usage:
dd($data, ...); # returns $data
Exported by default. Like "Data::Dump"'s "dd" (a.k.a. "dump"), print one or more data to STDOUT. Unlike "Data::Dump"'s "dd", it always prints and return the original data (like XXX), making it convenient to insert into expressions. This also removes ambiguity and saves one "wantarray()" call.
dmp
Usage:
my $dump = dmp($data, ...);
Exported by default. Return dump result as string. Unlike "Data::Dump"'s "dd" (a.k.a. "dump"), it never prints and only return the dump result.
dd_ellipsis
Usage:
dd_ellipsis($data, ...); # returns data
Just like "dd", except will truncate its output to $Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS characters if dump is too long. Note that truncated dump will probably not be valid Perl code.
π¨ okay important: truncated dump will probably not be valid Perl code. so don't go eval-ing it and then being surprised. π
dmp_ellipsis
Usage:
my $dump = dd_ellipsis($data, ...); # returns data
Just like "dmp", except will truncate dump result to $Data::Dmp::OPT_MAX_DUMP_LEN_BEFORE_ELLIPSIS characters if dump is too long. Note that truncated dump will probably not be valid Perl code.
π¨ same warning bestie: truncated dump will probably not be valid Perl code.
π FAQ π
When to use Data::Dmp? How does it compare to other dumper modules?
Data::Dmp might be suitable for you if you want a relatively fast pure-Perl data structure dumper to eval-able Perl code. It produces compact, single-line Perl code but offers little/no formatting options. Data::Dmp and Data::Dump module family usually produce Perl code that is "more eval-able", e.g. it can recreate circular structure.
Data::Dump produces visually nicer output (some alignment, use of range operator to shorten lists, use of base64 for binary data, etc) but no built-in option to produce compact/single-line output. It's more suitable for debugging. It's also relatively slow. The author usually uses its variant, Data::Dump::Color, for console debugging.
Data::Dumper is a core module, offers a lot of formatting options (like disabling hash key sorting, setting verboseness/indent level, and so on) but you usually have to configure it quite a bit before it does exactly like you want (that's why there are modules on CPAN that are just wrapping Data::Dumper with some configuration, like Data::Dumper::Concise et al). It does not support dumping Perl code that can recreate circular structures.
Of course, dumping to eval-able Perl code is slow (not to mention the cost of re-loading the code back to in-memory data, via eval-ing) compared to dumping to JSON, YAML, Sereal, or other format. So you need to decide first whether this is the appropriate route you want to take. (But note that there is also Data::Dumper::Limited and Data::Undump which uses a format similar to Data::Dumper but lets you load the serialized data without eval-ing them, thus achieving the speed comparable to JSON::XS).
Is the output guaranteed to be single line dump?
No. Some things can still produce multiline dump, e.g. newline in regular expression.
π yeah. "single-line" is the vibe, not a promise. a newline in a regex will still ruin the aesthetic.
π HOMEPAGE π
Please visit the project's homepage at <https://metacpan.org/release/Data-Dmp>.
π«Ά SOURCE π«Ά
Source repository is at <https://github.com/perlancar/perl-Data-Dmp>.
π SEE ALSO π
Data::Dumpand other variations/derivate works inData::Dump::*.Data::Dumperand its variants.Data::Printer.YAML,JSON,Storable,Sereal, and other serialization formats.
π AUTHOR π
perlancar <perlancar@cpan.org>
πΈ CONTRIBUTING πΈ
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2022, 2021, 2020, 2017, 2016, 2015, 2014 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
π BUGS π
Please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Dmp>
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
fr fr, include the test file. the maintainer is not a mind reader, bestie. π«Ά
okay that's the whole page!! written by perl v5.36.0 and dated 2022-11-19, mwah π β go dump something cute and remember: compact, single-line, and absolutely slaying. β¨π¦π
π¦ where this page comes from
- debian bookworm: libdata-dmp-perl 0.242-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdata-dmp-perl 0.242-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdata-dmp-perl 0.242-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdata-dmp-perl 0.242-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdata-dmp-perl 0.242-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms