Data-Printer
view release on metacpan or search on metacpan
lib/Data/Printer/Object.pm view on Meta::CPAN
Data::Printer::Object::ClassOptions;
sub parents { $_[0]->{'parents'} }
sub linear_isa { $_[0]->{'linear_isa'} }
sub universal { $_[0]->{'universal'} }
sub expand { $_[0]->{'expand'} }
sub stringify { $_[0]->{'stringify'} }
sub show_reftype { $_[0]->{'show_reftype'} }
sub show_overloads { $_[0]->{'show_overloads'} }
sub show_methods { $_[0]->{'show_methods'} }
sub sort_methods { $_[0]->{'sort_methods'} }
sub show_wrapped { $_[0]->{'show_wrapped'} }
sub inherited { $_[0]->{'inherited'} }
sub format_inheritance { $_[0]->{'format_inheritance'} }
sub parent_filters { $_[0]->{'parent_filters'} }
sub internals { $_[0]->{'internals'} }
sub new {
my ($class, $params) = @_;
my $self = {
'linear_isa' => Data::Printer::Common::_fetch_scalar_or_default($params, 'linear_isa', 'auto'),
'show_reftype' => Data::Printer::Common::_fetch_scalar_or_default($params, 'show_reftype', 0),
'show_overloads' => Data::Printer::Common::_fetch_scalar_or_default($params, 'show_overloads', 1),
'stringify' => Data::Printer::Common::_fetch_scalar_or_default($params, 'stringify', 1),
'expand' => Data::Printer::Common::_fetch_scalar_or_default($params, 'expand', 1),
'show_methods' => Data::Printer::Common::_fetch_anyof(
$params, 'show_methods', 'all', [qw(none all private public)]
),
'inherited' => Data::Printer::Common::_fetch_anyof(
$params, 'inherited', 'public', [qw(none all private public)]
),
'format_inheritance' => Data::Printer::Common::_fetch_anyof(
$params, 'format_inheritance', 'lines', [qw(string lines)]
),
'parent_filters' => Data::Printer::Common::_fetch_scalar_or_default($params, 'parent_filters', 1),
'universal' => Data::Printer::Common::_fetch_scalar_or_default($params, 'universal', 0),
'sort_methods' => Data::Printer::Common::_fetch_scalar_or_default($params, 'sort_methods', 1),
'show_wrapped' => Data::Printer::Common::_fetch_scalar_or_default($params, 'show_wrapped', 1),
'internals' => Data::Printer::Common::_fetch_scalar_or_default($params, 'internals', 1),
'parents' => Data::Printer::Common::_fetch_scalar_or_default($params, 'parents', 1),
};
return bless $self, $class;
}
1;
package Data::Printer::Object;
use Scalar::Util ();
use Data::Printer::Theme;
use Data::Printer::Filter::SCALAR; # also implements LVALUE
use Data::Printer::Filter::ARRAY;
use Data::Printer::Filter::HASH;
use Data::Printer::Filter::REF;
use Data::Printer::Filter::VSTRING;
use Data::Printer::Filter::GLOB;
use Data::Printer::Filter::FORMAT;
use Data::Printer::Filter::Regexp;
use Data::Printer::Filter::CODE;
use Data::Printer::Filter::OBJECT;
use Data::Printer::Filter::GenericClass;
# create our basic accessors:
my @method_names =qw(
name show_tainted show_unicode show_readonly show_lvalue show_refcount
show_memsize memsize_unit print_escapes scalar_quotes escape_chars
caller_info caller_message caller_message_newline caller_message_position
string_max string_overflow string_preserve resolve_scalar_refs
array_max array_overflow array_preserve hash_max hash_overflow
hash_preserve unicode_charnames colored theme show_weak
max_depth index separator end_separator class_method class hash_separator
align_hash sort_keys quote_keys deparse return_value show_dualvar show_tied
warnings arrows coderef_stub coderef_undefined
);
foreach my $method_name (@method_names) {
no strict 'refs';
*{__PACKAGE__ . "::$method_name"} = sub {
$_[0]->{$method_name} = $_[1] if @_ > 1;
return $_[0]->{$method_name};
}
}
sub extra_config { $_[0]->{extra_config} }
sub current_depth { $_[0]->{_depth} }
sub indent { $_[0]->{_depth}++ }
sub outdent { $_[0]->{_depth}-- }
sub newline {
my ($self) = @_;
return $self->{_linebreak}
. (' ' x ($self->{_depth} * $self->{_current_indent}))
. (' ' x $self->{_array_padding})
;
}
sub current_name {
my ($self, $new_value) = @_;
if (defined $new_value) {
$self->{_current_name} = $new_value;
}
else {
$self->{_current_name} = $self->name unless defined $self->{_current_name};
}
return $self->{_current_name};
}
sub _init {
my $self = shift;
my $props = { @_ == 1 ? %{$_[0]} : @_ };
$self->{'_linebreak'} = "\n";
$self->{'_depth'} = 0;
$self->{'_position'} = 0; # depth is for indentation only!
$self->{'_array_padding'} = 0;
$self->{'_seen'} = {};
$self->{_refcount_base} = 3;
$self->{'warnings'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'warning', 1);
$self->{'indent'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'indent', 4);
$self->{'index'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'index', 1);
$self->{'name'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'name', 'var');
$self->{'arrows'} = Data::Printer::Common::_fetch_anyof(
$props,
'arrows',
'none',
[qw(none first all)]
);
$self->{'show_tainted'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_tainted', 1);
$self->{'show_tied'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_tied', 1);
$self->{'show_weak'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_weak', 1);
$self->{'show_unicode'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_unicode', 0);
$self->{'show_readonly'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_readonly', 1);
$self->{'show_lvalue'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_lvalue', 1);
$self->{'show_refcount'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_refcount', 0);
$self->{'show_memsize'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'show_memsize', 0);
$self->{'memsize_unit'} = Data::Printer::Common::_fetch_anyof(
$props,
'memsize_unit',
'auto',
[qw(auto b k m)]
);
$self->{'print_escapes'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'print_escapes', 0);
$self->{'scalar_quotes'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'scalar_quotes', q("));
$self->{'escape_chars'} = Data::Printer::Common::_fetch_anyof(
$props,
'escape_chars',
'none',
[qw(none nonascii nonlatin1 all)]
);
$self->{'caller_info'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'caller_info', 0);
$self->{'caller_message'} = Data::Printer::Common::_fetch_scalar_or_default(
$props,
'caller_message',
'Printing in line __LINE__ of __FILENAME__:'
);
$self->{'caller_message_newline'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'caller_message_newline', 1);
$self->{'caller_message_position'} = Data::Printer::Common::_fetch_anyof($props, 'caller_message_position', 'before', [qw(before after)]);
$self->{'resolve_scalar_refs'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'resolve_scalar_refs', 0);
$self->{'string_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'string_max', 4096);
$self->{'string_preserve'} = Data::Printer::Common::_fetch_anyof(
$props,
'string_preserve',
'begin',
[qw(begin end middle extremes none)]
);
$self->{'string_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
$props,
'string_overflow',
'(...skipping __SKIPPED__ chars...)'
);
$self->{'array_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'array_max', 100);
$self->{'array_preserve'} = Data::Printer::Common::_fetch_anyof(
$props,
'array_preserve',
'begin',
[qw(begin end middle extremes none)]
);
$self->{'array_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
$props,
'array_overflow',
'(...skipping __SKIPPED__ items...)'
);
$self->{'hash_max'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'hash_max', 100);
$self->{'hash_preserve'} = Data::Printer::Common::_fetch_anyof(
$props,
'hash_preserve',
'begin',
[qw(begin end middle extremes none)]
);
$self->{'hash_overflow'} = Data::Printer::Common::_fetch_scalar_or_default(
$props,
'hash_overflow',
'(...skipping __SKIPPED__ keys...)'
);
$self->{'unicode_charnames'} = Data::Printer::Common::_fetch_scalar_or_default(
$props,
'unicode_charnames',
0
);
$self->{'colored'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'colored', 'auto');
$self->{'max_depth'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'max_depth', 0);
$self->{'separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'separator', ',');
$self->{'end_separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'end_separator', 0);
$self->{'class_method'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'class_method', '_data_printer');
$self->{'class'} = Data::Printer::Object::ClassOptions->new($props->{'class'});
$self->{'hash_separator'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'hash_separator', ' ');
$self->{'align_hash'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'align_hash', 1);
$self->{'sort_keys'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'sort_keys', 1);
$self->{'quote_keys'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'quote_keys', 'auto');
$self->{'deparse'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'deparse', 0);
$self->{'coderef_stub'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'coderef_stub', 'sub { ... }');
$self->{'coderef_undefined'} = Data::Printer::Common::_fetch_scalar_or_default($props, 'coderef_undefined', '<undefined coderef>');
$self->{'return_value'} = Data::Printer::Common::_fetch_anyof(
$props,
'return_value',
'pass',
[qw(pass dump void)]
);
$self->{'show_dualvar'} = Data::Printer::Common::_fetch_anyof(
$props,
'show_dualvar',
'lax',
[qw(lax strict off)]
);
if (exists $props->{as}) {
my $msg = Data::Printer::Common::_fetch_scalar_or_default($props, 'as', '');
$self->{caller_info} = 1;
$self->{caller_message} = $msg;
}
$self->multiline(
Data::Printer::Common::_fetch_scalar_or_default($props, 'multiline', 1)
);
$self->fulldump(
Data::Printer::Common::_fetch_scalar_or_default($props, 'fulldump', 0)
);
$self->output(defined $props->{output} ? $props->{output} : 'stderr');
$self->_load_colors($props);
$self->_load_filters($props);
my %extra_config;
my %core_options = map { $_ => 1 }
(@method_names, qw(as multiline output colors filters));
foreach my $key (keys %$props) {
$extra_config{$key} = $props->{$key} unless exists $core_options{$key};
}
$self->{extra_config} = \%extra_config;
return $self;
}
sub output {
my ($self, $new_output) = @_;
if (@_ > 1) {
lib/Data/Printer/Object.pm view on Meta::CPAN
sub maybe_colorize {
my ($self, $output, $color_type, $default_color, $end_color) = @_;
if ($self->{_output_color_level} && defined $color_type) {
my $theme = $self->theme;
my $sgr_color = $theme->sgr_color_for($color_type);
if (!defined $sgr_color && defined $default_color) {
$sgr_color = $theme->_parse_color($default_color);
}
if ($sgr_color) {
$output = $sgr_color
. $output
. (defined $end_color
? $theme->sgr_color_for($end_color)
: $theme->color_reset
);
}
}
return $output;
}
sub _check_readonly {
my ($self) = @_;
return ' (read-only)' if $self->show_readonly && &Internals::SvREADONLY($_[1]);
return '';
}
42;
__END__
=head1 NAME
Data::Printer::Object - underlying object for Data::Printer
=head1 SYNOPSIS
Unless you're writing a plugin, or looking for some
L<< configuration property details|/Attributes >>
the documentation you want is probably on L<Data::Printer>. Seriously!
=head1 DESCRIPTION
This module implements the underlying object used by Data::Printer to parse,
format and print Perl data structures.
It is passed to plugins so they can rely on contextual information from the
caller like colors, spacing and other options.
=head1 COMMON PROPERTIES / ATTRIBUTES
=head2 Scalar Options
=head3 show_tainted
When set, will detect and let you know of any tainted data (default: 1)
Note that this is a no-op unless your script is in taint mode, meaning
it's running with different real and effective user/group IDs, or with the
-T flag. See L<perlsec> for extra information.
=head3 show_unicode
Whether to label data that has the L<unicode flag|perlunifaq> set. (default: 1)
=head3 show_dualvar
Perl can interpret strings as numbers and vice-versa, but that doesn't mean
it always gets it right. When this option is set to "lax", Data::Printer will
show both values if they differ. If set to "strict", it will always show both
values, and when set to "off" it will never show the second value. (default: lax)
=head3 show_lvalue
Lets you know whenever a value is an lvalue (default: 1)
=head3 string_max
The maximum number of characters to display in a string. If the string is
bigger than that, Data::Printer will trim a part of the string (set by
L<string_preserve|/string_preserve>) and replace it with the message set on
L<string_overflow|/string_overflow>. Set C<string_max> to 0 to show all
characters (default: 4096)
=head3 string_overflow
Message to display once L<string_max|/string_max> is reached. Defaults to
I<< "(...skipping __SKIPPED__ chars...)" >>.
=head3 string_preserve
When the string has more characters than L<string_max|/string_max>, this
option defines which part of the string to preserve. Can be set to 'begin',
'middle' or 'end'. (default: 'begin')
=head3 scalar_quotes
Which quotation character to use when printing strings (default: ")
=head3 escape_chars
Use this to escape certain characters from strings, which could be useful if
your terminal is in a different encoding than the data being printed. Can be
set to 'nonascii', 'nonlatin1', 'all' or 'none' (default: none).
=head3 unicode_charnames
whether to use the character's names when escaping unicode (e.g. SNOWMAN instead of \x{2603}) (default: 0)
=head3 print_escapes
Whether to print invisible characters in strings, like \b, \n and \t (default: 0)
=head3 resolve_scalar_refs
If a reference to a scalar value is found more than once, print the resolved
value. For example, you may have an object that you reuse to represent 'true'
or 'false'. If you have more than one of those in your data, Data::Printer
will by default print the second one as a circular reference. When this option
is set to true, it will instead resolve the scalar value and keep going. (default: false)
=head2 Array Options
=head3 array_max
The maximum number of array elements to show. If the array is bigger than
that, Data::Printer will trim the offending slice (set by
L<array_preserve|/array_preserve>) and replace it with the message set on
L<array_overflow|/array_overflow>. Set C<array_max> to 0 to show all elements
in the array, regardless of array size (default: 100)
=head3 array_overflow
Message to display once L<array_max|/array_max> is reached. Defaults to
C<< "(...skipping __SKIPPED__ items...)" >>.
=head3 array_preserve
When an array has more elements than L<array_max|/array_max>, this option
defines which part of the array to preserve. Can be set to 'begin', 'middle'
or 'end'. (default: 'begin')
=head3 index
When set, shows the index number before each array element. (default: 1)
=head2 Hash Options
=head3 align_hash
If this option is set, hash keys will be vertically aligned by the length
of the longest key.
This is better explained with an example, so consider the hash
C<< my %h = ( a => 123, aaaaaa => 456 ) >>. This would be an unaligned output:
a => 123,
aaaaaa => 456
and this is what it looks like with C<< align_hash = 1 >>:
a => 123,
aaaaaa => 456
(default: 1)
=head3 hash_max
( run in 0.544 second using v1.01-cache-2.11-cpan-39bf76dae61 )