| 1 |
NAME |
| 2 |
Data::Dumper::Compact - Vertically compact width-limited data formatter |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
Basic usage as a function: |
| 6 |
|
| 7 |
use Data::Dumper::Compact 'ddc'; |
| 8 |
|
| 9 |
warn ddc($some_data_structure); |
| 10 |
|
| 11 |
warn ddc($some_data_structure, \%options); |
| 12 |
|
| 13 |
Slightly more clever usage as a function: |
| 14 |
|
| 15 |
use Data::Dumper::Compact ddc => \%default_options; |
| 16 |
|
| 17 |
warn ddc($some_data_structure); |
| 18 |
|
| 19 |
warn ddc($some_data_structure, \%extra_options); |
| 20 |
|
| 21 |
OO usage: |
| 22 |
|
| 23 |
use Data::Dumper::Compact; |
| 24 |
|
| 25 |
warn Data::Dumper::Compact->dump($data, \%options); |
| 26 |
|
| 27 |
my $ddc = Data::Dumper::Compact->new(\%options); |
| 28 |
|
| 29 |
warn $ddc->dump($data); |
| 30 |
|
| 31 |
warn $ddc->dump($data, \%extra_options); |
| 32 |
|
| 33 |
DESCRIPTION |
| 34 |
Data::Dumper::Compact, henceforth referred to as DDC, was born because I |
| 35 |
was annoyed at valuable wasted whitespace paging through both |
| 36 |
Data::Dumper and Data::Dump based logs - Data::Dump attempts to format |
| 37 |
horizontally first, but then if it fails, immediately switches to |
| 38 |
formatting fully vertically, rather than trying to e.g. format a six |
| 39 |
element arrayref three per line. |
| 40 |
|
| 41 |
So here's a few of the specifics (noting that all examples unless |
| 42 |
otherwise specified are dumped with default options): |
| 43 |
|
| 44 |
Arrays and Strings |
| 45 |
Given arrays consisting of reasonably long strings, DDC does its best to |
| 46 |
produce a sane representation within its "max_width": |
| 47 |
|
| 48 |
[ |
| 49 |
1, 2, [ |
| 50 |
'longstringislonglongstringislonglongstringislong', |
| 51 |
'longstringislonglongstringislong', 'longstringislong', |
| 52 |
'longstringislonglongstringislonglongstringislong', 'longstringislong', |
| 53 |
'longstringislonglongstringislong', 'longstringislong', |
| 54 |
'longstringislonglongstringislong', |
| 55 |
'longstringislonglongstringislonglongstringislong', |
| 56 |
'longstringislonglongstringislong', 'longstringislonglongstringislong', |
| 57 |
'longstringislonglongstringislonglongstringislong', 'longstringislong', |
| 58 |
'longstringislong', 'longstringislonglongstringislonglongstringislong', |
| 59 |
'longstringislong', 'longstringislong', 'longstringislong', |
| 60 |
'longstringislonglongstringislong', |
| 61 |
'longstringislonglongstringislonglongstringislong', 'a', 'b', 'c', |
| 62 |
'longstringislonglongstringislonglongstringislonglongstringislong', |
| 63 |
'longstringislonglongstringislonglongstringislonglongstringislong', |
| 64 |
'longstringislonglongstringislonglongstringislonglongstringislong', |
| 65 |
], 3, |
| 66 |
] |
| 67 |
|
| 68 |
Keys and Hashrefs |
| 69 |
When faced with a "-foo" style value, it gets a "=>" even in an array, |
| 70 |
and hash values that we can are single-line formatted: |
| 71 |
|
| 72 |
[ |
| 73 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', [ |
| 74 |
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', |
| 75 |
'cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc', |
| 76 |
], |
| 77 |
-blah => { baz => 'quux', foo => 'bar' }, |
| 78 |
] |
| 79 |
|
| 80 |
The String Thing |
| 81 |
Strings are single quoted when DDC is absolutely sure that's safe, and |
| 82 |
double quoted otherwise: |
| 83 |
|
| 84 |
[ { -foo => { |
| 85 |
bar => |
| 86 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', |
| 87 |
baz => "bbbbbbbbbbbbbbbbbbbb\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 88 |
} } ] |
| 89 |
|
| 90 |
Lonely hash key |
| 91 |
When a single hash key can't be formatted in a oneline form within the |
| 92 |
length, DDC will try spilling it to its own line: |
| 93 |
|
| 94 |
{ |
| 95 |
-xxxxxxxxxxxxx => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' |
| 96 |
} |
| 97 |
|
| 98 |
If even that isn't enough, it formats it below and indented: |
| 99 |
|
| 100 |
{ -xxxxxxxxxxxxx => |
| 101 |
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' |
| 102 |
} |
| 103 |
|
| 104 |
Strings and the dot operator |
| 105 |
If a string simply won't fit, DDC splits it and indents it using ".": |
| 106 |
|
| 107 |
[ 'xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyx' |
| 108 |
.'yxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy' |
| 109 |
.'xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyx' |
| 110 |
.'yxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy' |
| 111 |
] |
| 112 |
|
| 113 |
Unknown unknowns |
| 114 |
Anything DDC doesn't understand is passed through its "dumper" option, |
| 115 |
though since Data::Dumper (at the time of writing) forgets to pass |
| 116 |
through its indentation level to B::Concise, we slightly tweak that |
| 117 |
behaviour on the way in for the default "dumper". But the end result |
| 118 |
looks like: |
| 119 |
|
| 120 |
{ foo => { bar => sub { |
| 121 |
use warnings; |
| 122 |
use strict 'refs'; |
| 123 |
my($x, $y) = @_; |
| 124 |
return $x * $y; |
| 125 |
} } } |
| 126 |
|
| 127 |
Bless you |
| 128 |
When encountering an object, if it's a blessed array or hashref, DDC |
| 129 |
will attempt to format that too: |
| 130 |
|
| 131 |
[ bless( { |
| 132 |
x => 3, |
| 133 |
y => [ 'foo', 'bar', 'baz', 'quux', 'fleem', 'blather', 'obrien' ], |
| 134 |
z => 'lololololololololololololololol', |
| 135 |
}, "OhGods::Lol" ) ] |
| 136 |
|
| 137 |
All together now |
| 138 |
The full set of behaviours allows compact (and, we hope, readable) |
| 139 |
versions of complex data structures. To provide one of the examples that |
| 140 |
expired this module - here is the formatting under standard options for |
| 141 |
a moderately complex SQL::Abstract update statement: |
| 142 |
|
| 143 |
{ |
| 144 |
_ => [ |
| 145 |
'tree_table', -join => { |
| 146 |
as => 'tree', |
| 147 |
on => { 'tree.id' => 'tree_with_path.id' }, |
| 148 |
to => { -select => { |
| 149 |
from => 'tree_with_path', |
| 150 |
select => '*', |
| 151 |
with_recursive => [ |
| 152 |
[ 'tree_with_path', 'id', 'parent_id', 'path' ], { -select => { |
| 153 |
_ => [ |
| 154 |
'id', 'parent_id', { -as => |
| 155 |
[ |
| 156 |
{ -cast => { -as => [ 'id', 'char', 255 ] } }, |
| 157 |
'path', |
| 158 |
] |
| 159 |
}, |
| 160 |
], |
| 161 |
from => 'tree_table', |
| 162 |
union_all => { -select => { |
| 163 |
_ => [ |
| 164 |
't.id', 't.parent_id', { -as => [ |
| 165 |
{ -concat => [ 'r.path', \"'/'", 't.id' ] }, |
| 166 |
'path', |
| 167 |
] }, |
| 168 |
], |
| 169 |
from => [ |
| 170 |
'tree_table', -as => 't', -join => { |
| 171 |
as => 'r', |
| 172 |
on => { 't.parent_id' => 'r.id' }, |
| 173 |
to => 'tree_with_path', |
| 174 |
}, |
| 175 |
], |
| 176 |
} }, |
| 177 |
where => { parent_id => undef }, |
| 178 |
} }, |
| 179 |
], |
| 180 |
} }, |
| 181 |
}, |
| 182 |
], |
| 183 |
set => { path => { -ident => [ 'tree', 'path' ] } }, |
| 184 |
} |
| 185 |
|
| 186 |
And the version (generated by setting "max_width" to 40) that runs out |
| 187 |
of space and thereby forces the "spill vertically" logic to kick in |
| 188 |
while still attemping to be at least somewhat compact: |
| 189 |
|
| 190 |
{ |
| 191 |
_ => [ |
| 192 |
'tree_table', |
| 193 |
'-join', |
| 194 |
{ |
| 195 |
as => 'tree', |
| 196 |
on => { |
| 197 |
'tree.id' => 'tree_with_path.id', |
| 198 |
}, |
| 199 |
to => { |
| 200 |
-select => { |
| 201 |
from => 'tree_with_path', |
| 202 |
select => '*', |
| 203 |
with_recursive => [ |
| 204 |
[ |
| 205 |
'tree_with_path', |
| 206 |
'id', |
| 207 |
'parent_id', |
| 208 |
'path', |
| 209 |
], |
| 210 |
{ |
| 211 |
-select => { |
| 212 |
_ => [ |
| 213 |
'id', |
| 214 |
'parent_id', |
| 215 |
{ |
| 216 |
-as => [ |
| 217 |
{ |
| 218 |
-cast => { |
| 219 |
-as => [ |
| 220 |
'id', |
| 221 |
'char', |
| 222 |
255, |
| 223 |
], |
| 224 |
}, |
| 225 |
}, |
| 226 |
'path', |
| 227 |
], |
| 228 |
}, |
| 229 |
], |
| 230 |
from => 'tree_table', |
| 231 |
union_all => { |
| 232 |
-select => { |
| 233 |
_ => [ |
| 234 |
't.id', |
| 235 |
't.parent_id', |
| 236 |
{ |
| 237 |
-as => [ |
| 238 |
{ |
| 239 |
-concat => [ |
| 240 |
'r.path', |
| 241 |
\"'/'", |
| 242 |
't.id', |
| 243 |
], |
| 244 |
}, |
| 245 |
'path', |
| 246 |
], |
| 247 |
}, |
| 248 |
], |
| 249 |
from => [ |
| 250 |
'tree_table', |
| 251 |
'-as', |
| 252 |
't', |
| 253 |
'-join', |
| 254 |
{ |
| 255 |
as => 'r', |
| 256 |
on => { |
| 257 |
't.parent_id' => 'r.id', |
| 258 |
}, |
| 259 |
to => 'tree_with_path', |
| 260 |
}, |
| 261 |
], |
| 262 |
}, |
| 263 |
}, |
| 264 |
where => { |
| 265 |
parent_id => undef, |
| 266 |
}, |
| 267 |
}, |
| 268 |
}, |
| 269 |
], |
| 270 |
}, |
| 271 |
}, |
| 272 |
}, |
| 273 |
], |
| 274 |
set => { |
| 275 |
path => { |
| 276 |
-ident => [ |
| 277 |
'tree', |
| 278 |
'path', |
| 279 |
], |
| 280 |
}, |
| 281 |
}, |
| 282 |
} |
| 283 |
|
| 284 |
Summary |
| 285 |
Hopefully it's clear what the goal is, and what we've done to achieve |
| 286 |
it. |
| 287 |
|
| 288 |
While the system is already somewhat configurable, further options are |
| 289 |
almost certainly implementable, although if you really want such an |
| 290 |
option then we expect you to turn up with documentation and test cases |
| 291 |
for it so we just have to write the code. |
| 292 |
|
| 293 |
OPTIONS |
| 294 |
max_width |
| 295 |
Represents the width that DDC will attempt to keep as the maximum (if |
| 296 |
something overflows it in spite of our best efforts, DDC will fall back |
| 297 |
to a more vertically sprawling format to at least overflow as little as |
| 298 |
feasible). |
| 299 |
|
| 300 |
Default: 78 |
| 301 |
|
| 302 |
indent_by |
| 303 |
The string to indent by. To set e.g. 4 space indent, pass "' 'x4". |
| 304 |
|
| 305 |
Default: ' ' (two spaces). |
| 306 |
|
| 307 |
indent_width |
| 308 |
How many characters one indent should be considered to be. Generally you |
| 309 |
only need to manually set this if your "indent_by" is "\t". |
| 310 |
|
| 311 |
Default: "length($self->indent_by)" |
| 312 |
|
| 313 |
transforms |
| 314 |
Set of transforms to apply on every "dump" operation. See "transform" |
| 315 |
for more information. |
| 316 |
|
| 317 |
Default: "[]" |
| 318 |
|
| 319 |
dumper |
| 320 |
The dumper function to be used for dumping things DDC doesn't |
| 321 |
understand, such as coderefs, regexprefs, etc. |
| 322 |
|
| 323 |
Defaults to the same options as Data::Dumper::Concise (which is, itself, |
| 324 |
only a Data::Dumper configuration albeit it comes with Devel::Dwarn |
| 325 |
which is rather more interesting) - although on top of that we add a |
| 326 |
little bit of extra cleverness to make B::Deparse use the correct |
| 327 |
indentation, since for some reason Data::Dumper doesn't (at the time of |
| 328 |
writing) do that. |
| 329 |
|
| 330 |
If you supply it yourself, it needs to be a single argument coderef - |
| 331 |
you could for example use "\&Data::Dumper::Dumper" though that would |
| 332 |
almost certainly be pointless. |
| 333 |
|
| 334 |
EXPORTS |
| 335 |
ddc |
| 336 |
use Data::Dumper::Compact 'ddc'; |
| 337 |
use Data::Dumper::Compact 'ddc' => \%options; |
| 338 |
|
| 339 |
If the first argument to "use"/"import()" is 'ddc', a subroutine "ddc()" |
| 340 |
is installed in the calling package which behaves like calling "dump". |
| 341 |
|
| 342 |
If the second argument is a hashref, it becomes the options passed to |
| 343 |
"new". |
| 344 |
|
| 345 |
This feature is effectively sugar over "dump_cb", in that: |
| 346 |
|
| 347 |
Data::Dumper::Compact->import(ddc => \%options) |
| 348 |
|
| 349 |
is equivalent to: |
| 350 |
|
| 351 |
*ddc = Data::Dumper::Compact->new(\%options)->dump_cb; |
| 352 |
|
| 353 |
METHODS |
| 354 |
new |
| 355 |
my $ddc = Data::Dumper::Compact->new; |
| 356 |
my $ddc = Data::Dumper::Compact->new(%options); |
| 357 |
my $ddc = Data::Dumper::Compact->new(\%options); |
| 358 |
|
| 359 |
Constructor. Takes a hash or hashref of "OPTIONS" |
| 360 |
|
| 361 |
dump |
| 362 |
my $formatted = Data::Dumper::Compact->dump($data, \%options?); |
| 363 |
|
| 364 |
my $formatted = $ddc->dump($data, \%merge_options?); |
| 365 |
|
| 366 |
This is the method you're going to want to call most of the time, and |
| 367 |
ties together the rest of the functionality into a single |
| 368 |
data-structure-to-string bundle. With just a data argument, it's |
| 369 |
equivalent to: |
| 370 |
|
| 371 |
$ddc->format( $ddc->transform( $ddc->transforms, $ddc->expand($data) ); |
| 372 |
|
| 373 |
In class method form, options provided are passed to "new"; in instance |
| 374 |
method form, options if provided are merged into $ddc just for this |
| 375 |
invocation. |
| 376 |
|
| 377 |
dump_cb |
| 378 |
my $cb = $ddc->dump_cb; |
| 379 |
|
| 380 |
Returns a subroutine reference that's a curried call to "dump": |
| 381 |
|
| 382 |
$cb->($data, \%extra_options); # equivalent to $ddc->dump(...) |
| 383 |
|
| 384 |
Mostly useful for if you want to create a custom "ddc()" like thing: |
| 385 |
|
| 386 |
use Data::Dumper::Compact; |
| 387 |
BEGIN { *Dumper = Data::Dumper::Compact->new->dump_cb } |
| 388 |
|
| 389 |
expand |
| 390 |
my $exp = $ddc->expand($data); |
| 391 |
|
| 392 |
Expands a data structure to DDC tagged data. The result is, recursively, |
| 393 |
|
| 394 |
[ $type, $payload ] |
| 395 |
|
| 396 |
where if $type is one of "string", "key", or "thing", the payload is a |
| 397 |
simple string ("thing" meaning something unknown and therefore delegated |
| 398 |
to "dumper"). If the type is an array: |
| 399 |
|
| 400 |
[ array => \@values ] |
| 401 |
|
| 402 |
and if the type is a hash: |
| 403 |
|
| 404 |
[ hash => [ \@keys, \%value_map ] ] |
| 405 |
|
| 406 |
where the keys provide an order for formatting, and the value map is a |
| 407 |
hashref of keys to expanded values. |
| 408 |
|
| 409 |
A plain string becomes a "string", unless it fits the "-foo" style |
| 410 |
pattern that autoquotes, in which case it becomes a "key". |
| 411 |
|
| 412 |
add_transform |
| 413 |
$ddc->add_transform(sub { ... }); |
| 414 |
$ddc->add_transform({ hash => sub { ... }, _ => sub { ... }); |
| 415 |
|
| 416 |
Appends a transform to "$ddc->transforms", see "transform" for |
| 417 |
behaviour. |
| 418 |
|
| 419 |
Returns $ddc to enable chaining. |
| 420 |
|
| 421 |
transform |
| 422 |
my $tf_exp = $ddc->transform($tfspec, $exp); |
| 423 |
|
| 424 |
Takes a transform specification and expanded tagged data and returns the |
| 425 |
transformed expanded expression. A transform spec is an arrayref |
| 426 |
containing transforms, where each transform is applied in order, so the |
| 427 |
last transform added via "add_transform" will be the last one to |
| 428 |
transform the data (each transform will consist of a datastructure |
| 429 |
representing which parts of the $exp tree it should be called for, plus |
| 430 |
subroutines representing the relevant transforms). |
| 431 |
|
| 432 |
Transform subroutines are called as a method on the $ddc with the |
| 433 |
arguments of "$type, $payload, $path" where $path is an arrayref of the |
| 434 |
keys/values of the containing hashes and arrays, aggregated as DDC |
| 435 |
descends through the $exp tree. |
| 436 |
|
| 437 |
Each transform is expected to return either nothing, to indicate it |
| 438 |
doesn't wish to modify the result, or a replacement expanded data |
| 439 |
structure. The simplest form of transform is a subref, which gets called |
| 440 |
for everything. |
| 441 |
|
| 442 |
So, to add ' IN MICE' to every string that's part of an array under a |
| 443 |
hash key called study_results, i.e.: |
| 444 |
|
| 445 |
my $data = { study_results => [ |
| 446 |
'Sense Of Touch Is Formed In the Brain Before Birth'. |
| 447 |
"We can't currently cure MS but a single cell could change that", |
| 448 |
] }; |
| 449 |
|
| 450 |
my $tf_exp = $ddc->transform([ sub { |
| 451 |
my ($self, $type, $payload, $path) = @_; |
| 452 |
return unless $type eq 'string' and ($path->[-2]||'') eq 'study_results'; |
| 453 |
return [ $type, $payload.' IN MICE' ]; |
| 454 |
} ], $ddc->expand($data)); |
| 455 |
|
| 456 |
will return: |
| 457 |
|
| 458 |
[ hash => [ |
| 459 |
[ 'study_results' ], |
| 460 |
{ study_results => [ array => [ |
| 461 |
[ string => 'Sense Of Touch Is Formed In the Brain Before Birth IN MICE' ], |
| 462 |
[ string => "We can't currently cure MS but a single cell could change that IN MICE", ], |
| 463 |
] ] } |
| 464 |
] ] |
| 465 |
|
| 466 |
If a hashref is found, then the values are expected to be transforms, |
| 467 |
and DDC will use "$hashref->{$type}||$hashref->{_}" as the transform, or |
| 468 |
skip if neither is present. So the previous example could be written as: |
| 469 |
|
| 470 |
$ddc->transform([ { string => sub { |
| 471 |
my ($self, $type, $payload, $path) = @_; |
| 472 |
return unless ($path->[-2]||'') eq 'study_results'; |
| 473 |
return [ $type, $payload.' IN MICE' ]; |
| 474 |
} } ], $ddc->expand($data)); |
| 475 |
|
| 476 |
If the value of the spec entry itself *or* the relevant hash value is an |
| 477 |
arrayref, it is assumed to contain a spec for trailing path entries, |
| 478 |
with the last element being the transform subroutine. A path entry match |
| 479 |
can be an exact scalar (tested via "eq" since it works fine for both |
| 480 |
strings and integer array indices), regexp, "undef" to indicate "any |
| 481 |
value is fine here", or a subroutine which will be called with the path |
| 482 |
entry as both $_[0] and $_. So the example we've been using could also |
| 483 |
be written as: |
| 484 |
|
| 485 |
$ddc->transform([ { string => [ |
| 486 |
'study_results', undef, |
| 487 |
sub { [ string => $_[2].' IN MICE' ] } |
| 488 |
] } ], $ddc->expand($data)); |
| 489 |
|
| 490 |
or |
| 491 |
|
| 492 |
$ddc->transform([ { string => [ |
| 493 |
qr/^study_results$/, sub { 1 }, |
| 494 |
sub { [ string => $_[2].' IN MICE' ] } |
| 495 |
] } ], $ddc->expand($data)); |
| 496 |
|
| 497 |
Note that while the $tfspec is not passed to transform subroutines, for |
| 498 |
the duration of the "transform" call the "transforms" option is |
| 499 |
localised to the provided routine, so |
| 500 |
|
| 501 |
sub { |
| 502 |
my ($self, $type, $payload, $path) = @_; |
| 503 |
my $tfspec = $self->transforms; |
| 504 |
... |
| 505 |
} |
| 506 |
|
| 507 |
will return the top level $tfspec passed to the transform call. |
| 508 |
|
| 509 |
Thanks to <http://twitter.com/justsaysinmice> for the inspiration. |
| 510 |
|
| 511 |
format |
| 512 |
my $formatted = $ddc->format($exp); |
| 513 |
|
| 514 |
Takes expanded tagged data and renders it to a formatted string, |
| 515 |
suitable for printing or warning or etc. |
| 516 |
|
| 517 |
Accepts the following type tags: "array", "list", "hash", "key", |
| 518 |
"string", "thing". Arrays and hashes are formatted as compactly as |
| 519 |
possible within the constraint of "max_width", but if overflow occurs |
| 520 |
then DDC falls back to spilling everything vertically, so newlines are |
| 521 |
used for most spacing and therefore it doesn't exceed the max width any |
| 522 |
more than strictly necessary. |
| 523 |
|
| 524 |
Strings are formatted as single quote if obvious, and double quote if |
| 525 |
not. |
| 526 |
|
| 527 |
Keys are treated as strings when present as hash values, but when an |
| 528 |
element of array values, are formatted ask "the_key =>" where possible. |
| 529 |
|
| 530 |
Lists are formatted as single line "qw()" expressions if possible, or "( |
| 531 |
... )" if not. |
| 532 |
|
| 533 |
Arrays and hashes are formatted in the manner to which one would hope |
| 534 |
readers are accustomed, except more compact. |
| 535 |
|
| 536 |
ALGORITHM |
| 537 |
The following is a description of the current algorithm of DDC. We |
| 538 |
reserve the right to change it for the better. |
| 539 |
|
| 540 |
If you didn't already read the overview examples in "WHY" do that first. |
| 541 |
|
| 542 |
Vertical mode means DDC has given up on fitting within the desired width |
| 543 |
and is now just trying to not use *too* much vertical space. |
| 544 |
|
| 545 |
Oneline mode is DDC testing to see if a single line rendering of |
| 546 |
something will fit within the available space. Things will often be |
| 547 |
rendered more than once since DDC is optimising for compact readable |
| 548 |
output rather than raw straight line performance. |
| 549 |
|
| 550 |
Top level formatting |
| 551 |
If something is formatted and the remaining width is zero or negative, |
| 552 |
DDC accepts default on "max_width" and bails out to a fully vertical |
| 553 |
approach so it overflows the desired width no more than necessary. |
| 554 |
|
| 555 |
Array formatting |
| 556 |
If already in vertical mode, formats one array element per line, |
| 557 |
appended with ",": |
| 558 |
|
| 559 |
[ |
| 560 |
1, |
| 561 |
2, |
| 562 |
3 |
| 563 |
] |
| 564 |
|
| 565 |
If in possible oneline mode, formats all but the last element according |
| 566 |
to the "Array element" rules, the last element according to normal |
| 567 |
formatting, and joins them with ' ' in the hopes this is narrow enough. |
| 568 |
Return this if oneline is forced or it fits: |
| 569 |
|
| 570 |
[ 1, 2, 3 ] |
| 571 |
|
| 572 |
If there's only a single internal member, tries to use the "Single entry |
| 573 |
formatting" strategy to cuddle it. |
| 574 |
|
| 575 |
[ [ |
| 576 |
<something inside> |
| 577 |
] ] |
| 578 |
|
| 579 |
Otherwise, attempts to bundle things as best possible: Each element is |
| 580 |
formatted according to the "Array element" rules, and multiple results |
| 581 |
are concatenated together onto a single line where that still remains |
| 582 |
within the available width. |
| 583 |
|
| 584 |
[ |
| 585 |
'foo', 'bar', 'baz', |
| 586 |
'red', 'white', 'blue', |
| 587 |
] |
| 588 |
|
| 589 |
Array element |
| 590 |
Elements are normally formatted as "$formatted.','" except if an element |
| 591 |
is of type "key" in which cases it becomes "$key =>". |
| 592 |
|
| 593 |
"whatever the smeg", |
| 594 |
smeg_off => |
| 595 |
|
| 596 |
List formatting |
| 597 |
The type "list" is synthetic and only introduced by transforms. |
| 598 |
|
| 599 |
It is formatted identically to an arrayref except with "( )" instead of |
| 600 |
"[ ]", with the exception that if it consists of only plain strings and |
| 601 |
will fit onto a single line, it formats as a "qw(x y x)" style list. |
| 602 |
|
| 603 |
qw(foo bar baz) |
| 604 |
( |
| 605 |
'foo', |
| 606 |
'bar', |
| 607 |
'baz', |
| 608 |
) |
| 609 |
|
| 610 |
Single entry formatting |
| 611 |
Where possible, a single entry will be cuddled such that the opening |
| •612 |
delimiters are both on the first line, and the closing delimiters both |
| 613 |
on the final line, to reduce the vertical space consumption of nested |
| 614 |
single entry array and/or hashrefs. |
| 615 |
|
| 616 |
to => { -select => { |
| 617 |
... |
| 618 |
} } |
| 619 |
|
| 620 |
[ 'SRV:8FB66F32' ], [ [ |
| 621 |
'/opt/voice-srvc-native/bin/async-srvc-att-gateway-poller', 33, |
| 622 |
'NERV::Voice::SRV::Native::AsyncSRVATTGatewayPoller::main', |
| 623 |
] ], |
| 624 |
|
| 625 |
Hash formatting |
| 626 |
If already in vertical mode, key/value pairs are formatted separated by |
| 627 |
newlines, with no attention paid to key length. |
| 628 |
|
| 629 |
{ |
| 630 |
foo => ..., |
| 631 |
bar => ..., |
| 632 |
} |
| 633 |
|
| 634 |
If potentially in oneline mode, key/value pairs are formatted separated |
| 635 |
by ', ' and the value is returned if forced or if remaining width allows |
| 636 |
the oneline rendering. |
| 637 |
|
| 638 |
{ foo => ..., bar => ... } |
| 639 |
|
| 640 |
Otherwise, all key/value pairs are formatted as "key => value" where |
| 641 |
possible, but if the first line of the value is too long, the value is |
| 642 |
moved to the next line and indented. |
| 643 |
|
| 644 |
key => 'shortvalue' |
| 645 |
key => |
| 646 |
'overlylongvalue' |
| 647 |
|
| 648 |
If there's only a single such key/value pair, tries to use the "Single |
| 649 |
entry formatting" strategy to cuddle it. |
| 650 |
|
| 651 |
{ zathrus => { |
| 652 |
listened_to => 0, |
| 653 |
} } |
| 654 |
|
| 655 |
Otherwise returns key/value pairs indented and separated by newlines |
| 656 |
|
| 657 |
{ |
| 658 |
foo => ..., |
| 659 |
bar => ..., |
| 660 |
} |
| 661 |
|
| 662 |
String formatting |
| 663 |
Uses single quotes if sure that's safe, double quotes otherwise. |
| 664 |
|
| 665 |
'foo bar baz quux' |
| 666 |
"could have been '' but nicer to not screw up\n the indents with a newline" |
| 667 |
|
| 668 |
Attempts to format a string within the available width, using multiple |
| 669 |
lines and the "." concatenation operator if necessary,. |
| 670 |
|
| 671 |
'this would be an' |
| 672 |
.'annoyingly long' |
| 673 |
.'string' |
| 674 |
|
| 675 |
The target width is set to 20 in vertical mode to try and not be too |
| 676 |
ugly. |
| 677 |
|
| 678 |
Object formatting |
| 679 |
Objects are tested to see if their underlying reference is an array or |
| 680 |
hash. If so, it's formatted with 'bless( ' prepended and ', $class)' |
| 681 |
appended. This so far appears to interact nicely with everything else. |
| 682 |
|
| 683 |
AUTHOR |
| 684 |
mst - Matt S Trout (cpan:MSTROUT) <[email protected]> |
| 685 |
|
| 686 |
CONTRIBUTORS |
| 687 |
None so far. |
| 688 |
|
| 689 |
COPYRIGHT |
| 690 |
Copyright (c) 2019 the Data::Dumper::Compact "AUTHOR" and "CONTRIBUTORS" |
| 691 |
as listed above. |
| 692 |
|
| 693 |
LICENSE |
| 694 |
This library is free software and may be distributed under the same |
| 695 |
terms as perl itself. See <https://dev.perl.org/licenses/>. |
| 696 |
|