Tersify.pm

filter: Statementstmt 99.3% 146 / 147 Branchbran 96.8% 62 / 64 Conditioncond 66.6% 12 / 18 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 94.2% 244 / 259 CC 46 SCAR 23.5
CC
46
Cov
96%
_tersify
15.0
_tersify_via_plugin
14.0
_tersify_object
13.0
CRAP 10.5
↑ Summary Tersify/perldb.pm »
1 package Data::Tersify;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @EXPORT_OK = qw(tersify tersify_many);
9  
10 # Have you updated the version number in the POD below?
11 2 our $VERSION = '1.003';
12 2 $VERSION = eval $VERSION;
13  
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Devel::OverloadInfo 0.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Module::Pluggable require => 1, force_search_all_paths => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Scalar::Util qw(blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Data::Tersify - generate terse equivalents of complex data structures
22  
23 =head1 VERSION
24  
25 This is version 1.003 of Data::Tersify.
26  
27 =head1 SYNOPSIS
28  
29 use Data::Dumper;
30 use Data::Tersify qw(tersify);
31
32 my $complicated_data_structure = ...;
33
34 print Dumper(tersify($complicated_data_structure));
35 # Your scrollback is not full of DateTime, DBIx::Class, Moose etc.
36 # spoor which you weren't interested in.
37  
38 =head1 DESCRIPTION
39  
40 Complex data structures are useful; necessary, even. But they're not
41 I<helpful>. In particular, when you're buried in the guts of some code
42 you don't fully understand and you have a variable you want to inspect,
43 and you say C<x $foo> in the debugger, or C<print STDERR Dumper($foo)> from
44 your code, or something very similar with the dumper module of your choice,
45 and you then get I<pages upon pages of unhelpful stuff> because C<$foo>
46 contained, I<somewhere> one or more references to a DateTime, DBIx::Class,
47 Moose or other verbose object ... you didn't need that.
48  
49 Data::Tersify looks at any data structure it's given, and if it finds a
50 blessed object that it knows about, anywhere, it replaces it in the data
51 structure by a terser equivalent, designed to (a) not use up all of your
52 scrollback, but (b) be blatantly clear that this is I<not> the original object
53 that was in that data structure originally, but a terser equivalent.
54  
55 Do not use Data::Tersify as part of any serialisation implementation! By
56 design, Data::Tersify is lossy and will throw away information! That's because
57 it supposes that that if you're using it, you want to dump information about a
58 complex data structure, and you don't I<care> about the fine details.
59  
60 If you find yourself saying C<x $foo> in the debugger a lot, consider adding
61 Data::Tersify::perldb to your .perldb file, or something like it.
62  
63 =head2 tersify
64  
65 In: $data_structure
66 In: $terser_data_structure
67  
68 Supplied with a data structure, returns a data structure with the complicated
69 bits summarised. Every attempt is made to preserve those parts of the data
70 structure that don't need summarising.
71  
72 Objects are only summarised if (1) they're blessed objects, (2) they're
73 not the root structure passed to tersify (so if you actually to want to dump a
74 complex DBIx::Class object, for instance, you still can), and (3) a
75 plugin has been registered that groks that type of object, I<or> they
76 contain as an element one such object.
77  
78 Summaries are either scalar references of the form "I<Classname> (I<refaddr>)
79 I<summary>", e.g. "DateTime (0xdeadbeef) 2017-08-15 12:34:56", blessed into the
80 Data::Tersify::Summary class, I<or> copies of the
81 object's internal state with any sub-objects tersified as above, blessed into
82 the Data::Tersify::Summary::I<Foo>::0xI<refaddr> class, where I<Foo> is the
83 class the object was originally blessed into and I<refaddr> the object's
84 original address.
85  
86 So, if you had the plugin Data::Tersify::Plugin::DateTime installed,
87 passing a DateTime object to tersify would return that same object, untouched;
88 but passing
89  
90 {
91 name => 'Now',
92 description => 'The time it currently is, not a time in the future',
93 datetime => DateTime->now
94 }
95  
96 to tersify would return something like this:
97  
98 {
99 name => 'Now',
100 description => 'The time it currently is, not a time in the future',
101 datetime => bless \"DateTime (0xdeadbeef) 2018-08-12 17:15:00",
102 "Data::Tersify::Summary",
103 }
104  
105 If the hashref had been blessed into the class "Time::Description",
106 and had a refaddr of 0xcafebabe, you would get back a hash as above, but
107 blessed into the class
108 C<Data::Tersify::Summary::Time::Description::0xcafebabe>.
109  
110 Note that point 2 above (objects aren't tersified if they're the root
111 structure) applies only to plugins. If the object contains other objects
112 that could be tersified, they will be. One design consequence of this is that
113 you should consider writing plugins for I<multiple types of object>, rather
114 than the ur-object that they might be part of.
115  
116 =cut
117  
118 2 my (%seen_refaddr, %refaddr_tersified_as, %safe_to_mess_with_refaddr);
119  
120 38 ▶sub tersify {
Subroutine tersify: called CC=2 SCAR=6.9
Pod documented
121 38 my ($data_structure) = @_;
122  
123 38 %seen_refaddr = ();
124 38 %refaddr_tersified_as = ();
125 38 %safe_to_mess_with_refaddr = ();
126 38 ($data_structure) = _tersify($data_structure);
127 38 ▶ while (%refaddr_tersified_as) {
Branch: 1 branch
BranchTrueFalseTotal
if (%refaddr_tersified_as) 23 38 61
128 23 my @known_refaddrs = keys %refaddr_tersified_as;
129 23 %seen_refaddr = ();
130 23 ($data_structure) = _tersify($data_structure);
131 23 delete @refaddr_tersified_as{@known_refaddrs};
132 }
133 38 return $data_structure;
134 }
135  
136 293 ▶sub _tersify {
Subroutine _tersify: called CC=15 SCAR=27.1
137 293 my ($data_structure) = @_;
138  
139 # If this is a data structure that we've tersified already, replace it.
140 293 ▶ if (my $terse_object = $refaddr_tersified_as{refaddr($data_structure)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $terse_object = $refaddr_tersified_as{builtin::refaddr($data_structure)}) 13 280 293
141 13 return ($terse_object, 1);
142 }
143  
144 # If this is a simple scalar, there's nothing to change.
145 280 ▶ if (!ref($data_structure)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data_structure) 64 216 280
146 64 return ($data_structure, 0);
147 }
148  
149 # If it's a reference to something, tersify *that* and take a reference
150 # to it.
151 216 ▶ if (ref($data_structure) eq 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data_structure eq "REF") 2 214 216
152 2 my $referenced_data_structure = $$data_structure;
153 2 my ($maybe_new_data_structure, $changed)
154 = _tersify($referenced_data_structure);
155 2 ▶ if (!$changed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($changed) 1 1 2
156 1 return ($data_structure, 0);
157 }
158 1 my $ref = \$maybe_new_data_structure;
159 1 return ($ref, 1);
160 }
161  
162 # Don't loop infinitely through a complex structure.
163 214 ▶ return ($data_structure, 0) if $seen_refaddr{refaddr($data_structure)}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_refaddr{builtin::refaddr($data_structure)}++ 14 200 214
164
165 # If this is a blessed object, see if we know how to tersify it.
166 200 ▶ if (blessed($data_structure)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data_structure)) 73 127 200
167 73 my ($object, $changed) = _tersify_object($data_structure);
168 73 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 35 38 73
169 35 $refaddr_tersified_as{refaddr($data_structure)} = $object;
170 }
171 73 return ($object, $changed);
172 }
173  
174 # For arrays and hashes, check if any of the elements changed, and if so
175 # create a fresh array or hash.
176 127 my $changed;
177 214 ▶ my $get_new_value = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
178 214 my ($old_value) = @_;
179 214 my ($new_value, $this_value_changed) = _tersify($old_value);
180 214 $changed += $this_value_changed;
181 214 ▶ return $this_value_changed ? $new_value : $old_value;
Branch: 1 branch
BranchTrueFalseTotal
$this_value_changed ? : 57 157 214
182 127 };
183 127 my $new_structure;
184 127 ▶ if (ref($data_structure) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data_structure eq 'ARRAY') { } 14 113 127
elsif (ref $data_structure eq 'HASH') { } 112 1 113
185 14 my @new_array;
186 14 for my $element (@$data_structure) {
187 55 push @new_array, $get_new_value->($element);
188 }
189 14 ▶ if (!$changed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($changed) 10 4 14
190 10 return ($data_structure, 0);
191 }
192 4 $new_structure = \@new_array;
193 } elsif (ref($data_structure) eq 'HASH') {
194 112 my %new_hash;
195 112 for my $key (keys %$data_structure) {
196 159 $new_hash{$key} = $get_new_value->($data_structure->{$key});
197 }
198 112 ▶ if (!$changed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($changed) 72 40 112
199 72 return ($data_structure, 0);
200 }
201 40 $new_structure = \%new_hash;
202 } else {
203 1 return ($data_structure, 0);
204 }
205  
206 # If it's safe to mess with the existing data structure (e.g. because this
207 # is the second pass, or later, that we've done through a data structure
208 # and this is an arrayref or hashref that we already anonymised earlier),
209 # just update its contents. Otherwise mark it as a new data structure.
210 44 ▶ if (!$safe_to_mess_with_refaddr{refaddr($data_structure)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $safe_to_mess_with_refaddr{builtin::refaddr($data_structure)}) { } 42 2 44
211 42 $refaddr_tersified_as{ refaddr($data_structure) } = $new_structure;
212 42 $safe_to_mess_with_refaddr{refaddr($new_structure)} = 1;
213 42 return ($new_structure, 1);
214 } else {
215 2 _replace_contents_of_structure_with($data_structure,
216 $new_structure);
217 2 return ($data_structure, 0);
218 }
219 }
220  
221 73 ▶sub _tersify_object {
Subroutine _tersify_object: called CC=13 SCAR=25.7
222 73 my ($data_structure) = @_;
223  
224 # A summary has, by definition, already been tersified.
225 73 ▶ if (ref($data_structure) eq 'Data::Tersify::Summary') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data_structure eq "Data::Tersify::Summary") 27 46 73
226 27 return ($data_structure, 0);
227 }
228  
229 # We might know how to tersify such an object directly, via a
230 # plugin.
231 46 my $terse_object = _tersify_via_plugin($data_structure);
232 46 ▶ my $changed = blessed($terse_object)
Condition66%2/3
builtin::blessed($terse_object) && $terse_object->isa('Data::Tersify::Summary')
!ll&&!rl&&r
01927
MC/DC50%1/2
builtin::blessed($terse_object) && $terse_object->isa('Data::Tersify::Summary')
builtin::blessed($terse_object) $terse_object->isa('Data::Tersify::Summary')
Truth table66%2/3
A && B
A= builtin::blessed($terse_object)B= $terse_object->isa('Data::Tersify::Summary')
ABresult
0X0
100
111
233 && $terse_object->isa('Data::Tersify::Summary');
234  
235 # OK, but does it overload stringification?
236 46 ▶ if (!$changed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($changed) 19 27 46
237 19 ▶ if (my $overload_info
Branch: 1 branch
BranchTrueFalseTotal
if (my $overload_info = Devel::OverloadInfo::overload_info($data_structure)) 19 0 19
238 = Devel::OverloadInfo::overload_info($data_structure))
239 {
240 19 ▶ if ($overload_info->{'""'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overload_info->{"\"\""}) 2 17 19
241 return (
242 2 _summarise_object_as_string(
243 $data_structure, "$data_structure"
244 ),
245 1
246 );
247 }
248 }
249 }
250  
251 # Although if this is the root structure passed to tersify, we want
252 # to pass it through as-is; we only tersify complicated objects
253 # that feature somewhere deeper in the data structure, possibly
254 # unexpectedly.
255 44 my ($caller_sub) = (caller(2))[3];
256 44 ▶ if ($changed && $caller_sub ne 'Data::Tersify::tersify') {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed and $caller_sub ne "Data::Tersify::tersify") 25 19 44
Condition100%3/3
$changed and $caller_sub ne "Data::Tersify::tersify"
!ll&&!rl&&r
17225
MC/DC100%2/2
$changed and $caller_sub ne "Data::Tersify::tersify"
$changed $caller_sub ne "Data::Tersify::tersify"
Truth table100%3/3
A and B
A= $changedB= $caller_sub ne "Data::Tersify::tersify"
ABresult
0X0
100
111
257 25 return ($terse_object, $changed);
258 }
259  
260 # If we didn't tersify this object, maybe we can tersify its internal
261 # structure?
262 19 my $object_contents;
263 19 ▶ if (reftype($data_structure) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($data_structure) eq 'HASH') { } 14 5 19
elsif (builtin::reftype($data_structure) eq 'ARRAY') { } 2 3 5
264 14 $object_contents = {%$data_structure};
265 } elsif (reftype($data_structure) eq 'ARRAY') {
266 2 $object_contents = [@$data_structure];
267 }
268 19 ▶ if ($object_contents) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_contents) 16 3 19
269 16 my $maybe_new_structure;
270 16 ($maybe_new_structure, $changed) = _tersify($object_contents);
271 16 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 8 8 16
272 # We might need to build a new Data::Tersify::Summary object.
273 8 ▶ if (!$safe_to_mess_with_refaddr{refaddr($data_structure)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $safe_to_mess_with_refaddr{builtin::refaddr($data_structure)}) { } 5 3 8
274 # No need to remember that we messed with $object_contents;
275 # that was a temporary variable we created purely to see if
276 # we could tersify it, and it's not referenced anywhere.
277 5 delete $refaddr_tersified_as{refaddr($object_contents)};
278 # Just create a new blessed object; the calling code will
279 # realise that we created a new object and update
280 # %refaddr_tersified_as with the proper values.
281 5 $terse_object = $maybe_new_structure;
282 5 bless $terse_object =>
283 sprintf('Data::Tersify::Summary::%s::0x%x',
284 ref($data_structure), refaddr($data_structure));
285 5 $safe_to_mess_with_refaddr{refaddr($terse_object)}++;
286 } else {
287 # We can reuse the existing one, which is now *even terser*!
288 # There's no danger of blatting existing data structures,
289 # because we've *already* replaced the previous data structure
290 # with one of ours, as part of generating a new object.
291 3 _replace_contents_of_structure_with($data_structure,
292 $maybe_new_structure);
293 }
294 8 return ($terse_object, $changed);
295 }
296 }
297  
298 # OK, return this object unchanged.
299 11 return ($data_structure, 0);
300 }
301  
302 5 ▶sub _replace_contents_of_structure_with {
Subroutine _replace_contents_of_structure_with: called CC=5 SCAR=17.8
303 5 my ($safe_structure, $new_contents) = @_;
304  
305 5 ▶ if (reftype($safe_structure) eq 'HASH'
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($safe_structure) eq 'HASH' and builtin::reftype($new_contents) eq 'HASH') { } 3 2 5
elsif (builtin::reftype($safe_structure) eq 'ARRAY' and builtin::reftype($new_contents) eq 'ARRAY') { } 2 0 2
Condition66%2/3
builtin::reftype($safe_structure) eq "HASH" and builtin::reftype($new_contents) eq "HASH"
!ll&&!rl&&r
203
MC/DC50%1/2
builtin::reftype($safe_structure) eq "HASH" and builtin::reftype($new_contents) eq "HASH"
builtin::reftype($safe_structure) eq "HASH" builtin::reftype($new_contents) eq "HASH"
Truth table66%2/3
A and B
A= builtin::reftype($safe_structure) eq "HASH"B= builtin::reftype($new_contents) eq "HASH"
ABresult
0X0
100
111
306 && reftype($new_contents) eq 'HASH')
307 {
308 3 %$safe_structure = %$new_contents;
309 ▶ } elsif (reftype($safe_structure) eq 'ARRAY'
Condition33%1/3
builtin::reftype($safe_structure) eq "ARRAY" and builtin::reftype($new_contents) eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::reftype($safe_structure) eq "ARRAY" and builtin::reftype($new_contents) eq "ARRAY"
builtin::reftype($safe_structure) eq "ARRAY" builtin::reftype($new_contents) eq "ARRAY"
Truth table33%1/3
A and B
A= builtin::reftype($safe_structure) eq "ARRAY"B= builtin::reftype($new_contents) eq "ARRAY"
ABresult
0X0
100
111
310 && reftype($new_contents) eq 'ARRAY')
311 {
312 2 @$safe_structure = @$new_contents;
313 } else {
314 0 croak sprintf(q{Want to put %s in existing %s, but that's a %s?!},
315 $new_contents, $safe_structure, reftype($new_contents));
316 }
317 }
318  
319 =head2 tersify_many
320  
321 In: @data_structures
322 Out: @terser_data_structures
323  
324 A simple wrapper around L</tersify> that expects to be passed one or more
325 variables. Note that as each value is passed to L</tersify>, none of the values
326 in @data_structures will be tersified if they're objects recognised by plugins.
327 (Whereas they would have been if you'd said C<tersify(\@data_structures)>.
328  
329 This is intended to be used by e.g. the Perl debugger's x command.
330  
331 =cut
332  
333 1 ▶sub tersify_many {
Subroutine tersify_many: called CC=1 SCAR=0.0
Pod documented
334 1 return map { tersify($_) } @_;
335 }
336  
337 =head2 PLUGINS
338  
339 Data::Tersify can be extended by plugins. See L<Data::Tersify::Plugin> for
340 a general description of plugins; for examples of plugins, see
341 L<Data::Tersify::Plugin::DateTime> and L<Data::Tersify::Plugin::DBIx::Class>,
342 provided in separate distributions.
343  
344 =cut
345  
346 {
347 2 my (%handled_by_plugin, %plugin_handles_subclasses);
348 2 my $subclass_lookup_initialised;
349  
350 46 ▶ sub _tersify_via_plugin {
Subroutine _tersify_via_plugin: called CC=14 SCAR=26.4
351 46 my ($object) = @_;
352  
353 # A simple lookup of "this type of object is handled by this class".
354 # If the plugin doesn't handle subclasses of objects, this is fine.
355 46 ▶ if (!keys %handled_by_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %handled_by_plugin) 1 45 46
356 1 for my $plugin (plugins()) {
357 3 for my $class ($plugin->handles) {
358 4 $handled_by_plugin{$class} = $plugin;
359 }
360 }
361 }
362  
363 # A more complex lookup of "this class handles subclasses".
364 46 ▶ if (!$subclass_lookup_initialised) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subclass_lookup_initialised) 1 45 46
365 1 for my $plugin (plugins()) {
366 3 ▶ if ( $plugin->can('handles_subclasses')
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->can("handles_subclasses") and $plugin->handles_subclasses) 1 2 3
Condition66%2/3
$plugin->can("handles_subclasses") and $plugin->handles_subclasses
!ll&&!rl&&r
201
MC/DC50%1/2
$plugin->can("handles_subclasses") and $plugin->handles_subclasses
$plugin->can("handles_subclasses") $plugin->handles_subclasses
Truth table66%2/3
A and B
A= $plugin->can("handles_subclasses")B= $plugin->handles_subclasses
ABresult
0X0
100
111
367 && $plugin->handles_subclasses)
368 {
369 1 $plugin_handles_subclasses{$plugin} = [ $plugin->handles ];
370 }
371 }
372 1 $subclass_lookup_initialised = 1;
373 }
374  
375 # With that in mind, look for a plugin that handles this object
376 # one way or another.
377 46 my $chosen_plugin = $handled_by_plugin{ref($object)};
378 46 ▶ if (!$chosen_plugin && keys %plugin_handles_subclasses) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $chosen_plugin and keys %plugin_handles_subclasses) 21 25 46
Condition66%2/3
not $chosen_plugin and keys %plugin_handles_subclasses
!ll&&!rl&&r
25021
MC/DC50%1/2
not $chosen_plugin and keys %plugin_handles_subclasses
not $chosen_plugin keys %plugin_handles_subclasses
Truth table66%2/3
not(A) and B
A= not $chosen_pluginB= keys %plugin_handles_subclasses
ABresult
0X0
100
111
379 plugin:
380 21 for my $plugin (sort keys %plugin_handles_subclasses) {
381 21 for my $class (@{ $plugin_handles_subclasses{$plugin} }) {
382 21 ▶ if ($object->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->isa($class)) 2 19 21
383 2 $chosen_plugin = $plugin;
384 2 last plugin;
385 }
386 }
387 }
388 }
389  
390 # And use it to summarise the object if we can.
391 46 ▶ if ($chosen_plugin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chosen_plugin) 27 19 46
392 27 return _summarise_object_as_string($object,
393 $chosen_plugin->tersify($object));
394 }
395 19 return $object;
396 }
397 }
398  
399 2 ▶sub _summarise_object_as_string {
Subroutine _summarise_object_as_string: called CC=1 SCAR=0.0
400 29 my ($object, $string) = @_;
401 29 my $summary
402 = sprintf('%s (0x%x) %s', ref($object), refaddr($object), $string);
403 29 return bless \$summary => 'Data::Tersify::Summary';
404 }
405  
406 =head1 LICENSE
407  
408 This is free software; you can redistribute it and/or modify it under the same
409 terms as Perl 5.
410  
411 =head1 BUGS
412  
413 If you find any bugs, or have any feature suggestions, please report them
414 via L<github|https://github.com/skington/data-tersify/issues>.
415  
416 =head1 SEE ALSO
417  
418 L<Data::Printer> will tersify data structures as part of its standard
419 output.
420  
421 =cut
422  
423 2 1;
↑ Summary Tersify/perldb.pm »