-
Notifications
You must be signed in to change notification settings - Fork 0
/
Partial.pm
208 lines (145 loc) · 5.64 KB
/
Partial.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package Data::Dump::Partial;
# ABSTRACT: Dump data structure compactly and potentially partially
use 5.010;
use strict;
use warnings;
use Data::Dump::Filtered;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(dump_partial dumpp);
=head1 SYNOPSIS
use Data::Dump::Partial qw(dump_partial dumpp);
dump_partial([1, "some long string", 3, 4, 5, 6, 7]);
# prints something like: [1, "some long st...", 3, 4, 5, ...]
# specify options
dump_partial($data, $more_data, {max_total_len => 50, max_keys => 4});
=head1 DESCRIPTION
=cut
=head1 FUNCTIONS
=head2 dump_partial(..., $opts)
Dump one more data structures compactly and potentially
partially. Uses L<Data::Dump::Filtered> as the backend. By compactly,
it means all indents and comments and newlines are removed, so the
output all fits in one line. By partially, it means only a certain
number of scalar length, array elements, hash keys are showed.
$opts is a hashref, optional only when there is one data to dump, with
the following known keys:
=over 4
=item * max_total_len => NUM
Total length of output before it gets truncated with an
ellipsis. Default is 80.
=item * max_len => NUM
Maximum length of a scalar (string, etc) to show before the rest get
truncated with an ellipsis. Default is 32.
=item * max_keys => NUM
Number of key pairs of a hash to show before the rest get truncated
with an ellipsis. Default is 5.
=item * max_elems => NUM
Number of elements of an array to show before the rest get truncated
with an ellipsis. Default is 5.
=item * precious_keys => [KEY, ...]
Never truncate these keys (even if it results in max_keys limit being
exceeded).
=item * worthless_keys => [KEY, ...]
When needing to truncate hash keys, search for these first.
=item * hide_keys => [KEY, ...]
Always truncate these hash keys, no matter what. This is actually also
implemented by Data::Dump::Filtered.
=item * dd_filter => \&sub
If you have other Data::Dump::Filtered filter you want to execute, you
can pass it here.
=back
=cut
sub dump_partial {
my @data = @_;
my $opts = (@data > 1) ? {%{pop(@data)}} : {};
$opts->{max_keys} //= 5;
$opts->{max_elems} //= 5;
$opts->{max_len} //= 32;
$opts->{max_total_len} //= 80;
$opts->{max_keys} = @{$opts->{precious_keys}} if $opts->{precious_keys} &&
@{ $opts->{precious_keys} } > $opts->{max_keys};
my $out;
if ($opts->{_inner}) {
#print "DEBUG: inner dump\n";
$out = Data::Dump::dump(@data);
} else {
#print "DEBUG: outer dump\n";
my $filter = sub {
my ($ctx, $oref) = @_;
if ($opts->{max_len} && $ctx->is_scalar && defined($$oref) &&
length($$oref) > $opts->{max_len}) {
return { object => substr($$oref, 0, $opts->{max_len}-3)."..." };
} elsif ($opts->{max_elems} && $ctx->is_array &&
@$oref > $opts->{max_elems}) {
#print "DEBUG: truncating array\n";
my @ary = @{$oref}[0..($opts->{max_elems}-1)];
local $opts->{_inner} = 1;
local $opts->{max_total_len} = 0;
my $out = dump_partial(\@ary, $opts);
$out =~ s/(?:, )?]$/, ...]/;
return { dump => $out };
} elsif ($opts->{max_keys} && $ctx->is_hash &&
keys(%$oref) > $opts->{max_keys}) {
#print "DEBUG: truncating hash\n";
my %hash = %$oref;
my $mk = $opts->{max_keys};
{
if ($opts->{hide_keys}) {
for (keys %hash) {
delete $hash{$_} if $_ ~~ @{$opts->{hide_keys}};
}
}
last if keys(%hash) <= $mk;
if ($opts->{worthless_keys}) {
for (keys %hash) {
last if keys(%hash) <= $mk;
delete $hash{$_} if $_ ~~ @{$opts->{worthless_keys}};
}
}
last if keys(%hash) <= $mk;
for (keys %hash) {
delete $hash{$_} if !$opts->{precious_keys} ||
!($_ ~~ @{$opts->{precious_keys}});
last if keys(%hash) <= $mk;
}
}
local $opts->{_inner} = 1;
local $opts->{max_total_len} = 0;
my $out = dump_partial(\%hash, $opts);
$out =~ s/(?:, )? }$/, ... }/;
return { dump => $out };
} elsif ($opts->{dd_filter}) {
return $opts->{dd_filter}->($ctx, $oref);
} else {
return;
}
};
$out = Data::Dump::Filtered::dump_filtered(@data, $filter);
}
for ($out) {
s/^\s*#.*//mg; # comments
s/^\s+//mg; # indents
s/\n+/ /g; # newlines
}
if ($opts->{max_total_len} && length($out) > $opts->{max_total_len}) {
$out = substr($out, 0, $opts->{max_total_len}-3) . "...";
}
print STDERR "$out\n" unless defined wantarray;
$out;
}
1;
=head2 dumpp
An alias for dump_filtered().
=cut
sub dumpp { dump_partial(@_) }
=head1 FAQ
=head2 What is the point/purpose of this module?
Sometimes you want to dump a data structure, but need it to be short,
more than need it to be complete, for example when logging to log
files or database.
=head2 Is the dump result eval()-able? Will the dump result eval() to produce the original data?
Sometimes it is/will, sometimes it does/will not if it gets truncated.
=head1 SEE ALSO
L<Data::Dump::Filtered>
=cut