Data/Printer/Object.pm

filter: Statementstmt 88.3% 370 / 419 Branchbran 66.6% 116 / 174 Conditioncond 53.8% 56 / 104 MC/DCmcdc 35.0% 20 / 57 Subroutinesub 96.8% 62 / 64 Podpod 100% 15 / 15 totaltotal 76.7% 639 / 833 CC 131 SCAR 31.1
CC
131
Cov
78%
_detect_color_level
109.1
_check_memsize
73.6
__ANON__
56.0
CRAP 22.4
« Data/Printer/Config.pm ↑ Summary Data/Printer/Filter/HASH.pm »
1 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 35 ▶use Data::Printer::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package # hide from pause
6 Data::Printer::Object::ClassOptions;
7 24 ▶ sub parents { $_[0]->{'parents'} }
Subroutine parents: called CC=1 SCAR=0.0
8 112 ▶ sub linear_isa { $_[0]->{'linear_isa'} }
Subroutine linear_isa: called CC=1 SCAR=0.0
9 151 ▶ sub universal { $_[0]->{'universal'} }
Subroutine universal: called CC=1 SCAR=0.0
10 92 ▶ sub expand { $_[0]->{'expand'} }
Subroutine expand: called CC=1 SCAR=0.0
11 52 ▶ sub stringify { $_[0]->{'stringify'} }
Subroutine stringify: called CC=1 SCAR=0.0
12 47 ▶ sub show_reftype { $_[0]->{'show_reftype'} }
Subroutine show_reftype: called CC=1 SCAR=0.0
13 39 ▶ sub show_overloads { $_[0]->{'show_overloads'} }
Subroutine show_overloads: called CC=1 SCAR=0.0
14 116 ▶ sub show_methods { $_[0]->{'show_methods'} }
Subroutine show_methods: called CC=1 SCAR=0.0
15 70 ▶ sub sort_methods { $_[0]->{'sort_methods'} }
Subroutine sort_methods: called CC=1 SCAR=0.0
16 0 ▶ sub show_wrapped { $_[0]->{'show_wrapped'} }
Subroutine show_wrapped: not called CC=1 SCAR=6.9
17 38 ▶ sub inherited { $_[0]->{'inherited'} }
Subroutine inherited: called CC=1 SCAR=0.0
18 73 ▶ sub format_inheritance { $_[0]->{'format_inheritance'} }
Subroutine format_inheritance: called CC=1 SCAR=0.0
19 100 ▶ sub parent_filters { $_[0]->{'parent_filters'} }
Subroutine parent_filters: called CC=1 SCAR=0.0
20 47 ▶ sub internals { $_[0]->{'internals'} }
Subroutine internals: called CC=1 SCAR=0.0
21 253 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
22 253 my ($class, $params) = @_;
23 253 my $self = {
24 'linear_isa' => Data::Printer::Common::_fetch_scalar_or_default($params, 'linear_isa', 'auto'),
25 'show_reftype' => Data::Printer::Common::_fetch_scalar_or_default($params, 'show_reftype', 0),
26 'show_overloads' => Data::Printer::Common::_fetch_scalar_or_default($params, 'show_overloads', 1),
27 'stringify' => Data::Printer::Common::_fetch_scalar_or_default($params, 'stringify', 1),
28 'expand' => Data::Printer::Common::_fetch_scalar_or_default($params, 'expand', 1),
29 'show_methods' => Data::Printer::Common::_fetch_anyof(
30 $params, 'show_methods', 'all', [qw(none all private public)]
31 ),
32 'inherited' => Data::Printer::Common::_fetch_anyof(
33 $params, 'inherited', 'public', [qw(none all private public)]
34 ),
35 'format_inheritance' => Data::Printer::Common::_fetch_anyof(
36 $params, 'format_inheritance', 'lines', [qw(string lines)]
37 ),
38 'parent_filters' => Data::Printer::Common::_fetch_scalar_or_default($params, 'parent_filters', 1),
39 'universal' => Data::Printer::Common::_fetch_scalar_or_default($params, 'universal', 0),
40 'sort_methods' => Data::Printer::Common::_fetch_scalar_or_default($params, 'sort_methods', 1),
41 'show_wrapped' => Data::Printer::Common::_fetch_scalar_or_default($params, 'show_wrapped', 1),
42 'internals' => Data::Printer::Common::_fetch_scalar_or_default($params, 'internals', 1),
43 'parents' => Data::Printer::Common::_fetch_scalar_or_default($params, 'parents', 1),
44 };
45 253 return bless $self, $class;
46 }
47 35 1;
48  
49 package Data::Printer::Object;
50 35 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51 35 ▶use Data::Printer::Theme;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 35 ▶use Data::Printer::Filter::SCALAR; # also implements LVALUE
Subroutine BEGIN: called CC=1 SCAR=0.0
53 35 ▶use Data::Printer::Filter::ARRAY;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 35 ▶use Data::Printer::Filter::HASH;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 35 ▶use Data::Printer::Filter::REF;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 35 ▶use Data::Printer::Filter::VSTRING;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 35 ▶use Data::Printer::Filter::GLOB;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 35 ▶use Data::Printer::Filter::FORMAT;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 35 ▶use Data::Printer::Filter::Regexp;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 35 ▶use Data::Printer::Filter::CODE;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 35 ▶use Data::Printer::Filter::OBJECT;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 35 ▶use Data::Printer::Filter::GenericClass;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 # create our basic accessors:
65 35 my @method_names =qw(
66 name show_tainted show_unicode show_readonly show_lvalue show_refcount
67 show_memsize memsize_unit print_escapes scalar_quotes escape_chars
68 caller_info caller_message caller_message_newline caller_message_position
69 string_max string_overflow string_preserve resolve_scalar_refs
70 array_max array_overflow array_preserve hash_max hash_overflow
71 hash_preserve unicode_charnames colored theme show_weak
72 max_depth index separator end_separator class_method class hash_separator
73 align_hash sort_keys quote_keys deparse return_value show_dualvar show_tied
74 warnings arrows coderef_stub coderef_undefined
75 );
76 35 foreach my $method_name (@method_names) {
77 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1645 ▶ *{__PACKAGE__ . "::$method_name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
79 10577 ▶ $_[0]->{$method_name} = $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 193 10384 10577
80 10577 return $_[0]->{$method_name};
81 }
82 1645 }
83 188 ▶sub extra_config { $_[0]->{extra_config} }
Subroutine extra_config: called CC=1 SCAR=0.0
Pod documented
84  
85 33 ▶sub current_depth { $_[0]->{_depth} }
Subroutine current_depth: called CC=1 SCAR=0.0
Pod documented
86 249 ▶sub indent { $_[0]->{_depth}++ }
Subroutine indent: called CC=1 SCAR=0.0
Pod documented
87 248 ▶sub outdent { $_[0]->{_depth}-- }
Subroutine outdent: called CC=1 SCAR=0.0
Pod documented
88  
89 691 ▶sub newline {
Subroutine newline: called CC=1 SCAR=0.0
Pod documented
90 691 my ($self) = @_;
91 return $self->{_linebreak}
92 . (' ' x ($self->{_depth} * $self->{_current_indent}))
93 . (' ' x $self->{_array_padding})
94 691 ;
95 }
96  
97 1574 ▶sub current_name {
Subroutine current_name: called CC=3 SCAR=11.0
Pod documented
98 1574 my ($self, $new_value) = @_;
99 1574 ▶ if (defined $new_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) { } 612 962 1574
100 612 $self->{_current_name} = $new_value;
101 }
102 else {
103 962 ▶ $self->{_current_name} = $self->name unless defined $self->{_current_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_current_name"} 243 719 962
104 }
105 1574 return $self->{_current_name};
106 }
107  
108 253 ▶sub _init {
Subroutine _init: called CC=6 SCAR=17.9
109 253 my $self = shift;
110 253 ▶ my $props = { @_ == 1 ? %{$_[0]} : @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 64 189 253
111  
112 253 $self->{'_linebreak'} = "\n";
113 253 $self->{'_depth'} = 0;
114 253 $self->{'_position'} = 0; # depth is for indentation only!
115 253 $self->{'_array_padding'} = 0;
116 253 $self->{'_seen'} = {};
117 253 $self->{_refcount_base} = 3;
118 253 $self->{'warnings'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'warning', 1);
119 253 $self->{'indent'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'indent', 4);
120 253 $self->{'index'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'index', 1);
121 253 $self->{'name'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'name', 'var');
122 253 $self->{'arrows'} = Data::Printer::Common::_fetch_anyof(
123 $props,
124 'arrows',
125 'none',
126 [qw(none first all)]
127 );
128  
129 253 $self->{'show_tainted'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_tainted', 1);
130 253 $self->{'show_tied'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_tied', 1);
131 253 $self->{'show_weak'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_weak', 1);
132 253 $self->{'show_unicode'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_unicode', 0);
133 253 $self->{'show_readonly'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_readonly', 1);
134 253 $self->{'show_lvalue'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_lvalue', 1);
135 253 $self->{'show_refcount'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_refcount', 0);
136 253 $self->{'show_memsize'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_memsize', 0);
137 253 $self->{'memsize_unit'} = Data::Printer::Common::_fetch_anyof(
138 $props,
139 'memsize_unit',
140 'auto',
141 [qw(auto b k m)]
142 );
143 253 $self->{'print_escapes'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'print_escapes', 0);
144 253 $self->{'scalar_quotes'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'scalar_quotes', q("));
145 253 $self->{'escape_chars'} = Data::Printer::Common::_fetch_anyof(
146 $props,
147 'escape_chars',
148 'none',
149 [qw(none nonascii nonlatin1 all)]
150 );
151 253 $self->{'caller_info'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'caller_info', 0);
152 253 $self->{'caller_message'} = Data::Printer::Common::_fetch_scalar_or_default(
153 $props,
154 'caller_message',
155 'Printing in line __LINE__ of __FILENAME__:'
156 );
157 253 $self->{'caller_message_newline'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'caller_message_newline', 1);
158 253 $self->{'caller_message_position'} = Data::Printer::Common::_fetch_anyof($props, 'caller_message_position', 'before', [qw(before after)]);
159 253 $self->{'resolve_scalar_refs'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'resolve_scalar_refs', 0);
160 253 $self->{'string_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'string_max', 4096);
161 253 $self->{'string_preserve'} = Data::Printer::Common::_fetch_anyof(
162 $props,
163 'string_preserve',
164 'begin',
165 [qw(begin end middle extremes none)]
166 );
167 253 $self->{'string_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
168 $props,
169 'string_overflow',
170 '(...skipping __SKIPPED__ chars...)'
171 );
172 253 $self->{'array_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'array_max', 100);
173 253 $self->{'array_preserve'} = Data::Printer::Common::_fetch_anyof(
174 $props,
175 'array_preserve',
176 'begin',
177 [qw(begin end middle extremes none)]
178 );
179 253 $self->{'array_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
180 $props,
181 'array_overflow',
182 '(...skipping __SKIPPED__ items...)'
183 );
184 253 $self->{'hash_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'hash_max', 100);
185 253 $self->{'hash_preserve'} = Data::Printer::Common::_fetch_anyof(
186 $props,
187 'hash_preserve',
188 'begin',
189 [qw(begin end middle extremes none)]
190 );
191 253 $self->{'hash_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
192 $props,
193 'hash_overflow',
194 '(...skipping __SKIPPED__ keys...)'
195 );
196 253 $self->{'unicode_charnames'} = Data::Printer::Common::_fetch_scalar_or_default(
197 $props,
198 'unicode_charnames',
199 0
200 );
201 253 $self->{'colored'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'colored', 'auto');
202 253 $self->{'max_depth'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'max_depth', 0);
203 253 $self->{'separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'separator', ',');
204 253 $self->{'end_separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'end_separator', 0);
205 253 $self->{'class_method'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'class_method', '_data_printer');
206 253 $self->{'class'} = Data::Printer::Object::ClassOptions->new($props->{'class'});
207 253 $self->{'hash_separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'hash_separator', ' ');
208 253 $self->{'align_hash'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'align_hash', 1);
209 253 $self->{'sort_keys'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'sort_keys', 1);
210 253 $self->{'quote_keys'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'quote_keys', 'auto');
211 253 $self->{'deparse'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'deparse', 0);
212 253 $self->{'coderef_stub'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'coderef_stub', 'sub { ... }');
213 253 $self->{'coderef_undefined'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'coderef_undefined', '<undefined coderef>');
214 253 $self->{'return_value'} = Data::Printer::Common::_fetch_anyof(
215 $props,
216 'return_value',
217 'pass',
218 [qw(pass dump void)]
219 );
220 253 $self->{'show_dualvar'} = Data::Printer::Common::_fetch_anyof(
221 $props,
222 'show_dualvar',
223 'lax',
224 [qw(lax strict off)]
225 );
226  
227 253 ▶ if (exists $props->{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $props->{"as"}) 1 252 253
228 1 my $msg = Data::Printer::Common::_fetch_scalar_or_default($props, 'as', '');
229 1 $self->{caller_info} = 1;
230 1 $self->{caller_message} = $msg;
231 }
232  
233 $self->multiline(
234 253 Data::Printer::Common::_fetch_scalar_or_default($props, 'multiline', 1)
235 );
236  
237 253 $self->fulldump(
238 Data::Printer::Common::_fetch_scalar_or_default($props, 'fulldump', 0)
239 );
240  
241 253 ▶ $self->output(defined $props->{output} ? $props->{output} : 'stderr');
Branch: 1 branch
BranchTrueFalseTotal
defined $props->{'output'} ? : 7 246 253
242 253 $self->_load_colors($props);
243 253 $self->_load_filters($props);
244  
245 253 my %extra_config;
246 253 my %core_options = map { $_ => 1 }
247 (@method_names, qw(as multiline output colors filters));
248 253 foreach my $key (keys %$props) {
249 781 ▶ $extra_config{$key} = $props->{$key} unless exists $core_options{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $core_options{$key} 20 761 781
250 }
251 253 $self->{extra_config} = \%extra_config;
252  
253 253 return $self;
254 }
255  
256 257 ▶sub output {
Subroutine output: called CC=2 SCAR=6.9
Pod documented
257 257 my ($self, $new_output) = @_;
258 257 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 253 4 257
259 253 $self->_load_output_handle($new_output);
260 }
261 257 return $self->{output};
262 }
263  
264 253 ▶sub _load_output_handle {
Subroutine _load_output_handle: called CC=11 SCAR=24.7
265 253 my ($self, $output) = @_;
266 253 my %targets = ( stdout => *STDOUT, stderr => *STDERR );
267 253 my $error;
268 253 my $ref = ref $output;
269 253 ▶ if (!$ref and exists $targets{ lc $output }) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref and exists $targets{lc $output}) { } 248 5 253
elsif ($ref and $ref eq 'GLOB' or not $ref and (\$output) =~ /GLOB\([^()]+\)$/) { } 3 2 5
elsif (not $ref or $ref eq 'SCALAR') { } 2 0 2
Condition100%3/3
not $ref and exists $targets{lc $output}
!ll&&!rl&&r
32248
MC/DC100%2/2
not $ref and exists $targets{lc $output}
not $ref exists $targets{lc $output}
Truth table100%3/3
not(A) and B
A= not $refB= exists $targets{lc $output}
ABresult
0X0
100
111
270 248 $self->{output} = lc $output;
271 248 $self->{output_handle} = $targets{ $self->{output} };
272 }
273 ▶ elsif ( ( $ref and $ref eq 'GLOB')
Condition100%9/9
$ref and $ref eq "GLOB" or not $ref and (\$output) =~ /GLOB\([^()]+\)$/
l!l&&r!l&&!r
212
$ref and $ref eq "GLOB"
!ll&&!rl&&r
212
not $ref and (\$output) =~ /GLOB\([^()]+\)$/
!ll&&!rl&&r
111
MC/DC75%3/4
$ref and $ref eq "GLOB" or not $ref and (\$output) =~ /GLOB\([^()]+\)$/
$ref $ref eq "GLOB" not $ref (\$output) =~ /GLOB\([^()]+\)$/
Truth table57%4/7
A and B or not(C) and D
A= $refB= $ref eq "GLOB"C= not $refD= (\$output) =~ /GLOB\([^()]+\)$/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
274 or (!$ref and \$output =~ /GLOB\([^()]+\)$/)
275 ) {
276 3 $self->{output} = 'handle';
277 3 $self->{output_handle} = $output;
278 }
279 ▶ elsif (!$ref or $ref eq 'SCALAR') {
Condition66%2/3
not $ref or $ref eq "SCALAR"
l!l&&r!l&&!r
110
MC/DC0%0/2
not $ref or $ref eq "SCALAR"
not $ref $ref eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not $refB= $ref eq "SCALAR"
ABresult
1X1
011
000
280 2 ▶ if (open my $fh, '>>', $output) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>>', $output) { } 2 0 2
281 2 $self->{output} = 'file';
282 2 $self->{output_handle} = $fh;
283 }
284 else {
285 0 $error = "file '$output': $!";
286 }
287 }
288 else {
289 0 $error = 'unknown output data';
290 }
291 253 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 253 253
292 0 Data::Printer::Common::_warn($self, "error opening custom output handle: $error");
293 0 $self->{output_handle} = $targets{'stderr'}
294 }
295 253 return;
296 }
297  
298 253 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
299 253 my $class = shift;
300 253 my $self = bless {}, $class;
301 253 return $self->_init(@_);
302 }
303  
304 324 ▶sub multiline {
Subroutine multiline: called CC=8 SCAR=21.0
Pod documented
305 324 my ($self, $value) = @_;
306 324 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 253 71 324
307 253 $self->{multiline} = !!$value;
308 253 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 216 37 253
309 216 $self->{_linebreak} = "\n";
310 216 $self->{_current_indent} = $self->{indent};
311 $self->index( $self->{_original_index} )
312 216 ▶ if exists $self->{_original_index};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_index"} 0 216 216
313 $self->hash_separator( $self->{_original_separator} )
314 216 ▶ if exists $self->{_original_separator};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_separator"} 0 216 216
315 $self->array_overflow( $self->{_original_array_overflow} )
316 216 ▶ if exists $self->{_original_array_overflow};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_array_overflow"} 0 216 216
317 $self->hash_overflow( $self->{_original_hash_overflow} )
318 216 ▶ if exists $self->{_original_hash_overflow};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_hash_overflow"} 0 216 216
319 $self->string_overflow( $self->{_original_string_overflow} )
320 216 ▶ if exists $self->{_original_string_overflow};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_string_overflow"} 0 216 216
321 }
322 else {
323 37 $self->{_original_index} = $self->index;
324 37 $self->index(0);
325 37 $self->{_original_separator} = $self->hash_separator;
326 37 $self->hash_separator(':');
327 37 $self->{_original_array_overflow} = $self->array_overflow;
328 37 $self->array_overflow('(...)');
329 37 $self->{_original_hash_overflow} = $self->hash_overflow;
330 37 $self->hash_overflow('(...)');
331 37 $self->{_original_string_overflow} = $self->string_overflow;
332 37 $self->string_overflow('(...)');
333 37 $self->{_linebreak} = ' ';
334 37 $self->{_current_indent} = 0;
335 }
336 }
337 324 return $self->{multiline};
338 }
339  
340 253 ▶sub fulldump {
Subroutine fulldump: called CC=6 SCAR=23.0
Pod documented
341 253 my ($self, $value) = @_;
342 253 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 253 0 253
343 253 $self->{fulldump} = !!$value;
344 253 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 253 253
345 0 $self->{_original_string_max} = $self->string_max;
346 0 $self->string_max(0);
347 0 $self->{_original_array_max} = $self->array_max;
348 0 $self->array_max(0);
349 0 $self->{_original_hash_max} = $self->hash_max;
350 0 $self->hash_max(0);
351 }
352 else {
353 $self->string_max($self->{_original_string_max})
354 253 ▶ if exists $self->{_original_string_max};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_string_max"} 0 253 253
355 $self->array_max($self->{_original_array_max})
356 253 ▶ if exists $self->{_original_array_max};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_array_max"} 0 253 253
357 $self->hash_max($self->{_original_hash_max})
358 253 ▶ if exists $self->{_original_hash_max};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_original_hash_max"} 0 253 253
359 }
360 }
361 }
362  
363 253 ▶sub _load_filters {
Subroutine _load_filters: called CC=11 SCAR=26.4
364 253 my ($self, $props) = @_;
365  
366 # load our core filters (LVALUE is under the 'SCALAR' filter module)
367 253 my @core_filters = qw(SCALAR ARRAY HASH REF VSTRING GLOB FORMAT Regexp CODE OBJECT GenericClass);
368 253 foreach my $class (@core_filters) {
369 2783 $self->_load_external_filter($class);
370 }
371 253 my @filters;
372 # load any custom filters provided by the user
373 253 ▶ if (exists $props->{filters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $props->{"filters"}) 44 209 253
374 44 ▶ if (ref $props->{filters} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $props->{'filters'} eq 'HASH') { } 0 44 44
elsif (ref $props->{'filters'} eq 'ARRAY') { } 44 0 44
375 0 Data::Printer::Common::_warn(
376 $self,
377 'please update your code: filters => { ... } is now filters => [{ ... }]'
378 );
379 0 push @filters, $props->{filters};
380 }
381 elsif (ref $props->{filters} eq 'ARRAY') {
382 44 @filters = @{ $props->{filters} };
383 }
384 else {
385 0 Data::Printer::Common::_warn($self, 'filters must be an ARRAY reference');
386 }
387 }
388 253 foreach my $filter (@filters) {
389 46 my $filter_reftype = Scalar::Util::reftype($filter);
390 46 ▶ if (!defined $filter_reftype) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $filter_reftype) { } 31 15 46
elsif ($filter_reftype eq 'HASH') { } 15 0 15
391 31 $self->_load_external_filter($filter);
392 }
393 elsif ($filter_reftype eq 'HASH') {
394 15 foreach my $k (keys %$filter) {
395 47 ▶ if ($k eq '-external') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "-external") 0 47 47
396 0 Data::Printer::Common::_warn(
397 $self,
398 'please update your code: '
399 . 'filters => { -external => [qw(Foo Bar)}'
400 . ' is now filters => [qw(Foo Bar)]'
401 );
402 0 next;
403 }
404 47 ▶ if (Scalar::Util::reftype($filter->{$k}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($filter->{$k}) eq 'CODE') { } 47 0 47
405 47 my $type = Data::Printer::Common::_filter_category_for($k);
406 47 unshift @{ $self->{$type}{$k} }, $filter->{$k};
407 }
408 else {
409 0 Data::Printer::Common::_warn(
410 $self,
411 'hash filters must point to a CODE reference'
412 );
413 }
414 }
415 }
416 else {
417 0 Data::Printer::Common::_warn($self, 'filters must be a name or { type => sub {...} }');
418 }
419 }
420 253 return;
421 }
422  
423 2814 ▶sub _load_external_filter {
Subroutine _load_external_filter: called CC=4 SCAR=14.1
424 2814 my ($self, $class) = @_;
425 2814 my $module = "Data::Printer::Filter::$class";
426 2814 my $error = Data::Printer::Common::_tryme("use $module; 1;");
427 2814 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 2814 2814
428 0 Data::Printer::Common::_warn($self, "error loading filter '$class': $error");
429 0 return;
430 }
431 2814 my $from_module = $module->_filter_list;
432 2814 foreach my $kind (keys %$from_module) {
433 2814 foreach my $name (keys %{$from_module->{$kind}}) {
434 3351 unshift @{ $self->{$kind}{$name} }, @{ $from_module->{$kind}{$name} };
435 }
436 }
437 2814 return;
438 }
439  
440 253 ▶sub _detect_color_level {
Subroutine _detect_color_level: called CC=21 SCAR=46.9
441 253 my ($self) = @_;
442 253 my $colored = $self->colored;
443 253 my $color_level;
444  
445 # first we honour ANSI_COLORS_DISABLED, colored and writing to files
446 253 ▶ if ( !$colored
Branch: 1 branch
BranchTrueFalseTotal
if (not $colored or $colored eq 'auto' and exists $ENV{'ANSI_COLORS_DISABLED'} || $self->output eq 'handle' || $self->output eq 'file') { } 248 5 253
Condition50%6/12
not $colored or $colored eq "auto" and exists $ENV{"ANSI_COLORS_DISABLED"} || $self->output eq "handle" || $self->output eq "file"
l!l&&r!l&&!r
24805
$colored eq "auto" and exists $ENV{"ANSI_COLORS_DISABLED"} || $self->output eq "handle" || $self->output eq "file"
!ll&&!rl&&r
320
exists $ENV{"ANSI_COLORS_DISABLED"} or $self->output eq "handle" or $self->output eq "file"
l!l&&r!l&&!r
002
exists $ENV{"ANSI_COLORS_DISABLED"} or $self->output eq "handle"
l!l&&r!l&&!r
002
MC/DC20%1/5
not $colored or $colored eq "auto" and exists $ENV{"ANSI_COLORS_DISABLED"} || $self->output eq "handle" || $self->output eq "file"
not $colored $colored eq "auto" exists $ENV{"ANSI_COLORS_DISABLED"} $self->output eq "handle" $self->output eq "file"
Truth table50%3/6
not(A) or B and C or D or E
A= not $coloredB= $colored eq "auto"C= exists $ENV{"ANSI_COLORS_DISABLED"}D= $self->output eq "handle"E= $self->output eq "file"
ABCDEresult
1XXXX1
011XX1
0101X1
010011
00XXX0
010000
447 || ($colored eq 'auto'
448 && (exists $ENV{ANSI_COLORS_DISABLED}
449 || $self->output eq 'handle'
450 || $self->output eq 'file'
451 )
452 )
453 ) {
454 248 $color_level = 0;
455 }
456 else {
457 # NOTE: we could try `tput colors` but it may not give
458 # the proper result, so instead we do what most terminals
459 # currently do and rely on environment variables.
460 5 ▶ if ($ENV{COLORTERM} && $ENV{COLORTERM} eq 'truecolor') {
Branch: 6 branches
BranchTrueFalseTotal
if ($ENV{'COLORTERM'} and $ENV{'COLORTERM'} eq 'truecolor') { } 0 5 5
elsif ($ENV{'TERM_PROGRAM'} and $ENV{'TERM_PROGRAM'} eq 'iTerm.app') { } 0 5 5
elsif ($ENV{'TERM_PROGRAM'} and $ENV{'TERM_PROGRAM'} eq 'Apple_Terminal') { } 0 5 5
elsif ($ENV{'TERM'} and $ENV{'TERM'} =~ /\-256(?:color)?\z/i) { } 0 5 5
elsif ($ENV{'TERM'} and $ENV{'TERM'} =~ /\A(?:screen|xterm|vt100|rxvt)/i || $ENV{'TERM'} =~ /color|ansi|cygwin|linux/i) { } 5 0 5
elsif ($ENV{'COLORTERM'}) { } 0 0 0
Condition33%1/3
$ENV{"COLORTERM"} and $ENV{"COLORTERM"} eq "truecolor"
!ll&&!rl&&r
500
MC/DC0%0/2
$ENV{"COLORTERM"} and $ENV{"COLORTERM"} eq "truecolor"
$ENV{"COLORTERM"} $ENV{"COLORTERM"} eq "truecolor"
Truth table33%1/3
A and B
A= $ENV{"COLORTERM"}B= $ENV{"COLORTERM"} eq "truecolor"
ABresult
0X0
100
111
461 0 $color_level = 3;
462 }
463 ▶ elsif ($ENV{TERM_PROGRAM} && $ENV{TERM_PROGRAM} eq 'iTerm.app') {
Condition33%1/3
$ENV{"TERM_PROGRAM"} and $ENV{"TERM_PROGRAM"} eq "iTerm.app"
!ll&&!rl&&r
500
MC/DC0%0/2
$ENV{"TERM_PROGRAM"} and $ENV{"TERM_PROGRAM"} eq "iTerm.app"
$ENV{"TERM_PROGRAM"} $ENV{"TERM_PROGRAM"} eq "iTerm.app"
Truth table33%1/3
A and B
A= $ENV{"TERM_PROGRAM"}B= $ENV{"TERM_PROGRAM"} eq "iTerm.app"
ABresult
0X0
100
111
464 0 ▶ my $major_version = substr($ENV{TERM_PROGRAM_VERSION} || '0', 0, 1);
Condition0%0/2
$ENV{'TERM_PROGRAM_VERSION'} || '0'
l!l
00
MC/DC0%0/1
$ENV{'TERM_PROGRAM_VERSION'} || '0'
$ENV{'TERM_PROGRAM_VERSION'}
Truth table0%0/2
A
A= $ENV{'TERM_PROGRAM_VERSION'}
Aresult
11
00
465 0 ▶ $color_level = $major_version >= 3 ? 3 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$major_version >= 3 ? : 0 0 0
466 }
467 ▶ elsif ($ENV{TERM_PROGRAM} && $ENV{TERM_PROGRAM} eq 'Apple_Terminal') {
Condition33%1/3
$ENV{"TERM_PROGRAM"} and $ENV{"TERM_PROGRAM"} eq "Apple_Terminal"
!ll&&!rl&&r
500
MC/DC0%0/2
$ENV{"TERM_PROGRAM"} and $ENV{"TERM_PROGRAM"} eq "Apple_Terminal"
$ENV{"TERM_PROGRAM"} $ENV{"TERM_PROGRAM"} eq "Apple_Terminal"
Truth table33%1/3
A and B
A= $ENV{"TERM_PROGRAM"}B= $ENV{"TERM_PROGRAM"} eq "Apple_Terminal"
ABresult
0X0
100
111
468 0 $color_level= 2;
469 }
470 ▶ elsif ($ENV{TERM} && $ENV{TERM} =~ /\-256(?:color)?\z/i) {
Condition33%1/3
$ENV{"TERM"} and $ENV{"TERM"} =~ /\-256(?:color)?\z/i
!ll&&!rl&&r
050
MC/DC0%0/2
$ENV{"TERM"} and $ENV{"TERM"} =~ /\-256(?:color)?\z/i
$ENV{"TERM"} $ENV{"TERM"} =~ /\-256(?:color)?\z/i
Truth table33%1/3
A and B
A= $ENV{"TERM"}B= $ENV{"TERM"} =~ /\-256(?:color)?\z/i
ABresult
0X0
100
111
471 0 $color_level = 2;
472 }
473 ▶ elsif ($ENV{TERM}
Condition33%2/6
$ENV{"TERM"} and $ENV{"TERM"} =~ /\A(?:screen|xterm|vt100|rxvt)/i || $ENV{"TERM"} =~ /color|ansi|cygwin|linux/i
!ll&&!rl&&r
005
$ENV{"TERM"} =~ /\A(?:screen|xterm|vt100|rxvt)/i or $ENV{"TERM"} =~ /color|ansi|cygwin|linux/i
l!l&&r!l&&!r
500
MC/DC0%0/3
$ENV{"TERM"} and $ENV{"TERM"} =~ /\A(?:screen|xterm|vt100|rxvt)/i || $ENV{"TERM"} =~ /color|ansi|cygwin|linux/i
$ENV{"TERM"} $ENV{"TERM"} =~ /\A(?:screen|xterm|vt100|rxvt)/i $ENV{"TERM"} =~ /color|ansi|cygwin|linux/i
Truth table25%1/4
A and B or C
A= $ENV{"TERM"}B= $ENV{"TERM"} =~ /\A(?:screen|xterm|vt100|rxvt)/iC= $ENV{"TERM"} =~ /color|ansi|cygwin|linux/i
ABCresult
0XX0
1000
11X1
1011
474 && ($ENV{TERM} =~ /\A(?:screen|xterm|vt100|rxvt)/i
475 || $ENV{TERM} =~ /color|ansi|cygwin|linux/i)
476 ) {
477 5 $color_level = 1;
478 }
479 elsif ($ENV{COLORTERM}) {
480 0 $color_level = 1;
481 }
482 else {
483 0 ▶ $color_level = $colored eq 'auto' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$colored eq 'auto' ? : 0 0 0
484 }
485 }
486 253 return $color_level;
487 }
488  
489 253 ▶sub _load_colors {
Subroutine _load_colors: called CC=4 SCAR=17.3
490 253 my ($self, $props) = @_;
491  
492 253 $self->{_output_color_level} = $self->_detect_color_level;
493  
494 253 my $theme_object;
495 253 my $default_theme = 'Material';
496 253 my $theme_name = Data::Printer::Common::_fetch_scalar_or_default($props, 'theme', $default_theme);
497 $theme_object = Data::Printer::Theme->new(
498 name => $theme_name,
499 color_overrides => $props->{colors},
500 color_level => $self->{_output_color_level},
501 253 ddp => $self,
502 );
503 253 ▶ if (!$theme_object) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($theme_object) 0 253 253
504 0 ▶ if ($theme_name ne $default_theme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($theme_name ne $default_theme) 0 0 0
505 $theme_object = Data::Printer::Theme->new(
506 name => $default_theme,
507 color_overrides => $props->{colors},
508 color_level => $self->{_output_color_level},
509 0 ddp => $self,
510 );
511 }
512 0 ▶ Data::Printer::Common::_die("Unable to load default theme. This should never happen - please contact the author") unless $theme_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $theme_object 0 0 0
513 }
514 253 $self->{theme} = $theme_object;
515 }
516  
517 699 ▶sub _filters_for_type {
Subroutine _filters_for_type: called CC=2 SCAR=7.1
518 699 my ($self, $type) = @_;
519 699 ▶ return exists $self->{type_filters}{$type} ? @{ $self->{type_filters}{$type} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'type_filters'}{$type} ? : 699 0 699
520 }
521  
522 915 ▶sub _filters_for_class {
Subroutine _filters_for_class: called CC=2 SCAR=6.9
523 915 my ($self, $type) = @_;
524 915 ▶ return exists $self->{class_filters}{$type} ? @{ $self->{class_filters}{$type} } : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'class_filters'}{$type} ? : 144 771 915
525 }
526  
527 659 ▶sub _filters_for_data {
Subroutine _filters_for_data: called CC=11 SCAR=24.5
528 659 my ($self, $data) = @_;
529  
530 # we favour reftype() over ref() because you could have
531 # a HASH.pm (or ARRAY.pm or whatever) blessing any variable.
532 659 my $ref_kind = Scalar::Util::reftype($data);
533 659 ▶ $ref_kind = 'SCALAR' unless $ref_kind;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_kind 0 659 659
534  
535 # ref() returns 'Regexp' but reftype() returns 'REGEXP', so we picked one:
536 659 ▶ $ref_kind = 'Regexp' if $ref_kind eq 'REGEXP';
Branch: 1 branch
BranchTrueFalseTotal
if $ref_kind eq "REGEXP" 5 654 659
537  
538 659 my @potential_filters;
539  
540 # first, try class name + full inheritance for a specific name.
541 659 my $class = Scalar::Util::blessed($data);
542  
543 # a regular regexp is blessed, but in that case we want a
544 # regexp filter, not a class filter.
545 659 ▶ if (defined $class && $class eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class and $class eq "Regexp") 5 654 659
Condition100%3/3
defined $class and $class eq "Regexp"
!ll&&!rl&&r
5541005
MC/DC100%2/2
defined $class and $class eq "Regexp"
defined $class $class eq "Regexp"
Truth table100%3/3
A and B
A= defined $classB= $class eq "Regexp"
ABresult
0X0
100
111
546 5 ▶ if ($ref_kind eq 'Regexp' || ($] < 5.011 && $ref_kind eq 'SCALAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_kind eq "Regexp" or $] < "5.011" and $ref_kind eq "SCALAR") 5 0 5
Condition16%1/6
$ref_kind eq "Regexp" or $] < "5.011" and $ref_kind eq "SCALAR"
l!l&&r!l&&!r
500
$] < "5.011" and $ref_kind eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/3
$ref_kind eq "Regexp" or $] < "5.011" and $ref_kind eq "SCALAR"
$ref_kind eq "Regexp" $] < "5.011" $ref_kind eq "SCALAR"
Truth table25%1/4
A or B and C
A= $ref_kind eq "Regexp"B= $] < "5.011"C= $ref_kind eq "SCALAR"
ABCresult
1XX1
0111
00X0
0100
547 5 $ref_kind = 'Regexp';
548 5 undef $class;
549 }
550 }
551 659 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 100 559 659
552 100 ▶ if ($self->class->parent_filters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class->parent_filters) { } 99 1 100
553 99 my $linear_ISA = Data::Printer::Common::_linear_ISA_for($class, $self);
554 99 foreach my $candidate_class (@$linear_ISA) {
555 155 push @potential_filters, $self->_filters_for_class($candidate_class);
556 }
557 }
558 else {
559 1 push @potential_filters, $self->_filters_for_class($class);
560 }
561 # next, let any '-class' filters have a go:
562 100 push @potential_filters, $self->_filters_for_class('-class');
563 }
564  
565 # then, try regular data filters
566 659 push @potential_filters, $self->_filters_for_type($ref_kind);
567  
568 # finally, if it's neither a class nor a known core type,
569 # we must be in a future perl with some type we're unaware of:
570 659 push @potential_filters, $self->_filters_for_class('-unknown');
571  
572 659 return @potential_filters;
573 }
574  
575 # _see($data): marks data as seen if it was never seen before.
576 # if we are showing refcounts, we return those. Initially we had
577 # this funcionallity separated, but refcounts increase as we find
578 # them again and because of that we were seeing weird refcounting.
579 # So now instead we store the refcount of the variable when we
580 # first see it.
581 # Finally, if we have already seen the data, we return its stringified
582 # position, like "var", "var{foo}[7]", etc. UNLESS $options{seen_override}
583 # is set. Why seen_override? Sometimes we want to print the same data
584 # twice, like the GenericClass filter, which prints the object's metadata
585 # via parse() and then the internal structure via parse_as(). But if we
586 # simply do that, we'd get the "seen" version (because we have already
587 # visited it!) The refcount is still calculated only once though :)
588 732 ▶sub _see {
Subroutine _see: called CC=6 SCAR=17.9
589 732 my ($self, $data, %options) = @_;
590 732 ▶ return {} unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 2 730 732
591 730 my $id = pack 'J', Scalar::Util::refaddr($data);
592 730 ▶ if (!exists $self->{_seen}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_seen"}{$id}) 656 74 730
593 656 ▶ my $entry = {
Branch: 1 branch
BranchTrueFalseTotal
$self->show_refcount ? : 74 582 656
594 name => $self->current_name,
595 refcount => ($self->show_refcount ? $self->_refcount($data) : 0),
596 };
597 # the values returned by tied hashes are temporaries, so we can't
598 # mark them as 'seen'. Ideally, we'd use something like
599 # Hash::Util::Fieldhash::register() (see PR#179) and remove entries
600 # from $self->{_seen} when $data is destroyed. The problem is this
601 # adds a lot of internal magic to the data we're inspecting (we tried,
602 # see Issue#75), effectively changing it. So we just ignore them, at
603 # the risk of missing some circular reference.
604 656 ▶ $self->{_seen}{$id} = $entry unless $options{tied_parent};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"tied_parent"} 652 4 656
605 656 return { refcount => $entry->{refcount} };
606 }
607 74 ▶ return { refcount => $self->{_seen}{$id}->{refcount} } if $options{seen_override};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"seen_override"} 41 33 74
608 33 return $self->{_seen}{$id};
609 }
610  
611 3 ▶sub seen {
Subroutine seen: called CC=1 SCAR=0.0
Pod documented
612 3 my ($self, $data) = @_;
613 3 my $id = pack 'J', Scalar::Util::refaddr($data);
614 3 return exists $self->{_seen}{$id};
615 }
616  
617 20 ▶sub unsee {
Subroutine unsee: called CC=3 SCAR=11.6
Pod documented
618 20 my ($self, $data) = @_;
619 20 ▶ return unless ref $data && keys %{$self->{_seen}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data and keys %{$$self{"_seen"};} 0 20 20
Condition33%1/3
ref $data and keys %{$$self{"_seen"};}
!ll&&!rl&&r
0020
MC/DC0%0/2
ref $data and keys %{$$self{"_seen"};}
ref $data keys %{$$self{"_seen"};}
Truth table33%1/3
A and B
A= ref $dataB= keys %{$$self{"_seen"};}
ABresult
0X0
100
111
620  
621 20 my $id = pack 'J', Scalar::Util::refaddr($data);
622 20 delete $self->{_seen}{$id};
623 20 return;
624 }
625  
626 74 ▶sub _refcount {
Subroutine _refcount: called CC=4 SCAR=14.0
627 74 my ($self, $data) = @_;
628  
629 74 require B;
630 74 my $count;
631 74 my $rv = B::svref_2object(\$data)->RV;
632 74 ▶ if (ref($data) eq 'REF' && ref($$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "REF" and ref $$data) 25 49 74
Condition66%2/3
ref $data eq "REF" and ref $$data
!ll&&!rl&&r
49025
MC/DC50%1/2
ref $data eq "REF" and ref $$data
ref $data eq "REF" ref $$data
Truth table66%2/3
A and B
A= ref $data eq "REF"B= ref $$data
ABresult
0X0
100
111
633 25 $rv = B::svref_2object($data)->RV;
634 }
635  
636 # some SV's are special (represented by B::SPECIAL)
637 # and don't have a ->REFCNT (e.g. \undef)
638 74 ▶ return 0 unless $rv->can( 'REFCNT' );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv->can("REFCNT") 0 74 74
639  
640 # 3 is our magical number: so we return the actual reference count
641 # minus the references we added as we were traversing:
642 74 return $rv->REFCNT - $self->{_refcount_base};
643 }
644  
645 40 ▶sub parse_as {
Subroutine parse_as: called CC=1 SCAR=0.0
Pod documented
646 40 my ($self, $type, $data) = @_;
647 40 return $self->parse($data, force_type => $type, seen_override => 1);
648 }
649  
650 # parse() must always receive a reference, never a regular copy, because
651 # that's the only way we are able to figure whether the source data
652 # is a weak ref or not.
653 732 ▶sub parse {
Subroutine parse: called CC=12 SCAR=24.9
Pod documented
654 732 my $self = shift;
655 732 my $str_weak = $self->_check_weak( $_[0] );
656  
657 732 my ($data, %options) = @_;
658 732 my $parsed_string = '';
659  
660 # if we've seen this structure before, we return its location
661 # instead of going through it again. This avoids infinite loops
662 # when parsing circular references:
663 732 my $seen = $self->_see($data, %options);
664 732 ▶ if (my $name = $seen->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $seen->{"name"}) 33 699 732
665 33 ▶ $parsed_string .= $self->maybe_colorize(
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'SCALAR' && $self->resolve_scalar_refs ? : 0 33 33
Condition66%2/3
ref $data eq "SCALAR" and $self->resolve_scalar_refs
!ll&&!rl&&r
2940
MC/DC0%0/2
ref $data eq "SCALAR" and $self->resolve_scalar_refs
ref $data eq "SCALAR" $self->resolve_scalar_refs
Truth table66%2/3
A and B
A= ref $data eq "SCALAR"B= $self->resolve_scalar_refs
ABresult
0X0
100
111
666 ((ref $data eq 'SCALAR' && $self->resolve_scalar_refs)
667 ? $$data
668 : $name
669 ),
670 'repeated'
671 );
672 # on repeated references, the only extra data we put
673 # is whether this reference is weak or not.
674 33 $parsed_string .= $str_weak;
675 33 return $parsed_string;
676 }
677 699 $self->{_position}++;
678  
679 # Each filter type provides an array of potential parsers.
680 # Once we find the right kind, we go through all of them,
681 # from most precise match to most generic.
682 # The first filter that returns a defined value "wins"
683 # (even if it's an empty string)
684 699 ▶ foreach my $filter (
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'force_type'} ? : 40 659 699
685 exists $options{force_type}
686 ? $self->_filters_for_type($options{force_type})
687 : $self->_filters_for_data($data)
688 ) {
689 700 ▶ if (defined (my $result = $filter->($data, $self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $result = &$filter($data, $self))) 699 1 700
690 699 $parsed_string .= $result;
691 699 last;
692 }
693 }
694  
695 # FIXME: because of prototypes, p(@data) becomes a ref (that we don't care about)
696 # to the data (that we do care about). So we should not show refcounts, memsize
697 # or readonly status for something guaranteed to be ephemeral.
698 699 $parsed_string .= $self->_check_readonly($data);
699 699 ▶ $parsed_string .= $str_weak if ref($data) ne 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "REF" 631 68 699
700 699 $parsed_string .= $self->_check_memsize($data);
701  
702 699 ▶ if ($self->show_refcount && ref($data) ne 'SCALAR' && $seen->{refcount} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_refcount and ref $data ne "SCALAR" and $seen->{"refcount"} > 1) 20 679 699
Condition100%6/6
$self->show_refcount and ref $data ne "SCALAR" and $seen->{"refcount"} > 1
!ll&&!rl&&r
6483120
$self->show_refcount and ref $data ne "SCALAR"
!ll&&!rl&&r
6252351
MC/DC100%3/3
$self->show_refcount and ref $data ne "SCALAR" and $seen->{"refcount"} > 1
$self->show_refcount ref $data ne "SCALAR" $seen->{"refcount"} > 1
Truth table100%4/4
A and B and C
A= $self->show_refcountB= ref $data ne "SCALAR"C= $seen->{"refcount"} > 1
ABCresult
0XX0
10X0
1100
1111
703 20 $parsed_string .= ' (refcount: ' . $seen->{refcount} .')';
704 }
705  
706 699 ▶ if (--$self->{'_position'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"_position"} == 0) 316 383 699
707 316 $self->{'_seen'} = {};
708 316 $self->{'_refcount_base'} = 3;
709 316 $self->{'_position'} = 0;
710 }
711  
712 699 return $parsed_string;
713 }
714  
715 699 ▶sub _check_memsize {
Subroutine _check_memsize: called CC=9 SCAR=43.0
716 699 my ($self, $data) = @_;
717 return '' unless $self->show_memsize
718 && ( $self->show_memsize eq 'all'
719 699 ▶ || $self->show_memsize >= $self->{_position});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->show_memsize and $self->show_memsize eq "all" || $self->show_memsize >= $self->{"_position"} 699 0 699
Condition16%1/6
$self->show_memsize and $self->show_memsize eq "all" || $self->show_memsize >= $self->{"_position"}
!ll&&!rl&&r
69900
$self->show_memsize eq "all" or $self->show_memsize >= $self->{"_position"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->show_memsize and $self->show_memsize eq "all" || $self->show_memsize >= $self->{"_position"}
$self->show_memsize $self->show_memsize eq "all" $self->show_memsize >= $self->{"_position"}
Truth table25%1/4
A and B or C
A= $self->show_memsizeB= $self->show_memsize eq "all"C= $self->show_memsize >= $self->{"_position"}
ABCresult
0XX0
1000
11X1
1011
720 0 my $size;
721 my $unit;
722 0 ▶ my $error = Data::Printer::Common::_tryme(sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
723 0 require Devel::Size;
724 0 $size = Devel::Size::total_size($data);
725 0 $unit = uc $self->memsize_unit;
726 0 ▶ if ($unit eq 'M' || ($unit eq 'AUTO' && $size > 1024*1024)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($unit eq 'M' or $unit eq 'AUTO' and $size > 1048576) { } 0 0 0
elsif ($unit eq 'K' or $unit eq 'AUTO' and $size > 1024) { } 0 0 0
Condition0%0/6
$unit eq "M" or $unit eq "AUTO" and $size > 1048576
l!l&&r!l&&!r
000
$unit eq "AUTO" and $size > 1048576
!ll&&!rl&&r
000
MC/DC0%0/3
$unit eq "M" or $unit eq "AUTO" and $size > 1048576
$unit eq "M" $unit eq "AUTO" $size > 1048576
Truth table0%0/4
A or B and C
A= $unit eq "M"B= $unit eq "AUTO"C= $size > 1048576
ABCresult
1XX1
0111
00X0
0100
727 0 $size = $size / (1024*1024);
728 0 $unit = 'M';
729 }
730 ▶ elsif ($unit eq 'K' || ($unit eq 'AUTO' && $size > 1024)) {
Condition0%0/6
$unit eq "K" or $unit eq "AUTO" and $size > 1024
l!l&&r!l&&!r
000
$unit eq "AUTO" and $size > 1024
!ll&&!rl&&r
000
MC/DC0%0/3
$unit eq "K" or $unit eq "AUTO" and $size > 1024
$unit eq "K" $unit eq "AUTO" $size > 1024
Truth table0%0/4
A or B and C
A= $unit eq "K"B= $unit eq "AUTO"C= $size > 1024
ABCresult
1XX1
0111
00X0
0100
731 0 $size = $size / 1024;
732 0 $unit = 'K';
733 }
734 else {
735 0 $unit = 'B';
736 }
737 0 });
738 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
739 0 ▶ if ($error =~ m{locate Devel/Size.pm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ m[locate Devel/Size.pm]) { } 0 0 0
740 Data::Printer::Common::_warn($self, "Devel::Size not found, show_memsize will be ignored")
741 0 ▶ if $self->{_position} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_position"} == 1 0 0 0
742 }
743 else {
744 0 Data::Printer::Common::_warn($self, "error fetching memory usage: $error");
745 }
746 0 return '';
747 }
748 0 ▶ return '' unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
749 0 ▶ my $string = ' (' . ($size < 0 ? sprintf("%.2f", $size) : int($size)) . $unit . ')';
Branch: 1 branch
BranchTrueFalseTotal
$size < 0 ? : 0 0 0
750 0 return $self->maybe_colorize($string, 'memsize');
751 }
752  
753 732 ▶sub _check_weak {
Subroutine _check_weak: called CC=6 SCAR=17.9
754 732 my ($self) = shift;
755 732 ▶ return '' unless $self->show_weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->show_weak 38 694 732
756  
757 694 my $realtype = Scalar::Util::reftype($_[0]);
758 694 my $isweak;
759 694 ▶ if ($realtype && ($realtype eq 'REF' || $realtype eq 'SCALAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($realtype and $realtype eq 'REF' || $realtype eq 'SCALAR') { } 452 242 694
Condition83%5/6
$realtype and $realtype eq "REF" || $realtype eq "SCALAR"
!ll&&!rl&&r
0242452
$realtype eq "REF" or $realtype eq "SCALAR"
l!l&&r!l&&!r
68384242
MC/DC66%2/3
$realtype and $realtype eq "REF" || $realtype eq "SCALAR"
$realtype $realtype eq "REF" $realtype eq "SCALAR"
Truth table75%3/4
A and B or C
A= $realtypeB= $realtype eq "REF"C= $realtype eq "SCALAR"
ABCresult
0XX0
1000
11X1
1011
760 452 $isweak = Scalar::Util::isweak($_[0]);
761 }
762 else {
763 242 $isweak = Scalar::Util::isweak($_[0]);
764 }
765 694 ▶ return '' unless $isweak;
Branch: 1 branch
BranchTrueFalseTotal
unless $isweak 677 17 694
766 17 return ' ' . $self->maybe_colorize('(weak)', 'weak');
767 }
768  
769 61 ▶sub _write_label {
Subroutine _write_label: called CC=4 SCAR=13.9
770 61 my ($self) = @_;
771 61 ▶ return '' unless $self->caller_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->caller_info 59 2 61
772 2 my @caller = caller 1;
773  
774 2 my $message = $self->caller_message;
775  
776 2 $message =~ s/\b__PACKAGE__\b/$caller[0]/g;
777 2 $message =~ s/\b__FILENAME__\b/$caller[1]/g;
778 2 $message =~ s/\b__LINE__\b/$caller[2]/g;
779  
780 2 ▶ my $separator = $self->caller_message_newline ? "\n" : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->caller_message_newline ? : 0 2 2
781 2 $message = $self->maybe_colorize($message, 'caller_info');
782 2 ▶ $message = $self->caller_message_position eq 'before'
Branch: 1 branch
BranchTrueFalseTotal
$self->caller_message_position eq 'before' ? : 1 1 2
783 ? $message . $separator
784 : $separator . $message
785 ;
786 2 return $message;
787 }
788  
789 1895 ▶sub maybe_colorize {
Subroutine maybe_colorize: called CC=7 SCAR=19.5
Pod documented
790 1895 my ($self, $output, $color_type, $default_color, $end_color) = @_;
791  
792 1895 ▶ if ($self->{_output_color_level} && defined $color_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_output_color_level"} and defined $color_type) 3 1892 1895
Condition100%3/3
$self->{"_output_color_level"} and defined $color_type
!ll&&!rl&&r
189113
MC/DC100%2/2
$self->{"_output_color_level"} and defined $color_type
$self->{"_output_color_level"} defined $color_type
Truth table100%3/3
A and B
A= $self->{"_output_color_level"}B= defined $color_type
ABresult
0X0
100
111
793 3 my $theme = $self->theme;
794 3 my $sgr_color = $theme->sgr_color_for($color_type);
795 3 ▶ if (!defined $sgr_color && defined $default_color) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sgr_color and defined $default_color) 1 2 3
Condition100%3/3
not defined $sgr_color and defined $default_color
!ll&&!rl&&r
111
MC/DC100%2/2
not defined $sgr_color and defined $default_color
not defined $sgr_color defined $default_color
Truth table100%3/3
not(A) and B
A= not defined $sgr_colorB= defined $default_color
ABresult
0X0
100
111
796 1 $sgr_color = $theme->_parse_color($default_color);
797 }
798 3 ▶ if ($sgr_color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sgr_color) 1 2 3
799 1 ▶ $output = $sgr_color
Branch: 1 branch
BranchTrueFalseTotal
defined $end_color ? : 0 1 1
800 . $output
801 . (defined $end_color
802 ? $theme->sgr_color_for($end_color)
803 : $theme->color_reset
804 );
805 }
806 }
807 1895 return $output;
808 }
809  
810 699 ▶sub _check_readonly {
Subroutine _check_readonly: called CC=3 SCAR=11.0
811 699 my ($self) = @_;
812 699 ▶ return ' (read-only)' if $self->show_readonly && &Internals::SvREADONLY($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $self->show_readonly and &Internals::SvREADONLY($_[1]) 7 692 699
Condition100%3/3
$self->show_readonly and &Internals::SvREADONLY($_[1])
!ll&&!rl&&r
716217
MC/DC100%2/2
$self->show_readonly and &Internals::SvREADONLY($_[1])
$self->show_readonly &Internals::SvREADONLY($_[1])
Truth table100%3/3
A and B
A= $self->show_readonlyB= &Internals::SvREADONLY($_[1])
ABresult
0X0
100
111
813 692 return '';
814 }
815  
816 35 42;
817 __END__
« Data/Printer/Config.pm ↑ Summary Data/Printer/Filter/HASH.pm »