Data::...Compact(3pm)

๐Ÿ“ฆ
version 0.006000 ยท 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ’– NAME ๐Ÿ’–

okay so bestie, this is the User Contributed Perl Documentation page for Data::Dumper::Compact(3pm) โ€” it's a vertically compact width-limited data formatter. that's the whole name-vibe. periodt. ๐Ÿ’…

โœจ SYNOPSIS โœจ

basic usage as a function:

use Data::Dumper::Compact 'ddc';

warn ddc($some_data_structure);

warn ddc($some_data_structure, \%options);

slightly more clever usage as a function:

use Data::Dumper::Compact ddc => \%default_options;

warn ddc($some_data_structure);

warn ddc($some_data_structure, \%extra_options);

OO usage:

use Data::Dumper::Compact;

warn Data::Dumper::Compact->dump($data, \%options);

my $ddc = Data::Dumper::Compact->new(\%options);

warn $ddc->dump($data);

warn $ddc->dump($data, \%extra_options);

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay so Data::Dumper::Compact, aka DDC from here on out, was born because the author was annoyed at valuable wasted whitespace while paging through logs from both Data::Dumper and Data::Dump. Data::Dump tries to format horizontally first, but then if that fails, it immediately switches to formatting fully vertically, instead of trying to, like, format a six-element arrayref three per line. ๐Ÿ’€

so here are a few of the specifics โ€” and note that all examples, unless otherwise specified, are dumped with default options:

๐Ÿ“ Arrays and Strings ๐Ÿ“

when you give DDC arrays made of reasonably long strings, it does its best to produce a sane representation within its max_width:

[
  1, 2, [
    'longstringislonglongstringislonglongstringislong',
    'longstringislonglongstringislong', 'longstringislong',
    'longstringislonglongstringislonglongstringislong', 'longstringislong',
    'longstringislonglongstringislong', 'longstringislong',
    'longstringislonglongstringislong',
    'longstringislonglongstringislonglongstringislong',
    'longstringislonglongstringislong', 'longstringislonglongstringislong',
    'longstringislonglongstringislonglongstringislong', 'longstringislong',
    'longstringislong', 'longstringislonglongstringislonglongstringislong',
    'longstringislong', 'longstringislong', 'longstringislong',
    'longstringislonglongstringislong',
    'longstringislonglongstringislonglongstringislong', 'a', 'b', 'c',
    'longstringislonglongstringislonglongstringislonglongstringislong',
    'longstringislonglongstringislonglongstringislonglongstringislong',
    'longstringislonglongstringislonglongstringislonglongstringislong',
  ], 3,
]

๐Ÿ‡ Keys and Hashrefs ๐Ÿ‡

when faced with a -foo style value, it gets a => even in an array, and hash values that we can are single-line formatted:

[
  'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', [
    'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
    'cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
  ],
  -blah => { baz => 'quux', foo => 'bar' },
]

๐ŸŽ€ The String Thing ๐ŸŽ€

strings are single quoted when DDC is absolutely sure that's safe, and double quoted otherwise:

[ { -foo => {
      bar =>
        'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
      baz => "bbbbbbbbbbbbbbbbbbbb\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
} } ]

๐ŸŒ™ Lonely hash key ๐ŸŒ™

when a single hash key can't be formatted in a oneline form within the length, DDC will try spilling it to its own line:

{
  -xxxxxxxxxxxxx => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
}

if even that isn't enough, it formats it below and indented:

{ -xxxxxxxxxxxxx =>
    'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
}

๐Ÿฆ‹ Strings and the dot operator ๐Ÿฆ‹

if a string simply won't fit, DDC splits it and indents it using .:

[ 'xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyx'
  .'yxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy'
  .'xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyx'
  .'yxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy'
]

๐Ÿ‘ฝ Unknown unknowns ๐Ÿ‘ฝ

anything DDC doesn't understand is passed through its dumper option, though since Data::Dumper (at the time of writing) forgets to pass through its indentation level to B::Concise, we slightly tweak that behaviour on the way in for the default dumper. but the end result looks like:

{ foo => { bar => sub {
      use warnings;
      use strict 'refs';
      my($x, $y) = @_;
      return $x * $y;
} } }

๐Ÿ‘‘ Bless you ๐Ÿ‘‘

when encountering an object, if it's a blessed array or hashref, DDC will attempt to format that too:

[ bless( {
    x => 3,
    y => [ 'foo', 'bar', 'baz', 'quux', 'fleem', 'blather', 'obrien' ],
    z => 'lololololololololololololololol',
}, "OhGods::Lol" ) ]

๐ŸŒˆ All together now ๐ŸŒˆ

the full set of behaviours allows compact (and, we hope, readable) versions of complex data structures. to give you one of the examples that expired this module โ€” here is the formatting under standard options for a moderately complex SQL::Abstract update statement:

{
  _ => [
    'tree_table', -join => {
      as => 'tree',
      on => { 'tree.id' => 'tree_with_path.id' },
      to => { -select => {
          from => 'tree_with_path',
          select => '*',
          with_recursive => [
            [ 'tree_with_path', 'id', 'parent_id', 'path' ], { -select => {
                _ => [
                  'id', 'parent_id', { -as =>
                      [
                        { -cast => { -as => [ 'id', 'char', 255 ] } },
                        'path',
                      ]
                  },
                ],
                from => 'tree_table',
                union_all => { -select => {
                    _ => [
                      't.id', 't.parent_id', { -as => [
                          { -concat => [ 'r.path', \"'/'", 't.id' ] },
                          'path',
                      ] },
                    ],
                    from => [
                      'tree_table', -as => 't', -join => {
                        as => 'r',
                        on => { 't.parent_id' => 'r.id' },
                        to => 'tree_with_path',
                      },
                    ],
                } },
                where => { parent_id => undef },
            } },
          ],
      } },
    },
  ],
  set => { path => { -ident => [ 'tree', 'path' ] } },
}

and the version (generated by setting max_width to 40) that runs out of space and thereby forces the spill vertically logic to kick in while still attempting to be at least somewhat compact:

{
  _ => [
    'tree_table',
    '-join',
    {
      as => 'tree',
      on => {
        'tree.id' => 'tree_with_path.id',
      },
      to => {
        -select => {
          from => 'tree_with_path',
          select => '*',
          with_recursive => [
            [
              'tree_with_path',
              'id',
              'parent_id',
              'path',
            ],
            {
              -select => {
                _ => [
                  'id',
                  'parent_id',
                  {
                    -as => [
                      {
                        -cast => {
                          -as => [
                            'id',
                            'char',
                            255,
                          ],
                        },
                      },
                      'path',
                    ],
                  },
                ],
                from => 'tree_table',
                union_all => {
                  -select => {
                    _ => [
                      't.id',
                      't.parent_id',
                      {
                        -as => [
                          {
                            -concat => [
                              'r.path',
                              \"'/'",
                              't.id',
                            ],
                          },
                          'path',
                        ],
                      },
                    ],
                    from => [
                      'tree_table',
                      '-as',
                      't',
                      '-join',
                      {
                        as => 'r',
                        on => {
                          't.parent_id' => 'r.id',
                        },
                        to => 'tree_with_path',
                      },
                    ],
                  },
                },
                where => {
                  parent_id => undef,
                },
              },
            },
          ],
        },
      },
    },
  ],
  set => {
    path => {
      -ident => [
        'tree',
        'path',
      ],
    },
  },
}

โญ Summary โญ

hopefully it's clear what the goal is, and what we've done to achieve it. โœจ

while the system is already somewhat configurable, further options are almost certainly implementable, although if you really want such an option then we expect you to turn up with documentation and test cases for it so we just have to write the code. ๐Ÿ’…

โœจ OPTIONS โœจ

okay so these are all the little knobs you can twiddle ๐ŸŽ›๏ธ they get passed along to new (peep that section below), so let's go through every single one, no skips.

max_width

This is the width DDC will attempt to keep as the maximum ๐Ÿ“ like, that's the character budget it's genuinely trying its best to respect. And if something overflows it anyway โ€” despite our best efforts โ€” DDC falls back to a more vertically sprawling format, so it at least overflows as little as is feasible. ๐Ÿ’€

Default: 78

indent_by

The string to indent by. So if you wanna set, like, a 4 space indent, you pass "' 'x4". ๐ŸŽ€

Default: ' ' (that's two spaces, bestie).

indent_width

How many characters one indent should be considered to be. Lowkey you generally only need to manually set this if your indent_by is "\t".

Default: length($self->indent_by)

transforms

The set of transforms to apply on every dump operation. Slide down to "transform" for the full tea ๐Ÿซ–

Default: []

dumper

The dumper function that gets used for dumping the things DDC just does not understand โ€” coderefs, regexprefs, etc. ๐Ÿ™ˆ

Defaults to the same options as Data::Dumper::Concise (which is, itself, only a Data::Dumper configuration, albeit it comes with Devel::Dwarn, which is rather more interesting) โ€” although on top of that we add a little bit of extra cleverness to make B::Deparse use the correct indentation, since for some reason Data::Dumper doesn't (at the time of writing) do that. ๐Ÿ˜ญ

If you supply it yourself, it needs to be a single argument coderef โ€” you could for example use \&Data::Dumper::Dumper, though that would almost certainly be pointless.


๐ŸŽ EXPORTS ๐ŸŽ

ddc

use Data::Dumper::Compact 'ddc';
use Data::Dumper::Compact 'ddc' => \%options;

If the first argument to use/import() is 'ddc', a subroutine ddc() gets installed in the calling package, and it behaves exactly like calling dump. โœจ

If the second argument is a hashref, it becomes the options passed to new.

This feature is effectively sugar over dump_cb, in that:

Data::Dumper::Compact->import(ddc => \%options)

is equivalent to:

*ddc = Data::Dumper::Compact->new(\%options)->dump_cb;

๐Ÿ› ๏ธ METHODS ๐Ÿ› ๏ธ

new

my $ddc = Data::Dumper::Compact->new;
my $ddc = Data::Dumper::Compact->new(%options);
my $ddc = Data::Dumper::Compact->new(\%options);

Constructor, bestie ๐ŸŽ€ Takes a hash or hashref of OPTIONS.

dump

my $formatted = Data::Dumper::Compact->dump($data, \%options?);

my $formatted = $ddc->dump($data, \%merge_options?);

This is THE method you're gonna want most of the time ๐Ÿ‘‘ it ties the rest of the functionality together into one single data-structure-to-string bundle. With just a data argument, it's equivalent to:

$ddc->format( $ddc->transform( $ddc->transforms, $ddc->expand($data) );

In class method form, the options you provide get passed to new; in instance method form, options โ€” if you provide them โ€” get merged into $ddc just for this one invocation. ๐Ÿฆ‹

dump_cb

my $cb = $ddc->dump_cb;

Returns a subroutine reference that's a curried call to dump:

$cb->($data, \%extra_options); # equivalent to $ddc->dump(...)

Mostly useful if you wanna create your own custom ddc()-like thing:

use Data::Dumper::Compact;
BEGIN { *Dumper = Data::Dumper::Compact->new->dump_cb }

expand

my $exp = $ddc->expand($data);

Expands a data structure into DDC tagged data. The result is, recursively,

[ $type, $payload ]

where if $type is one of string, key, or thing, the payload is a simple string (thing meaning something unknown and therefore delegated to dumper). If the type is an array:

[ array => \@values ]

and if the type is a hash:

[ hash => [ \@keys, \%value_map ] ]

where the keys provide an order for formatting, and the value map is a hashref of keys to expanded values.

A plain string becomes a string, unless it fits the -foo style pattern that autoquotes, in which case it becomes a key. ๐Ÿ’…

add_transform

$ddc->add_transform(sub { ... });
$ddc->add_transform({ hash => sub { ... }, _ => sub { ... });

Appends a transform to $ddc->transforms โ€” head to "transform" for how it behaves.

Returns $ddc so you can chain, because obviously. โ›“๏ธ

transform

my $tf_exp = $ddc->transform($tfspec, $exp);

Takes a transform specification and expanded tagged data, and returns the transformed expanded expression. A transform spec is an arrayref containing transforms, where each transform is applied in order โ€” so the last transform added via add_transform will be the last one to transform the data (each transform will consist of a datastructure representing which parts of the $exp tree it should be called for, plus subroutines representing the relevant transforms).

Transform subroutines are called as a method on the $ddc with the arguments $type, $payload, $path, where $path is an arrayref of the keys/values of the containing hashes and arrays, aggregated as DDC descends through the $exp tree.

Each transform is expected to return either nothing โ€” to indicate it doesn't wish to modify the result โ€” or a replacement expanded data structure. The simplest form of transform is a subref, which gets called for everything.

So, to add ' IN MICE' to every string that's part of an array under a hash key called study_results, i.e.:

my $data = { study_results => [
    'Sense Of Touch Is Formed In the Brain Before Birth'.
    "We can't currently cure MS but a single cell could change that",
] };

my $tf_exp = $ddc->transform([ sub {
  my ($self, $type, $payload, $path) = @_;
  return unless $type eq 'string' and ($path->[-2]||'') eq 'study_results';
  return [ $type, $payload.' IN MICE' ];
} ], $ddc->expand($data));

will return:

[ hash => [
  [ 'study_results' ],
  { study_results => [ array => [
    [ string => 'Sense Of Touch Is Formed In the Brain Before Birth IN MICE' ],
    [ string => "We can't currently cure MS but a single cell could change that IN MICE", ],
  ] ] }
] ]

If a hashref is found, then the values are expected to be transforms, and DDC will use $hashref->{$type}||$hashref->{_} as the transform, or skip if neither is present. So the previous example could be written as:

$ddc->transform([ { string => sub {
  my ($self, $type, $payload, $path) = @_;
  return unless ($path->[-2]||'') eq 'study_results';
  return [ $type, $payload.' IN MICE' ];
} } ], $ddc->expand($data));

If the value of the spec entry itself, or the relevant hash value, is an arrayref, it's assumed to contain a spec for trailing path entries, with the last element being the transform subroutine. A path entry match can be an exact scalar (tested via eq, since it works fine for both strings and integer array indices), a regexp, undef to indicate "any value is fine here", or a subroutine which will be called with the path entry as both $_[0] and $_. So the example we've been using could also be written as:

$ddc->transform([ { string => [
  'study_results', undef,
  sub { [ string => $_[2].' IN MICE' ] }
] } ], $ddc->expand($data));

or

$ddc->transform([ { string => [
  qr/^study_results$/, sub { 1 },
  sub { [ string => $_[2].' IN MICE' ] }
] } ], $ddc->expand($data));

Note that while the $tfspec is not passed to transform subroutines, for the duration of the transform call the transforms option is localised to the provided routine, so

sub {
  my ($self, $type, $payload, $path) = @_;
  my $tfspec = $self->transforms;
  ...
}

will return the top level $tfspec passed to the transform call. ๐Ÿง 

Thanks to http://twitter.com/justsaysinmice for the inspiration. ๐Ÿญ

format

my $formatted = $ddc->format($exp);

Takes expanded tagged data and renders it to a formatted string โ€” suitable for printing or warning or etc. ๐Ÿ’–

Accepts the following type tags: array, list, hash, key, string, thing. Arrays and hashes are formatted as compactly as possible within the constraint of max_width, but if overflow occurs then DDC falls back to spilling everything vertically, so newlines are used for most spacing and therefore it doesn't exceed the max width any more than strictly necessary. ๐Ÿ˜ค

Strings are formatted as single quote if obvious, and double quote if not.

Keys are treated as strings when present as hash values, but when an element of array values, are formatted as the_key => where possible.

Lists are formatted as single line qw() expressions if possible, or ( ... ) if not.

Arrays and hashes are formatted in the manner to which one would hope readers are accustomed, except more compact. โœจ

ALGORITHM ๐Ÿง โœจ

okay bestie, here's the current algorithm of DDC described in full. We reserve the right to change it for the better. ๐Ÿ’…

If you didn't already read the overview examples in "WHY" do that first. ๐Ÿซถ

Vertical mode means DDC has given up on fitting within the desired width and is now just trying to not use too much vertical space. ๐Ÿ˜ฎโ€๐Ÿ’จ

Oneline mode is DDC testing to see if a single line rendering of something will fit within the available space. Things will often be rendered more than once since DDC is optimising for compact readable output rather than raw straight line performance. ๐Ÿ”


Top level formatting ๐ŸŽฏ

If something is formatted and the remaining width is zero or negative, DDC accepts default on max_width and bails out to a fully vertical approach so it overflows the desired width no more than necessary. ๐Ÿ’€

Array formatting ๐Ÿ“

If already in vertical mode, formats one array element per line, appended with ,:

[
  1,
  2,
  3
]

If in possible oneline mode, formats all but the last element according to the "Array element" rules, the last element according to normal formatting, and joins them with ' ' in the hopes this is narrow enough. Return this if oneline is forced or it fits:

[ 1, 2, 3 ]

If there's only a single internal member, tries to use the "Single entry formatting" strategy to cuddle it. ๐Ÿฅฐ

[ [
  <something inside>
] ]

Otherwise, attempts to bundle things as best possible: Each element is formatted according to the "Array element" rules, and multiple results are concatenated together onto a single line where that still remains within the available width.

[
  'foo', 'bar', 'baz',
  'red', 'white', 'blue',
]

Array element ๐Ÿงฉ

Elements are normally formatted as "$formatted.','" except if an element is of type "key" in which cases it becomes "$key =>".

"whatever the smeg",
smeg_off =>

List formatting ๐Ÿ“

The type "list" is synthetic and only introduced by transforms. โœจ

It is formatted identically to an arrayref except with "( )" instead of "[ ]", with the exception that if it consists of only plain strings and will fit onto a single line, it formats as a "qw(x y x)" style list.

qw(foo bar baz)
(
  'foo',
  'bar',
  'baz',
)

Single entry formatting ๐Ÿซ‚

Where possible, a single entry will be cuddled such that the opening delimiters are both on the first line, and the closing delimiters both on the final line, to reduce the vertical space consumption of nested single entry array and/or hashrefs.

to => { -select => {
    ...
} }

[ 'SRV:8FB66F32' ], [ [
    '/opt/voice-srvc-native/bin/async-srvc-att-gateway-poller', 33,
    'NERV::Voice::SRV::Native::AsyncSRVATTGatewayPoller::main',
] ],

Hash formatting ๐Ÿ—๏ธ

If already in vertical mode, key/value pairs are formatted separated by newlines, with no attention paid to key length.

{
  foo => ...,
  bar => ...,
}

If potentially in oneline mode, key/value pairs are formatted separated by ', ' and the value is returned if forced or if remaining width allows the oneline rendering.

{ foo => ..., bar => ... }

Otherwise, all key/value pairs are formatted as "key => value" where possible, but if the first line of the value is too long, the value is moved to the next line and indented.

key => 'shortvalue'
key =>
  'overlylongvalue'

If there's only a single such key/value pair, tries to use the "Single entry formatting" strategy to cuddle it. ๐Ÿฅบ

{ zathrus => {
    listened_to => 0,
} }

Otherwise returns key/value pairs indented and separated by newlines

{
  foo => ...,
  bar => ...,
}

String formatting ๐Ÿงต

Uses single quotes if sure that's safe, double quotes otherwise.

'foo bar baz quux'
"could have been '' but nicer to not screw up\n the indents with a newline"

Attempts to format a string within the available width, using multiple lines and the "." concatenation operator if necessary,.

'this would be an'
.'annoyingly long'
.'string'

The target width is set to 20 in vertical mode to try and not be too ugly. ๐Ÿ˜ญ

Object formatting ๐ŸŽ€

Objects are tested to see if their underlying reference is an array or hash. If so, it's formatted with 'bless( ' prepended and , $class) appended. This so far appears to interact nicely with everything else. ๐Ÿ’…


AUTHOR ๐Ÿ‘‘

mst - Matt S Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>

CONTRIBUTORS ๐Ÿซถ

None so far.

Copyright (c) 2019 the Data::Dumper::Compact "AUTHOR" and "CONTRIBUTORS" as listed above.

LICENSE ๐Ÿ“œ

This library is free software and may be distributed under the same terms as perl itself. See https://dev.perl.org/licenses/.


and that's the whole vibe, bestie! this was perl v5.36.0, dated 2022-10-04, from Data::...Compact(3pm) ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from