File Coverage

blib/lib/Data/Printer/Object.pm
Criterion Covered Total %
statement 365 413 88.3
branch 116 174 66.6
condition 54 101 53.4
subroutine 63 64 98.4
pod 15 15 100.0
total 613 767 79.9


line stmt bran cond sub pod time code
1 35     35   2685862 use strict;
  35         69  
  35         1371  
2 35     35   175 use warnings;
  35         99  
  35         2134  
3 35     35   12457 use Data::Printer::Common;
  35         116  
  35         20361  
4              
5             package # hide from pause
6             Data::Printer::Object::ClassOptions;
7 26     26   105 sub parents { $_[0]->{'parents'} }
8 118     118   533 sub linear_isa { $_[0]->{'linear_isa'} }
9 155     155   809 sub universal { $_[0]->{'universal'} }
10 96     96   470 sub expand { $_[0]->{'expand'} }
11 54     54   271 sub stringify { $_[0]->{'stringify'} }
12 49     49   176 sub show_reftype { $_[0]->{'show_reftype'} }
13 41     41   129 sub show_overloads { $_[0]->{'show_overloads'} }
14 122     122   447 sub show_methods { $_[0]->{'show_methods'} }
15 78     78   429 sub sort_methods { $_[0]->{'sort_methods'} }
16 2     2   11 sub show_wrapped { $_[0]->{'show_wrapped'} }
17 40     40   94 sub inherited { $_[0]->{'inherited'} }
18 77     77   225 sub format_inheritance { $_[0]->{'format_inheritance'} }
19 102     102   378 sub parent_filters { $_[0]->{'parent_filters'} }
20 49     49   122 sub internals { $_[0]->{'internals'} }
21             sub new {
22 255     255   939 my ($class, $params) = @_;
23 255         707 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 255         1914 return bless $self, $class;
46             }
47             1;
48              
49             package Data::Printer::Object;
50 35     35   272 use Scalar::Util ();
  35         125  
  35         984  
51 35     35   18741 use Data::Printer::Theme;
  35         105  
  35         1499  
52 35     35   17871 use Data::Printer::Filter::SCALAR; # also implements LVALUE
  35         114  
  35         3480  
53 35     35   17635 use Data::Printer::Filter::ARRAY;
  35         104  
  35         3602  
54 35     35   19446 use Data::Printer::Filter::HASH;
  35         106  
  35         1476  
55 35     35   16720 use Data::Printer::Filter::REF;
  35         105  
  35         1478  
56 35     35   18466 use Data::Printer::Filter::VSTRING;
  35         105  
  35         1396  
57 35     35   16728 use Data::Printer::Filter::GLOB;
  35         111  
  35         3266  
58 35     35   18578 use Data::Printer::Filter::FORMAT;
  35         98  
  35         3373  
59 35     35   16215 use Data::Printer::Filter::Regexp;
  35         108  
  35         1497  
60 35     35   18318 use Data::Printer::Filter::CODE;
  35         109  
  35         1406  
61 35     35   16510 use Data::Printer::Filter::OBJECT;
  35         99  
  35         1405  
62 35     35   18447 use Data::Printer::Filter::GenericClass;
  35         101  
  35         3708  
63              
64             # create our basic accessors:
65             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             foreach my $method_name (@method_names) {
77 35     35   282 no strict 'refs';
  35         69  
  35         202702  
78             *{__PACKAGE__ . "::$method_name"} = sub {
79 10657 100   10657   24776 $_[0]->{$method_name} = $_[1] if @_ > 1;
80 10657         44826 return $_[0]->{$method_name};
81             }
82             }
83 188     188 1 793 sub extra_config { $_[0]->{extra_config} }
84              
85 33     33 1 180 sub current_depth { $_[0]->{_depth} }
86 261     261 1 639 sub indent { $_[0]->{_depth}++ }
87 260     260 1 788 sub outdent { $_[0]->{_depth}-- }
88              
89             sub newline {
90 712     712 1 1473 my ($self) = @_;
91             return $self->{_linebreak}
92             . (' ' x ($self->{_depth} * $self->{_current_indent}))
93             . (' ' x $self->{_array_padding})
94 712         3928 ;
95             }
96              
97             sub current_name {
98 1576     1576 1 3250 my ($self, $new_value) = @_;
99 1576 100       3216 if (defined $new_value) {
100 612         1332 $self->{_current_name} = $new_value;
101             }
102             else {
103 964 100       3407 $self->{_current_name} = $self->name unless defined $self->{_current_name};
104             }
105 1576         4521 return $self->{_current_name};
106             }
107              
108             sub _init {
109 255     255   585 my $self = shift;
110 255 100       1328 my $props = { @_ == 1 ? %{$_[0]} : @_ };
  64         430  
111              
112 255         1088 $self->{'_linebreak'} = "\n";
113 255         708 $self->{'_depth'} = 0;
114 255         673 $self->{'_position'} = 0; # depth is for indentation only!
115 255         683 $self->{'_array_padding'} = 0;
116 255         680 $self->{'_seen'} = {};
117 255         806 $self->{_refcount_base} = 3;
118 255         1166 $self->{'warnings'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'warning', 1);
119 255         723 $self->{'indent'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'indent', 4);
120 255         727 $self->{'index'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'index', 1);
121 255         735 $self->{'name'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'name', 'var');
122 255         1226 $self->{'arrows'} = Data::Printer::Common::_fetch_anyof(
123             $props,
124             'arrows',
125             'none',
126             [qw(none first all)]
127             );
128              
129 255         902 $self->{'show_tainted'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_tainted', 1);
130 255         657 $self->{'show_tied'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_tied', 1);
131 255         702 $self->{'show_weak'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_weak', 1);
132 255         715 $self->{'show_unicode'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_unicode', 0);
133 255         675 $self->{'show_readonly'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_readonly', 1);
134 255         766 $self->{'show_lvalue'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_lvalue', 1);
135 255         680 $self->{'show_refcount'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_refcount', 0);
136 255         689 $self->{'show_memsize'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_memsize', 0);
137 255         1075 $self->{'memsize_unit'} = Data::Printer::Common::_fetch_anyof(
138             $props,
139             'memsize_unit',
140             'auto',
141             [qw(auto b k m)]
142             );
143 255         785 $self->{'print_escapes'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'print_escapes', 0);
144 255         693 $self->{'scalar_quotes'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'scalar_quotes', q("));
145 255         992 $self->{'escape_chars'} = Data::Printer::Common::_fetch_anyof(
146             $props,
147             'escape_chars',
148             'none',
149             [qw(none nonascii nonlatin1 all)]
150             );
151 255         799 $self->{'caller_info'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'caller_info', 0);
152 255         742 $self->{'caller_message'} = Data::Printer::Common::_fetch_scalar_or_default(
153             $props,
154             'caller_message',
155             'Printing in line __LINE__ of __FILENAME__:'
156             );
157 255         727 $self->{'caller_message_newline'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'caller_message_newline', 1);
158 255         1434 $self->{'caller_message_position'} = Data::Printer::Common::_fetch_anyof($props, 'caller_message_position', 'before', [qw(before after)]);
159 255         945 $self->{'resolve_scalar_refs'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'resolve_scalar_refs', 0);
160 255         747 $self->{'string_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'string_max', 4096);
161 255         1097 $self->{'string_preserve'} = Data::Printer::Common::_fetch_anyof(
162             $props,
163             'string_preserve',
164             'begin',
165             [qw(begin end middle extremes none)]
166             );
167 255         770 $self->{'string_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
168             $props,
169             'string_overflow',
170             '(...skipping __SKIPPED__ chars...)'
171             );
172 255         748 $self->{'array_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'array_max', 100);
173 255         1032 $self->{'array_preserve'} = Data::Printer::Common::_fetch_anyof(
174             $props,
175             'array_preserve',
176             'begin',
177             [qw(begin end middle extremes none)]
178             );
179 255         836 $self->{'array_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
180             $props,
181             'array_overflow',
182             '(...skipping __SKIPPED__ items...)'
183             );
184 255         784 $self->{'hash_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'hash_max', 100);
185 255         1005 $self->{'hash_preserve'} = Data::Printer::Common::_fetch_anyof(
186             $props,
187             'hash_preserve',
188             'begin',
189             [qw(begin end middle extremes none)]
190             );
191 255         792 $self->{'hash_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
192             $props,
193             'hash_overflow',
194             '(...skipping __SKIPPED__ keys...)'
195             );
196 255         637 $self->{'unicode_charnames'} = Data::Printer::Common::_fetch_scalar_or_default(
197             $props,
198             'unicode_charnames',
199             0
200             );
201 255         686 $self->{'colored'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'colored', 'auto');
202 255         661 $self->{'max_depth'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'max_depth', 0);
203 255         689 $self->{'separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'separator', ',');
204 255         637 $self->{'end_separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'end_separator', 0);
205 255         694 $self->{'class_method'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'class_method', '_data_printer');
206 255         1867 $self->{'class'} = Data::Printer::Object::ClassOptions->new($props->{'class'});
207 255         896 $self->{'hash_separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'hash_separator', ' ');
208 255         718 $self->{'align_hash'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'align_hash', 1);
209 255         645 $self->{'sort_keys'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'sort_keys', 1);
210 255         710 $self->{'quote_keys'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'quote_keys', 'auto');
211 255         714 $self->{'deparse'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'deparse', 0);
212 255         700 $self->{'coderef_stub'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'coderef_stub', 'sub { ... }');
213 255         710 $self->{'coderef_undefined'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'coderef_undefined', '<undefined coderef>');
214 255         1559 $self->{'return_value'} = Data::Printer::Common::_fetch_anyof(
215             $props,
216             'return_value',
217             'pass',
218             [qw(pass dump void)]
219             );
220 255         950 $self->{'show_dualvar'} = Data::Printer::Common::_fetch_anyof(
221             $props,
222             'show_dualvar',
223             'lax',
224             [qw(lax strict off)]
225             );
226              
227 255 100       878 if (exists $props->{as}) {
228 1         4 my $msg = Data::Printer::Common::_fetch_scalar_or_default($props, 'as', '');
229 1         3 $self->{caller_info} = 1;
230 1         5 $self->{caller_message} = $msg;
231             }
232              
233             $self->multiline(
234 255         766 Data::Printer::Common::_fetch_scalar_or_default($props, 'multiline', 1)
235             );
236              
237 255         817 $self->fulldump(
238             Data::Printer::Common::_fetch_scalar_or_default($props, 'fulldump', 0)
239             );
240              
241 255 100       1167 $self->output(defined $props->{output} ? $props->{output} : 'stderr');
242 255         1049 $self->_load_colors($props);
243 255         1091 $self->_load_filters($props);
244              
245 255         473 my %extra_config;
246 255         788 my %core_options = map { $_ => 1 }
  13260         32564  
247             (@method_names, qw(as multiline output colors filters));
248 255         1773 foreach my $key (keys %$props) {
249 783 100       2189 $extra_config{$key} = $props->{$key} unless exists $core_options{$key};
250             }
251 255         891 $self->{extra_config} = \%extra_config;
252              
253 255         6710 return $self;
254             }
255              
256             sub output {
257 259     259 1 646 my ($self, $new_output) = @_;
258 259 100       807 if (@_ > 1) {
259 255         761 $self->_load_output_handle($new_output);
260             }
261 259         603 return $self->{output};
262             }
263              
264             sub _load_output_handle {
265 255     255   560 my ($self, $output) = @_;
266 255         1811 my %targets = ( stdout => *STDOUT, stderr => *STDERR );
267 255         503 my $error;
268 255         534 my $ref = ref $output;
269 255 100 100     1684 if (!$ref and exists $targets{ lc $output }) {
    100 100        
    50 100        
      66        
      66        
270 250         910 $self->{output} = lc $output;
271 250         937 $self->{output_handle} = $targets{ $self->{output} };
272             }
273             elsif ( ( $ref and $ref eq 'GLOB')
274             or (!$ref and \$output =~ /GLOB\([^()]+\)$/)
275             ) {
276 3         6 $self->{output} = 'handle';
277 3         7 $self->{output_handle} = $output;
278             }
279             elsif (!$ref or $ref eq 'SCALAR') {
280 2 50       51 if (open my $fh, '>>', $output) {
281 2         5 $self->{output} = 'file';
282 2         6 $self->{output_handle} = $fh;
283             }
284             else {
285 0         0 $error = "file '$output': $!";
286             }
287             }
288             else {
289 0         0 $error = 'unknown output data';
290             }
291 255 50       735 if ($error) {
292 0         0 Data::Printer::Common::_warn($self, "error opening custom output handle: $error");
293 0         0 $self->{output_handle} = $targets{'stderr'}
294             }
295 255         995 return;
296             }
297              
298             sub new {
299 255     255 1 4397032 my $class = shift;
300 255         886 my $self = bless {}, $class;
301 255         1170 return $self->_init(@_);
302             }
303              
304             sub multiline {
305 326     326 1 850 my ($self, $value) = @_;
306 326 100       940 if (defined $value) {
307 255         744 $self->{multiline} = !!$value;
308 255 100       695 if ($value) {
309 218         544 $self->{_linebreak} = "\n";
310 218         622 $self->{_current_indent} = $self->{indent};
311             $self->index( $self->{_original_index} )
312 218 50       665 if exists $self->{_original_index};
313             $self->hash_separator( $self->{_original_separator} )
314 218 50       630 if exists $self->{_original_separator};
315             $self->array_overflow( $self->{_original_array_overflow} )
316 218 50       652 if exists $self->{_original_array_overflow};
317             $self->hash_overflow( $self->{_original_hash_overflow} )
318 218 50       673 if exists $self->{_original_hash_overflow};
319             $self->string_overflow( $self->{_original_string_overflow} )
320 218 50       666 if exists $self->{_original_string_overflow};
321             }
322             else {
323 37         110 $self->{_original_index} = $self->index;
324 37         109 $self->index(0);
325 37         88 $self->{_original_separator} = $self->hash_separator;
326 37         94 $self->hash_separator(':');
327 37         93 $self->{_original_array_overflow} = $self->array_overflow;
328 37         106 $self->array_overflow('(...)');
329 37         134 $self->{_original_hash_overflow} = $self->hash_overflow;
330 37         116 $self->hash_overflow('(...)');
331 37         89 $self->{_original_string_overflow} = $self->string_overflow;
332 37         113 $self->string_overflow('(...)');
333 37         76 $self->{_linebreak} = ' ';
334 37         90 $self->{_current_indent} = 0;
335             }
336             }
337 326         936 return $self->{multiline};
338             }
339              
340             sub fulldump {
341 255     255 1 645 my ($self, $value) = @_;
342 255 50       709 if (defined $value) {
343 255         727 $self->{fulldump} = !!$value;
344 255 50       658 if ($value) {
345 0         0 $self->{_original_string_max} = $self->string_max;
346 0         0 $self->string_max(0);
347 0         0 $self->{_original_array_max} = $self->array_max;
348 0         0 $self->array_max(0);
349 0         0 $self->{_original_hash_max} = $self->hash_max;
350 0         0 $self->hash_max(0);
351             }
352             else {
353             $self->string_max($self->{_original_string_max})
354 255 50       684 if exists $self->{_original_string_max};
355             $self->array_max($self->{_original_array_max})
356 255 50       667 if exists $self->{_original_array_max};
357             $self->hash_max($self->{_original_hash_max})
358 255 50       909 if exists $self->{_original_hash_max};
359             }
360             }
361             }
362              
363             sub _load_filters {
364 255     255   600 my ($self, $props) = @_;
365              
366             # load our core filters (LVALUE is under the 'SCALAR' filter module)
367 255         1136 my @core_filters = qw(SCALAR ARRAY HASH REF VSTRING GLOB FORMAT Regexp CODE OBJECT GenericClass);
368 255         614 foreach my $class (@core_filters) {
369 2805         7201 $self->_load_external_filter($class);
370             }
371 255         585 my @filters;
372             # load any custom filters provided by the user
373 255 100       966 if (exists $props->{filters}) {
374 44 50       213 if (ref $props->{filters} eq 'HASH') {
    50          
375 0         0 Data::Printer::Common::_warn(
376             $self,
377             'please update your code: filters => { ... } is now filters => [{ ... }]'
378             );
379 0         0 push @filters, $props->{filters};
380             }
381             elsif (ref $props->{filters} eq 'ARRAY') {
382 44         84 @filters = @{ $props->{filters} };
  44         141  
383             }
384             else {
385 0         0 Data::Printer::Common::_warn($self, 'filters must be an ARRAY reference');
386             }
387             }
388 255         633 foreach my $filter (@filters) {
389 46         100 my $filter_reftype = Scalar::Util::reftype($filter);
390 46 100       156 if (!defined $filter_reftype) {
    50          
391 31         89 $self->_load_external_filter($filter);
392             }
393             elsif ($filter_reftype eq 'HASH') {
394 15         52 foreach my $k (keys %$filter) {
395 47 50       134 if ($k eq '-external') {
396 0         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         0 next;
403             }
404 47 50       119 if (Scalar::Util::reftype($filter->{$k}) eq 'CODE') {
405 47         123 my $type = Data::Printer::Common::_filter_category_for($k);
406 47         82 unshift @{ $self->{$type}{$k} }, $filter->{$k};
  47         198  
407             }
408             else {
409 0         0 Data::Printer::Common::_warn(
410             $self,
411             'hash filters must point to a CODE reference'
412             );
413             }
414             }
415             }
416             else {
417 0         0 Data::Printer::Common::_warn($self, 'filters must be a name or { type => sub {...} }');
418             }
419             }
420 255         1100 return;
421             }
422              
423             sub _load_external_filter {
424 2836     2836   6695 my ($self, $class) = @_;
425 2836         5447 my $module = "Data::Printer::Filter::$class";
426 2836         9511 my $error = Data::Printer::Common::_tryme("use $module; 1;");
427 2836 50       8098 if ($error) {
428 0         0 Data::Printer::Common::_warn($self, "error loading filter '$class': $error");
429 0         0 return;
430             }
431 2836         11422 my $from_module = $module->_filter_list;
432 2836         9537 foreach my $kind (keys %$from_module) {
433 2836         4644 foreach my $name (keys %{$from_module->{$kind}}) {
  2836         8304  
434 3375         4970 unshift @{ $self->{$kind}{$name} }, @{ $from_module->{$kind}{$name} };
  3375         10391  
  3375         12593  
435             }
436             }
437 2836         8293 return;
438             }
439              
440             sub _detect_color_level {
441 255     255   599 my ($self) = @_;
442 255         1702 my $colored = $self->colored;
443 255         532 my $color_level;
444              
445             # first we honour ANSI_COLORS_DISABLED, colored and writing to files
446 255 100 33     992 if ( !$colored
      66        
      66        
447             || ($colored eq 'auto'
448             && (exists $ENV{ANSI_COLORS_DISABLED}
449             || $self->output eq 'handle'
450             || $self->output eq 'file'
451             )
452             )
453             ) {
454 250         556 $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 50 33     117 if ($ENV{COLORTERM} && $ENV{COLORTERM} eq 'truecolor') {
    50 33        
    50 33        
    50 33        
    50 33        
    0 33        
461 0         0 $color_level = 3;
462             }
463             elsif ($ENV{TERM_PROGRAM} && $ENV{TERM_PROGRAM} eq 'iTerm.app') {
464 0   0     0 my $major_version = substr($ENV{TERM_PROGRAM_VERSION} || '0', 0, 1);
465 0 0       0 $color_level = $major_version >= 3 ? 3 : 2;
466             }
467             elsif ($ENV{TERM_PROGRAM} && $ENV{TERM_PROGRAM} eq 'Apple_Terminal') {
468 0         0 $color_level= 2;
469             }
470             elsif ($ENV{TERM} && $ENV{TERM} =~ /\-256(?:color)?\z/i) {
471 0         0 $color_level = 2;
472             }
473             elsif ($ENV{TERM}
474             && ($ENV{TERM} =~ /\A(?:screen|xterm|vt100|rxvt)/i
475             || $ENV{TERM} =~ /color|ansi|cygwin|linux/i)
476             ) {
477 5         17 $color_level = 1;
478             }
479             elsif ($ENV{COLORTERM}) {
480 0         0 $color_level = 1;
481             }
482             else {
483 0 0       0 $color_level = $colored eq 'auto' ? 0 : 1;
484             }
485             }
486 255         744 return $color_level;
487             }
488              
489             sub _load_colors {
490 255     255   592 my ($self, $props) = @_;
491              
492 255         856 $self->{_output_color_level} = $self->_detect_color_level;
493              
494 255         482 my $theme_object;
495 255         512 my $default_theme = 'Material';
496 255         814 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 255         2161 ddp => $self,
502             );
503 255 50       1018 if (!$theme_object) {
504 0 0       0 if ($theme_name ne $default_theme) {
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         0 ddp => $self,
510             );
511             }
512 0 0       0 Data::Printer::Common::_die("Unable to load default theme. This should never happen - please contact the author") unless $theme_object;
513             }
514 255         881 $self->{theme} = $theme_object;
515             }
516              
517             sub _filters_for_type {
518 703     703   1410 my ($self, $type) = @_;
519 703 50       2001 return exists $self->{type_filters}{$type} ? @{ $self->{type_filters}{$type} } : ();
  703         2085  
520             }
521              
522             sub _filters_for_class {
523 923     923   1868 my ($self, $type) = @_;
524 923 100       2632 return exists $self->{class_filters}{$type} ? @{ $self->{class_filters}{$type} } : ();
  146         476  
525             }
526              
527             sub _filters_for_data {
528 661     661   1480 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 661         1313 my $ref_kind = Scalar::Util::reftype($data);
533 661 50       1492 $ref_kind = 'SCALAR' unless $ref_kind;
534              
535             # ref() returns 'Regexp' but reftype() returns 'REGEXP', so we picked one:
536 661 100       1804 $ref_kind = 'Regexp' if $ref_kind eq 'REGEXP';
537              
538 661         1095 my @potential_filters;
539              
540             # first, try class name + full inheritance for a specific name.
541 661         1322 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 661 100 100     1969 if (defined $class && $class eq 'Regexp') {
546 5 50 0     17 if ($ref_kind eq 'Regexp' || ($] < 5.011 && $ref_kind eq 'SCALAR')) {
      33        
547 5         9 $ref_kind = 'Regexp';
548 5         71 undef $class;
549             }
550             }
551 661 100       1549 if (defined $class) {
552 102 100       301 if ($self->class->parent_filters) {
553 101         468 my $linear_ISA = Data::Printer::Common::_linear_ISA_for($class, $self);
554 101         267 foreach my $candidate_class (@$linear_ISA) {
555 159         441 push @potential_filters, $self->_filters_for_class($candidate_class);
556             }
557             }
558             else {
559 1         4 push @potential_filters, $self->_filters_for_class($class);
560             }
561             # next, let any '-class' filters have a go:
562 102         264 push @potential_filters, $self->_filters_for_class('-class');
563             }
564              
565             # then, try regular data filters
566 661         1753 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 661         1693 push @potential_filters, $self->_filters_for_class('-unknown');
571              
572 661         1912 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             sub _see {
589 736     736   1749 my ($self, $data, %options) = @_;
590 736 100       1903 return {} unless ref $data;
591 734         2886 my $id = pack 'J', Scalar::Util::refaddr($data);
592 734 100       2222 if (!exists $self->{_seen}{$id}) {
593 658 100       1698 my $entry = {
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 658 100       2986 $self->{_seen}{$id} = $entry unless $options{tied_parent};
605 658         2484 return { refcount => $entry->{refcount} };
606             }
607 76 100       334 return { refcount => $self->{_seen}{$id}->{refcount} } if $options{seen_override};
608 33         91 return $self->{_seen}{$id};
609             }
610              
611             sub seen {
612 3     3 1 8 my ($self, $data) = @_;
613 3         8 my $id = pack 'J', Scalar::Util::refaddr($data);
614 3         14 return exists $self->{_seen}{$id};
615             }
616              
617             sub unsee {
618 20     20 1 59 my ($self, $data) = @_;
619 20 50 33     89 return unless ref $data && keys %{$self->{_seen}};
  20         83  
620              
621 20         62 my $id = pack 'J', Scalar::Util::refaddr($data);
622 20         95 delete $self->{_seen}{$id};
623 20         74 return;
624             }
625              
626             sub _refcount {
627 74     74   151 my ($self, $data) = @_;
628              
629 74         420 require B;
630 74         120 my $count;
631 74         429 my $rv = B::svref_2object(\$data)->RV;
632 74 100 66     315 if (ref($data) eq 'REF' && ref($$data)) {
633 25         134 $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 50       392 return 0 unless $rv->can( 'REFCNT' );
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         499 return $rv->REFCNT - $self->{_refcount_base};
643             }
644              
645             sub parse_as {
646 42     42 1 131 my ($self, $type, $data) = @_;
647 42         174 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             sub parse {
654 736     736 1 34067 my $self = shift;
655 736         2289 my $str_weak = $self->_check_weak( $_[0] );
656              
657 736         2480 my ($data, %options) = @_;
658 736         1425 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 736         2223 my $seen = $self->_see($data, %options);
664 736 100       2303 if (my $name = $seen->{name}) {
665 33 50 66     161 $parsed_string .= $self->maybe_colorize(
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         111 $parsed_string .= $str_weak;
675 33         133 return $parsed_string;
676             }
677 703         1459 $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 703 100       2344 foreach my $filter (
685             exists $options{force_type}
686             ? $self->_filters_for_type($options{force_type})
687             : $self->_filters_for_data($data)
688             ) {
689 704 100       2615 if (defined (my $result = $filter->($data, $self))) {
690 703         1766 $parsed_string .= $result;
691 703         1656 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 703         1892 $parsed_string .= $self->_check_readonly($data);
699 703 100       2153 $parsed_string .= $str_weak if ref($data) ne 'REF';
700 703         1977 $parsed_string .= $self->_check_memsize($data);
701              
702 703 100 100     1618 if ($self->show_refcount && ref($data) ne 'SCALAR' && $seen->{refcount} > 1 ) {
      100        
703 20         82 $parsed_string .= ' (refcount: ' . $seen->{refcount} .')';
704             }
705              
706 703 100       2019 if (--$self->{'_position'} == 0) {
707 318         1606 $self->{'_seen'} = {};
708 318         707 $self->{'_refcount_base'} = 3;
709 318         687 $self->{'_position'} = 0;
710             }
711              
712 703         4983 return $parsed_string;
713             }
714              
715             sub _check_memsize {
716 703     703   1395 my ($self, $data) = @_;
717             return '' unless $self->show_memsize
718             && ( $self->show_memsize eq 'all'
719 703 50 0     1626 || $self->show_memsize >= $self->{_position});
      33        
720 0         0 my $size;
721             my $unit;
722             my $error = Data::Printer::Common::_tryme(sub {
723 0     0   0 require Devel::Size;
724 0         0 $size = Devel::Size::total_size($data);
725 0         0 $unit = uc $self->memsize_unit;
726 0 0 0     0 if ($unit eq 'M' || ($unit eq 'AUTO' && $size > 1024*1024)) {
    0 0        
      0        
      0        
727 0         0 $size = $size / (1024*1024);
728 0         0 $unit = 'M';
729             }
730             elsif ($unit eq 'K' || ($unit eq 'AUTO' && $size > 1024)) {
731 0         0 $size = $size / 1024;
732 0         0 $unit = 'K';
733             }
734             else {
735 0         0 $unit = 'B';
736             }
737 0         0 });
738 0 0       0 if ($error) {
739 0 0       0 if ($error =~ m{locate Devel/Size.pm}) {
740             Data::Printer::Common::_warn($self, "Devel::Size not found, show_memsize will be ignored")
741 0 0       0 if $self->{_position} == 1;
742             }
743             else {
744 0         0 Data::Printer::Common::_warn($self, "error fetching memory usage: $error");
745             }
746 0         0 return '';
747             }
748 0 0       0 return '' unless $size;
749 0 0       0 my $string = ' (' . ($size < 0 ? sprintf("%.2f", $size) : int($size)) . $unit . ')';
750 0         0 return $self->maybe_colorize($string, 'memsize');
751             }
752              
753             sub _check_weak {
754 736     736   1444 my ($self) = shift;
755 736 100       1869 return '' unless $self->show_weak;
756              
757 698         1862 my $realtype = Scalar::Util::reftype($_[0]);
758 698         1261 my $isweak;
759 698 100 100     3973 if ($realtype && ($realtype eq 'REF' || $realtype eq 'SCALAR')) {
      66        
760 452         997 $isweak = Scalar::Util::isweak($_[0]);
761             }
762             else {
763 246         542 $isweak = Scalar::Util::isweak($_[0]);
764             }
765 698 100       2331 return '' unless $isweak;
766 17         115 return ' ' . $self->maybe_colorize('(weak)', 'weak');
767             }
768              
769             sub _write_label {
770 61     61   132 my ($self) = @_;
771 61 100       168 return '' unless $self->caller_info;
772 2         19 my @caller = caller 1;
773              
774 2         7 my $message = $self->caller_message;
775              
776 2         7 $message =~ s/\b__PACKAGE__\b/$caller[0]/g;
777 2         20 $message =~ s/\b__FILENAME__\b/$caller[1]/g;
778 2         12 $message =~ s/\b__LINE__\b/$caller[2]/g;
779              
780 2 50       7 my $separator = $self->caller_message_newline ? "\n" : ' ';
781 2         16 $message = $self->maybe_colorize($message, 'caller_info');
782 2 100       7 $message = $self->caller_message_position eq 'before'
783             ? $message . $separator
784             : $separator . $message
785             ;
786 2         12 return $message;
787             }
788              
789             sub maybe_colorize {
790 1928     1928 1 23716 my ($self, $output, $color_type, $default_color, $end_color) = @_;
791              
792 1928 100 100     5248 if ($self->{_output_color_level} && defined $color_type) {
793 3         10 my $theme = $self->theme;
794 3         16 my $sgr_color = $theme->sgr_color_for($color_type);
795 3 100 100     16 if (!defined $sgr_color && defined $default_color) {
796 1         5 $sgr_color = $theme->_parse_color($default_color);
797             }
798 3 100       9 if ($sgr_color) {
799 1 50       7 $output = $sgr_color
800             . $output
801             . (defined $end_color
802             ? $theme->sgr_color_for($end_color)
803             : $theme->color_reset
804             );
805             }
806             }
807 1928         6494 return $output;
808             }
809              
810             sub _check_readonly {
811 703     703   1496 my ($self) = @_;
812 703 100 100     1740 return ' (read-only)' if $self->show_readonly && &Internals::SvREADONLY($_[1]);
813 696         1625 return '';
814             }
815              
816             42;
817             __END__
818              
819             =head1 NAME
820              
821             Data::Printer::Object - underlying object for Data::Printer
822              
823             =head1 SYNOPSIS
824              
825             Unless you're writing a plugin, or looking for some
826             L<< configuration property details|/Attributes >>
827             the documentation you want is probably on L<Data::Printer>. Seriously!
828              
829              
830             =head1 DESCRIPTION
831              
832             This module implements the underlying object used by Data::Printer to parse,
833             format and print Perl data structures.
834              
835             It is passed to plugins so they can rely on contextual information from the
836             caller like colors, spacing and other options.
837              
838             =head1 COMMON PROPERTIES / ATTRIBUTES
839              
840             =head2 Scalar Options
841              
842             =head3 show_tainted
843              
844             When set, will detect and let you know of any tainted data (default: 1)
845             Note that this is a no-op unless your script is in taint mode, meaning
846             it's running with different real and effective user/group IDs, or with the
847             -T flag. See L<perlsec> for extra information.
848              
849             =head3 show_unicode
850              
851             Whether to label data that has the L<unicode flag|perlunifaq> set. (default: 1)
852              
853             =head3 show_dualvar
854              
855             Perl can interpret strings as numbers and vice-versa, but that doesn't mean
856             it always gets it right. When this option is set to "lax", Data::Printer will
857             show both values if they differ. If set to "strict", it will always show both
858             values, and when set to "off" it will never show the second value. (default: lax)
859              
860             =head3 show_lvalue
861              
862             Lets you know whenever a value is an lvalue (default: 1)
863              
864             =head3 string_max
865              
866             The maximum number of characters to display in a string. If the string is
867             bigger than that, Data::Printer will trim a part of the string (set by
868             L<string_preserve|/string_preserve>) and replace it with the message set on
869             L<string_overflow|/string_overflow>. Set C<string_max> to 0 to show all
870             characters (default: 4096)
871              
872             =head3 string_overflow
873              
874             Message to display once L<string_max|/string_max> is reached. Defaults to
875             I<< "(...skipping __SKIPPED__ chars...)" >>.
876              
877             =head3 string_preserve
878              
879             When the string has more characters than L<string_max|/string_max>, this
880             option defines which part of the string to preserve. Can be set to 'begin',
881             'middle' or 'end'. (default: 'begin')
882              
883             =head3 scalar_quotes
884              
885             Which quotation character to use when printing strings (default: ")
886              
887             =head3 escape_chars
888              
889             Use this to escape certain characters from strings, which could be useful if
890             your terminal is in a different encoding than the data being printed. Can be
891             set to 'nonascii', 'nonlatin1', 'all' or 'none' (default: none).
892              
893             =head3 unicode_charnames
894              
895             whether to use the character's names when escaping unicode (e.g. SNOWMAN instead of \x{2603}) (default: 0)
896              
897             =head3 print_escapes
898              
899             Whether to print invisible characters in strings, like \b, \n and \t (default: 0)
900              
901             =head3 resolve_scalar_refs
902              
903             If a reference to a scalar value is found more than once, print the resolved
904             value. For example, you may have an object that you reuse to represent 'true'
905             or 'false'. If you have more than one of those in your data, Data::Printer
906             will by default print the second one as a circular reference. When this option
907             is set to true, it will instead resolve the scalar value and keep going. (default: false)
908              
909             =head2 Array Options
910              
911             =head3 array_max
912              
913             The maximum number of array elements to show. If the array is bigger than
914             that, Data::Printer will trim the offending slice (set by
915             L<array_preserve|/array_preserve>) and replace it with the message set on
916             L<array_overflow|/array_overflow>. Set C<array_max> to 0 to show all elements
917             in the array, regardless of array size (default: 100)
918              
919             =head3 array_overflow
920              
921             Message to display once L<array_max|/array_max> is reached. Defaults to
922             C<< "(...skipping __SKIPPED__ items...)" >>.
923              
924             =head3 array_preserve
925              
926             When an array has more elements than L<array_max|/array_max>, this option
927             defines which part of the array to preserve. Can be set to 'begin', 'middle'
928             or 'end'. (default: 'begin')
929              
930             =head3 index
931              
932             When set, shows the index number before each array element. (default: 1)
933              
934             =head2 Hash Options
935              
936             =head3 align_hash
937              
938             If this option is set, hash keys will be vertically aligned by the length
939             of the longest key.
940              
941             This is better explained with an example, so consider the hash
942             C<< my %h = ( a => 123, aaaaaa => 456 ) >>. This would be an unaligned output:
943              
944             a => 123,
945             aaaaaa => 456
946              
947             and this is what it looks like with C<< align_hash = 1 >>:
948              
949             a => 123,
950             aaaaaa => 456
951              
952             (default: 1)
953              
954             =head3 hash_max
955              
956             The maximum number of hash key/value pairs to show. If the hash is bigger than
957             that, Data::Printer will trim the offending slice (set by
958             L<hash_preserve|/hash_preserve>) and replace it with the message set on
959             L<hash_overflow|/hash_overflow>. Set C<hash_max> to 0 to show all elements
960             in the hash, regardless of the total keys. (default: 100)
961              
962             =head3 hash_overflow
963              
964             Message to display once L<hash_max|/hash_max> is reached. Defaults to
965             C<< "(...skipping __SKIPPED__ keys...)" >>.
966              
967             =head3 hash_preserve
968              
969             When a hash has more elements than L<hash_max|/hash_max>, this option
970             defines which part of the hash to preserve. Can be set to 'begin', 'middle'
971             or 'end'. Note that Perl makes no promises regarding key order, so this
972             option only makes sense if keys are sorted. In other words, if
973             you have disabled L<sort_keys|/sort_keys>, expect random keys to be
974             shown regardless of which part was preserved. (default: 'begin')
975              
976             =head3 hash_separator
977              
978             What to use to separate keys from values. Default is ' ' (three spaces)
979              
980             =head3 sort_keys
981              
982             Whether to sort keys when printing the contents of a hash (default: 1)
983              
984             =head3 quote_keys
985              
986             Whether to quote hash keys or not. Can be set to 1 (always quote), 0
987             (never quote) or 'auto' to quote only when a key contains spaces or
988             linebreaks. (default: 'auto')
989              
990              
991             =head2 Caller Information
992              
993             Data::Printer can add an informational message to every call to C<p()> or
994             C<np()> if you enable C<caller_info>. So for example if you write:
995              
996             my $var = "meep!";
997             p $var, caller_info => 1;
998              
999             this will output something like:
1000              
1001             Printing in line 2 of myapp.pl:
1002             "meep!"
1003              
1004             The following options let you customize the message and how it is displayed.
1005              
1006             =head3 caller_info
1007              
1008             Set this option to a true value to display a L<message|/caller_message> next
1009             to the data being printed. (default: 0)
1010              
1011             =head3 caller_message
1012              
1013             What message to print when L<caller_info|/caller_info> is true.
1014              
1015             Defaults to
1016             "C<< Printing in line __LINE__ of __FILENAME__ >>".
1017              
1018             If the special strings C<__LINE__>, C<__FILENAME__> or C<__PACKAGE__> are
1019             present in the message, they'll be interpolated into their according value
1020             so you can customize the message at will:
1021              
1022             caller_message = "[__PACKAGE__:__LINE__]"
1023              
1024             =head3 caller_message_newline
1025              
1026             When true, skips a line when printing L<caller_message|/caller_message>.
1027             When false, only a single space is added between the message and the data.
1028             (default: 1)
1029              
1030             =head3 caller_message_position
1031              
1032             This option controls where the L<caller_message|/caller_message> will appear
1033             in relation to the code being printed. Can be set to 'before' or 'after'. A
1034             line is always skipped between the message and the data (either before or
1035             after), unless you set L<caller_message_newline|/caller_message_newline> to 0.
1036             (default: 'before')
1037              
1038              
1039             =head2 General Options
1040              
1041             =head3 arrows
1042              
1043             Data::Printer shows circular references as a data path, indicating where in
1044             the data that reference points to. You may use this option to control if/when
1045             should it print reference arrows. Possible values are 'all' (e.g
1046             C<< var->{x}->[y]->[z] >>), 'first' (C<< var->{x}[y][z] >>) or 'none'
1047             (C<< var{x}[y][z] >>). Default is 'none'.
1048              
1049             =head3 colored
1050              
1051             Whether to colorize the output or not. Can be set to 1 (always colorize), 0
1052             (never colorize) or 'auto'. Default is 'auto', meaning it will colorize only
1053             when printing to STDOUT or STDERR, never to a file or to a variable. The 'auto'
1054             setting also respects the C<ANSI_COLORS_DISABLED> environment variable.
1055              
1056             =head3 deparse
1057              
1058             If the data structure contains a subroutine reference (coderef), this option
1059             can be set to deparse it and print the underlying code, which hopefully
1060             resembles the original source code. (default: 0)
1061              
1062             =head3 coderef_stub
1063              
1064             If the data structure contains a subroutine reference (coderef) and the
1065             'L<deparse|/deparse>' option above is set to false, Data::Printer will print this
1066             instead. (default: 'C<< sub { ... } >>')
1067              
1068             =head3 coderef_undefined
1069              
1070             If the data structure contains a subroutine reference (coderef) that has
1071             not actually been defined at the time of inspection, Data::Printer will
1072             print this instead. Set it to '0' to disable this check, in which case
1073             Data::Printer will use whatever value you set on
1074             L<coderef_stub|/coderef_stub> above. (default: '<undefined coderef>').
1075              
1076             =head3 end_separator
1077              
1078             When set, the last item on an array or hash will always contain a
1079             trailing L<separator|/separator>. (default: 0)
1080              
1081             =head3 show_memsize
1082              
1083             Set to true and Data::Printer will show the estimate memory size of the data
1084             structure being printed. Requires Devel::Size. (default: 0)
1085              
1086             =head3 memsize_unit
1087              
1088             If L<show_memsize|/show_memsize> is on, this option lets you specify the
1089             unit in which to show the memory size. Can be set to "b" to show size in
1090             bytes, "k" for kilobytes, "m" for megabytes or "auto", which will use the
1091             biggest unit that makes sense. (default: auto)
1092              
1093             =head3 output
1094              
1095             Where you want the output to be printed. Can be set to the following values:
1096              
1097             =over 4
1098              
1099             =item * C<'stderr'> - outputs to the standard error handle.
1100              
1101             =item * C<'stdout'> - outputs to the standard output handle.
1102              
1103             =item * reference to a scalar (e.g. C<\$string>) - outputs to the scalar reference.
1104              
1105             =item * file handle - any open file handle:
1106              
1107             open my $fh, '>>', '/path/to/some/file.log' or die $!;
1108             p @{[ 1,2,3 ]}, output => $fh;
1109              
1110             =item * file path - if you pass a non-empty string that is not 'stderr' nor 'stdout',
1111             Data::Printer will consider it to be a file path and create/append to it automatically
1112             for you. So you can do this in your C<.dataprinter>:
1113              
1114             output = /path/to/some/file.log
1115              
1116             By default, Data::Printer will print to the standard error (stderr).
1117              
1118             =back
1119              
1120             =head3 max_depth
1121              
1122             This setting controls how far inside the data structure we should go
1123             (default: 0 for no depth limit)
1124              
1125             =head3 return_value
1126              
1127             Whether the user wants the return value to be a pass-through of the source
1128             data ('pass'), the dump content itself ('dump') or nothing at all ('void').
1129              
1130             Defaults to C<'pass'> since version 0.36. B<NOTE>: if you set it to 'dump',
1131             make sure it's not the last statement of a subroutine or that, if it is, the
1132             sub is only called in void context.
1133              
1134             =head3 separator
1135              
1136             The separator character(s) to use for arrays and hashes. The default is the
1137             comma ",".
1138              
1139             =head3 show_readonly
1140              
1141             When this option is set, Data::Printer will let you know whenever a value is
1142             read-only. (default: 1)
1143              
1144             =head3 show_refcount
1145              
1146             Whether to show data refcount it's above 1 (default: 0)
1147              
1148             =head3 show_weak
1149              
1150             When this option is set, Data::Printer will let you know whenever it finds a
1151             weak reference (default: 1)
1152              
1153             =head3 show_tied
1154              
1155             When set to true, this option will let you know whenever a tied variable
1156             is detected, including what is tied to it (default: 1)
1157              
1158             =head3 theme
1159              
1160             theme = Monokai
1161              
1162             This setting gets/sets the current color theme module. The default theme
1163             is L<Material|Data::Printer::Theme::Material>. Data::Printer ships with
1164             several themes for you to choose, and you can create your own theme or use
1165             any other from CPAN.
1166              
1167             =head3 warnings
1168              
1169             If something goes wrong when parsing your data or printing it to the selected
1170             output, Data::Printer by default shows you a warning from the standpoint of
1171             the actual call to C<p()> or C<np()>. To silence those warnings, set this
1172             option to 0.
1173              
1174              
1175             =head2 Class / Object Options
1176              
1177             =head3 class_method
1178              
1179             When Data::Printer is printing an object, it first looks for a method
1180             named "C<_data_printer>" and, if one is found, we call it instead of actually
1181             parsing the structure.
1182              
1183             This way, module authors can control how Data::Printer outputs their objects
1184             the best possible way by simply adding a private method instead of having
1185             to write a full filter or even adding Data::Printer as a dependency.
1186              
1187             To disable this behavior, simply set this option to false or an empty string.
1188             You can also change it to a different name and Data::Printer will look for
1189             that instead.
1190              
1191             =head3 class - class properties to override.
1192              
1193             This "namespace" gets/sets all class properties that are used by the
1194             L<standard class filter|Data::Printer::Filter::GenericClass> that ships
1195             with Data::Printer. Note that, if you are using a specific filter for that
1196             object, most (if not all) of the settings below will not apply.
1197              
1198             In your C<.dataprinter> file, the defaults would look like this:
1199              
1200             class.parents = 1
1201             class.linear_isa = auto
1202             class.universal = 0
1203             class.expand = 1
1204             class.stringify = 1
1205             class.show_reftype = 0
1206             class.show_overloads = 1
1207             class.show_methods = all
1208             class.sort_methods = 1
1209             class.inherited = public
1210             class.format_inheritance = lines
1211             class.parent_filters = 1
1212             class.internals = 1
1213              
1214             In code, you should use the "class" namespace as a key to a hash reference:
1215              
1216             use Data::Printer class => {
1217             parents => 1,
1218             linear_isa => 'auto',
1219             universal => 0,
1220             expand => 1,
1221             stringify => 1,
1222             show_reftype => 0,
1223             show_overloads => 1,
1224             show_methods => 'all',
1225             sort_methods => 1,
1226             inherited => 'public',
1227             format_inheritance => 'lines',
1228             parent_filters => 1,
1229             internals => 1,
1230             };
1231              
1232             Or inline:
1233              
1234             p $some_object, class => { internals => 1, ... };
1235              
1236              
1237             =head4 parents
1238              
1239             When set, shows all superclasses of the object being printed. (default: 1)
1240              
1241             =head4 linear_isa
1242              
1243             This setting controls whether to show the linearized @ISA, which is the
1244             order of preference in which the object's methods and attributes are resolved
1245             according to its inheritance. Can be set to 1 (always show), 0 (never show)
1246             or 'auto', which shows only when the object has more than one superclass.
1247             (default: 'auto')
1248              
1249             =head4 universal
1250              
1251             Set this option to 1 to include UNIVERSAL methods to the list of public
1252             methods (like C<can> and C<isa>). (default: 0)
1253              
1254             =head4 expand
1255              
1256             Sets how many levels to descend when printing classes, in case their internals
1257             point to other classes. Set this to 0 to never expand any objects, just show
1258             their name. Set to any integer number and when Data::Printer reaches that
1259             depth, only the class name will be printed. Set to 'all' to always expand
1260             objects found inside your object. (default: 1)
1261              
1262             =head4 stringify
1263              
1264             When this option is set, Data::Printer will check if the object being printed
1265             contains any methods named C<as_string>, C<to_string> or C<stringify>. If it
1266             does, Data::Printer will use it as the object's output instead of the
1267             generic class plugin. (default: 1)
1268              
1269             =head4 show_reftype
1270              
1271             If set to a true value, Data::Printer will show the internal reference type
1272             of the object. (default: 0)
1273              
1274             =head4 show_overloads
1275              
1276             This option includes a list of all overloads implemented by the object.
1277             (default: 1)
1278              
1279             =head4 show_methods
1280              
1281             Controls which of the object's direct methods to show. Can be set to 'none',
1282             'all', 'private' or 'public'. When applicable (Moo, Moose) it will also
1283             show attributes and roles. (default: 'all')
1284              
1285             =head4 sort_methods
1286              
1287             When listing methods, attributes and roles, this option will order them
1288             alphabetically, rather than on whatever order the list of methods returned.
1289             (default: 1)
1290              
1291             =head4 inherited
1292              
1293             Controls which of the object's parent methods to show. Can be set to 'none',
1294             'all', 'private' or 'public'. (default: 'public')
1295              
1296             =head4 format_inheritance
1297              
1298             This option controls how to format the list of methods set by a parent class
1299             (and not the class itself). Setting it to C<'lines'> it will print one line
1300             for each parent, like so:
1301              
1302             public methods (5):
1303             foo, bar
1304             Parent::Class:
1305             baz, meep
1306             Other::Parent:
1307             moop
1308              
1309              
1310             Setting it to C<'string'>, it will put all methods on the same line:
1311              
1312             public methods (5): foo, bar, baz (Parent::Class), meep (Parent::CLass), moop (Other::Parent)
1313              
1314             Default is: 'lines'.
1315              
1316              
1317             =head4 parent_filters
1318              
1319             If there is no filter for the given object's class, there may still be a
1320             filter for one of its parent classes. When this option is set, Data::Printer
1321             will traverse the object's superclass and use the first filter it finds,
1322             if one is present. (default: 1)
1323              
1324             =head4 internals
1325              
1326             Shows the object's internal data structure. (default: 1)
1327              
1328              
1329             =head2 "Shortcuts"
1330              
1331             Some options are so often used together we have created shortcuts for them.
1332              
1333             =head3 as
1334              
1335             p $somevar, as => 'is this right?';
1336              
1337             The "C<as>" shortcut activates L<caller_info|/caller_info> and sets
1338             L<caller_message|/caller_message> to whatever you set it to. It's really
1339             useful to quickly differentiate between sequential uses of C<p()>.
1340              
1341             =head3 multiline
1342              
1343             p $somevar, multiline => 0;
1344              
1345             When set to 0, disables array index and linebreaks, uses ':' as hash separator
1346             and '(...)' as overflow for hashes, arrays and strings, and also disables
1347             'caller_message_newline' so any caller message is shown on the same line as
1348             the variable being printed. If this is set on a global configuration or on the
1349             C<.dataprinter> file, Can be "undone" by setting it to "1".
1350              
1351             =head3 fulldump
1352              
1353             p $somevar, fulldump => 1;
1354              
1355             By default, Data::Printer limits the size of string/array/hash dumps to a
1356             (hopefully) reasonable size. Still, sometimes you really need to see
1357             everything. To completely disable such limits, just set this option to true.
1358              
1359              
1360             =head2 Methods and Accessors for Filter Writers
1361              
1362             The following attributes could be useful if you're writing your own custom
1363             filters or maybe even a non-obvious profile. Otherwise, no need to worry about
1364             any of them ;)
1365              
1366             And make sure to check out the current filter list for real usage examples!
1367              
1368             =head3 indent
1369              
1370             =head3 outdent
1371              
1372             =head3 newline
1373              
1374             These methods are used to control the indentation level of the string being
1375             created to represent your data. While C<indent> and C<outdent> respectively
1376             increase and decrease the indentation level, C<newline> will add a linebreak
1377             and position the "cursor" where you are expected to continue your dump string:
1378              
1379             my $output = $ddp->newline . 'this is a new line';
1380             $ddp->indent;
1381             $output .= $ddp->newline . 'this is indented';
1382             $ddp->outdent;
1383             $output .= $ddp->newline . 'back to our previous indentation!';
1384              
1385             Unless multiline was set to 0, the code above should print something like:
1386              
1387             this is a new line
1388             this is indented
1389             back to our previous indentation
1390              
1391             =head3 extra_config
1392              
1393             Data::Printer will read and pass-through any unrecognized settings in either
1394             your C<.dataprinter> file or your inline arguments inside this structure.
1395             This is useful to create custom settings for your filters.
1396              
1397             While any and all unknown settings will be readable here, we recommend you
1398             prepend them with a namespace like C<filter_xxx> as those are reserved for
1399             filters and thus guaranteed not to colide with any core Data::Printer
1400             settings now or in the future.
1401              
1402             For example, on the L<Web filter|Data::Printer::Filter::Web> we have the
1403             C<expand_headers> option, and even though Data::Printer itself doesn't have
1404             this option, we prepend everything with the C<filter_web> namespace, either
1405             in the config file:
1406              
1407             filter_web.expand_headers = 1
1408              
1409             or inline:
1410              
1411             p $http_response, filters => ['Web'], filter_web => { expand_headers => 1 };
1412              
1413              
1414             =head3 maybe_colorize( $string, $label )
1415              
1416             =head3 maybe_colorize( $string, $label, $default_color )
1417              
1418             my $output = $ddp->maybe_colorize( 12.3, 'number');
1419              
1420             Instead of simply adding raw content to your dump string, you should wrap it
1421             with this method, as it will look up colors on the current theme and print
1422             them (or not, depending on whether the terminal supports color or the user
1423             has explicitly turned them off).
1424              
1425             If you are writing a custom filter and don't want to use the core labels to
1426             colorize your content, you may want to set your own label and pass a default
1427             color. For example:
1428              
1429             my $output = $ddp->maybe_colorize( $data, 'filter_myclass', '#ffccb3' );
1430              
1431             In the code above, if the user has C<colors.filter_myclass> set either on the
1432             C<.dataprinter> file or the runtime hashref, that one will be used. Otherwise,
1433             Data::Printer will use C<'#ffccb3'>.
1434              
1435             =head3 current_depth
1436              
1437             Shows the current depth level, from 0 onwards.
1438              
1439             =head3 current_name
1440              
1441             Gets/sets the name for the current posistion, to be printed when the parser
1442             visits that data again. E.g. C<var[0]{abc}[2]>.
1443              
1444             =head3 parse( $data_ref )
1445              
1446             =head3 parse( $data_ref, %options )
1447              
1448             This method receives a reference to a data structure to parse, and returns the
1449             parsed string. It will call each filter and colorize the output accordingly.
1450              
1451             Use this inside filters whenever you want to use the result of a parsed data
1452             strucure.
1453              
1454             my $output = $ddp->parse( [3,2,1] );
1455              
1456             An optional set of parameters may be passed:
1457              
1458             =over 4
1459              
1460             =item * C<< force_type => $type >> - forces data to be treated as that type,
1461             where $type is the name of the Perl data strucuture as returned by
1462             Scalar::Util::reftype (e.g. 'HASH', 'ARRAY' etc). This is used when a filter
1463             wants to show the internals of blessed data. Otherwise parse would just call
1464             the same filter over and over again.
1465              
1466             =item * C<< seen_override => 1 >> - Data::Printer::Object tries to remember
1467             if it has already seen a data structure before, so it can show the circular
1468             reference instead of entenring an infinite loop. However, there are cases
1469             when you want to print the same data structure twice, like when you're doing
1470             a second pass on a blessed object to print its internals, or if you're using
1471             the same object over and over again. This setting overrides the internal
1472             counter and prints the same data again. Check L<unsee|/unsee( $data )> below
1473             for another way to achieve this.
1474              
1475             =back
1476              
1477             =head3 parse_as( $type, $data_ref )
1478              
1479             This is a convenience method to force some data to be interpreted as a
1480             particular type. It is the same as:
1481              
1482             $ddp->parse( $data, force_type => $type, seen_override => 1 );
1483              
1484             =head2 unsee( $data )
1485              
1486             Sometimes you are writing a filter for data that you know will be repeated
1487             several times, like JSON Boolean objects. To prevent Data::Printer from
1488             showing this content as repeated, you can use the C<unsee> method to make
1489             the current object forget about having ever visited this data.
1490              
1491              
1492             =head1 OBJECT CONSTRUCTION
1493              
1494             You'll most like never need this unless you're planning on extending
1495             Data::Printer itself.
1496              
1497             =head2 new( %options )
1498              
1499             Creates a new Data::Printer::Object instance. It may (optionally) receive a
1500             hash or hash reference with custom settings for any of its properties.
1501              
1502              
1503             =head1 SEE ALSO
1504              
1505             L<Data::Printer>