/usr/share/perl5/Devel/PartialDump.pm is in libdevel-partialdump-perl 0.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | #!/usr/bin/perl
package Devel::PartialDump;
use Moose;
use Carp ();
use Scalar::Util qw(looks_like_number reftype blessed);
use namespace::clean -except => 'meta';
our $VERSION = "0.15";
use Sub::Exporter -setup => {
exports => [qw(dump warn show show_scalar croak carp confess cluck $default_dumper)],
groups => {
easy => [qw(dump warn show show_scalar carp croak)],
carp => [qw(croak carp)],
},
collectors => {
override_carp => sub {
no warnings 'redefine';
require Carp::Heavy;
*Carp::caller_info = \&replacement_caller_info;
},
},
};
# a replacement for Carp::caller_info
sub replacement_caller_info {
my $i = shift(@_) + 1;
package DB;
my %call_info;
@call_info{
qw(pack file line sub has_args wantarray evaltext is_require)
} = caller($i);
return unless (defined $call_info{pack});
my $sub_name = Carp::get_subname(\%call_info);
if ($call_info{has_args}) {
$sub_name .= '(' . Devel::PartialDump::dump(@DB::args) . ')';
}
$call_info{sub_name} = $sub_name;
return wantarray() ? %call_info : \%call_info;
}
has max_length => (
isa => "Int",
is => "rw",
predicate => "has_max_length",
clearer => "clear_max_length",
);
has max_elements => (
isa => "Int",
is => "rw",
default => 6,
predicate => "has_max_elements",
clearer => "clear_max_elements",
);
has max_depth => (
isa => "Int",
is => "rw",
required => 1,
default => 2,
);
has stringify => (
isa => "Bool",
is => "rw",
default => 0,
);
has pairs => (
isa => "Bool",
is => "rw",
default => 1,
);
has objects => (
isa => "Bool",
is => "rw",
default => 1,
);
has list_delim => (
isa => "Str",
default => ", ",
is => "rw",
);
has pair_delim => (
isa => "Str",
#default => " => ",
default => ": ",
is => "rw",
);
sub warn_str {
my ( @args ) = @_;
my $self;
if ( blessed($args[0]) and $args[0]->isa(__PACKAGE__) ) {
$self = shift @args;
} else {
$self = our $default_dumper;
}
return $self->_join(
map {
!ref($_) && defined($_)
? $_
: $self->dump($_)
} @args
);
}
sub warn {
Carp::carp(warn_str(@_));
}
foreach my $f ( qw(carp croak confess cluck) ) {
no warnings 'redefine';
eval "sub $f {
local \$Carp::CarpLevel = \$Carp::CarpLevel + 1;
Carp::$f(warn_str(\@_));
}";
}
sub show {
my ( @args ) = @_;
my $self;
if ( blessed($args[0]) and $args[0]->isa(__PACKAGE__) ) {
$self = shift @args;
} else {
$self = our $default_dumper;
}
$self->warn(@args);
return ( @args == 1 ? $args[0] : @args );
}
sub show_scalar ($) { goto \&show }
sub _join {
my ( $self, @strings ) = @_;
my $ret = "";
if ( @strings ) {
my $sep = $, || $" || " ";
my $re = qr/(?: \s| \Q$sep\E )$/x;
my $last = pop @strings;
foreach my $string ( @strings ) {
$ret .= $string;
$ret .= $sep unless $string =~ $re;
}
$ret .= $last;
}
return $ret;
}
sub dump {
my ( @args ) = @_;
my $self;
if ( blessed($args[0]) and $args[0]->isa(__PACKAGE__) ) {
$self = shift @args;
} else {
$self = our $default_dumper;
}
my $method = "dump_as_" . ( $self->should_dump_as_pairs(@args) ? "pairs" : "list" );
my $dump = $self->$method(1, @args);
if ( $self->has_max_length ) {
if ( length($dump) > $self->max_length ) {
$dump = substr($dump, 0, $self->max_length - 3) . "...";
}
}
if ( not defined wantarray ) {
CORE::warn "$dump\n";
} else {
return $dump;
}
}
sub should_dump_as_pairs {
my ( $self, @what ) = @_;
return unless $self->pairs;
return if @what % 2 != 0; # must be an even list
for ( my $i = 0; $i < @what; $i += 2 ) {
return if ref $what[$i]; # plain strings are keys
}
return 1;
}
sub dump_as_pairs {
my ( $self, $depth, @what ) = @_;
my $truncated;
if ( $self->has_max_elements and ( @what / 2 ) > $self->max_elements ) {
$truncated = 1;
@what = splice(@what, 0, $self->max_elements * 2 );
}
return join($self->list_delim, $self->_dump_as_pairs($depth, @what), ($truncated ? "..." : ()) );
}
sub _dump_as_pairs {
my ( $self, $depth, @what ) = @_;
return unless @what;
my ( $key, $value, @rest ) = @what;
return (
( $self->format_key($depth, $key) . $self->pair_delim . $self->format($depth, $value) ),
$self->_dump_as_pairs($depth, @rest),
);
}
sub dump_as_list {
my ( $self, $depth, @what ) = @_;
my $truncated;
if ( $self->has_max_elements and @what > $self->max_elements ) {
$truncated = 1;
@what = splice(@what, 0, $self->max_elements );
}
return join( ", ", ( map { $self->format($depth, $_) } @what ), ($truncated ? "..." : ()) );
}
sub format {
my ( $self, $depth, $value ) = @_;
defined($value)
? ( ref($value)
? ( blessed($value)
? $self->format_object($depth, $value)
: $self->format_ref($depth, $value) )
: ( looks_like_number($value)
? $self->format_number($depth, $value)
: $self->format_string($depth, $value) ) )
: $self->format_undef($depth, $value),
}
sub format_key {
my ( $self, $depth, $key ) = @_;
return $key;
}
sub format_ref {
my ( $self, $depth, $ref ) = @_;
if ( $depth > $self->max_depth ) {
return overload::StrVal($ref);
} else {
my $reftype = reftype($ref);
$reftype = 'SCALAR'
if $reftype eq 'REF' || $reftype eq 'LVALUE';
my $method = "format_" . lc $reftype;
if ( $self->can($method) ) {
return $self->$method( $depth, $ref );
} else {
return overload::StrVal($ref);
}
}
}
sub format_array {
my ( $self, $depth, $array ) = @_;
my $class = blessed($array) || '';
return $class . "[ " . $self->dump_as_list($depth + 1, @$array) . " ]";
}
sub format_hash {
my ( $self, $depth, $hash ) = @_;
my $class = blessed($hash) || '';
return $class . "{ " . $self->dump_as_pairs($depth + 1, map { $_ => $hash->{$_} } sort keys %$hash) . " }";
}
sub format_scalar {
my ( $self, $depth, $scalar ) = @_;
my $class = blessed($scalar) || '';
$class .= "=" if $class;
return $class . "\\" . $self->format($depth + 1, $$scalar);
}
sub format_object {
my ( $self, $depth, $object ) = @_;
if ( $self->objects ) {
return $self->format_ref($depth, $object);
} else {
return $self->stringify ? "$object" : overload::StrVal($object);
}
}
sub format_string {
my ( $self, $depth, $str ) =@_;
# FIXME use String::Escape ?
# remove vertical whitespace
$str =~ s/\n/\\n/g;
$str =~ s/\r/\\r/g;
# reformat nonprintables
$str =~ s/(\P{IsPrint})/"\\x{" . sprintf("%x", ord($1)) . "}"/ge;
$self->quote($str);
}
sub quote {
my ( $self, $str ) = @_;
qq{"$str"};
}
sub format_undef { "undef" }
sub format_number {
my ( $self, $depth, $value ) = @_;
return "$value";
}
our $default_dumper = __PACKAGE__->new;
__PACKAGE__
__END__
=pod
=head1 NAME
Devel::PartialDump - Partial dumping of data structures, optimized for argument
printing.
=head1 SYNOPSIS
use Devel::PartialDump;
sub foo {
print "foo called with args: " . Devel::PartialDump->new->dump(@_);
}
use Devel::PartialDump qw(warn);
# warn is overloaded to create a concise dump instead of stringifying $some_bad_data
warn "this made a boo boo: ", $some_bad_data
=head1 DESCRIPTION
This module is a data dumper optimized for logging of arbitrary parameters.
It attempts to truncate overly verbose data, in a way that is hopefully more
useful for diagnostics warnings than
warn Dumper(@stuff);
Unlike other data dumping modules there are no attempts at correctness or cross
referencing, this is only meant to provide a slightly deeper look into the data
in question.
There is a default recursion limit, and a default truncation of long lists, and
the dump is formatted on one line (new lines in strings are escaped), to aid in
readability.
You can enable it temporarily by importing functions like C<warn>, C<croak> etc
to get more informative errors during development, or even use it as:
BEGIN { local $@; eval "use Devel::PartialDump qw(...)" }
to get DWIM formatting only if it's installed, without introducing a
dependency.
=head1 SAMPLE OUTPUT
=over 4
=item C<< "foo" >>
"foo"
=item C<< "foo" => "bar" >>
foo: "bar"
=item C<< foo => "bar", gorch => [ 1, "bah" ] >>
foo: "bar", gorch: [ 1, "bah" ]
=item C<< [ { foo => ["bar"] } ] >>
[ { foo: ARRAY(0x9b265d0) } ]
=item C<< [ 1 .. 10 ] >>
[ 1, 2, 3, 4, 5, 6, ... ]
=item C<< "foo\nbar" >>
"foo\nbar"
=item C<< "foo" . chr(1) >>
"foo\x{1}"
=back
=head1 ATTRIBUTES
=over 4
=item max_length
The maximum character length of the dump.
Anything bigger than this will be truncated.
Not defined by default.
=item max_elements
The maximum number of elements (array elements or pairs in a hash) to print.
Defualts to 6.
=item max_depth
The maximum level of recursion.
Defaults to 2.
=item stringify
Whether or not to let objects stringify themeslves, instead of using
L<overload/StrVal> to avoid sideffects.
Defaults to false (no overloading).
=item pairs
Whether or not to autodetect named args as pairs in the main C<dump> function.
If this attribute is true, and the top level value list is even sized, and
every odd element is not a reference, then it will dumped as pairs instead of a
list.
=back
=head1 EXPORTS
All exports are optional, nothing is exported by default.
This module uses L<Sub::Exporter>, so exports can be renamed, curried, etc.
=over 4
=item warn
=item show
=item show_scalar
=item croak
=item carp
=item confess
=item cluck
=item dump
See the various methods for behavior documentation.
These methods will use C<$Devel::PartialDump::default_dumper> as the invocant if the
first argument is not blessed and C<isa> L<Devel::PartialDump>, so they can be
used as functions too.
Particularly C<warn> can be used as a drop in replacement for the built in
warn:
warn "blah blah: ", $some_data;
by importing
use Devel::PartialDump qw(warn);
C<$some_data> will be have some of it's data dumped.
=item $default_dumper
The default dumper object to use for export style calls.
Can be assigned to to alter behavior globally.
This is generally useful when using the C<warn> export as a drop in replacement
for C<CORE::warn>.
=back
=head1 METHODS
=over 4
=item warn @blah
A warpper for C<dump> that prints strings plainly.
=item show @blah
=item show_scalar $x
Like C<warn>, but instead of returning the value from C<warn> it returns its
arguments, so it can be used in the middle of an expression.
Note that
my $x = show foo();
will actually evaluaate C<foo> in list context, so if you only want to dump a
single element and retain scalar context use
my $x = show_scalar foo();
which has a prototype of C<$> (as opposed to taking a list).
This is similar to the venerable Ingy's fabulous and amazing L<XXX> module.
=item carp
=item croak
=item confess
=item cluck
Drop in replacements for L<Carp> exports, that format their arguments like
C<warn>.
=item dump @stuff
Returns a one line, human readable, concise dump of @stuff.
If called in void context, will C<warn> with the dump.
Truncates the dump according to C<max_length> if specified.
=item dump_as_list $depth, @stuff
=item dump_as_pairs $depth, @stuff
Dump C<@stuff> using the various formatting functions.
Dump as pairs returns comma delimited pairs with C<< => >> between the key and the value.
Dump as list returns a comma delimited dump of the values.
=item frmat $depth, $value
=item format_key $depth, $key
=item format_object $depth, $object
=item format_ref $depth, $Ref
=item format_array $depth, $array_ref
=item format_hash $depth, $hash_ref
=item format_undef $depth, undef
=item format_string $depth, $string
=item format_number $depth, $number
=item quote $string
The various formatting methods.
You can override these to provide a custom format.
C<format_array> and C<format_hash> recurse with C<$depth + 1> into
C<dump_as_list> and C<dump_as_pairs> respectively.
C<format_ref> delegates to C<format_array> and C<format_hash> and does the
C<max_depth> tracking. It will simply stringify the ref if the recursion limit
has been reached.
=back
=head1 VERSION CONTROL
This module is maintained using git. You can get the latest version from
L<http://github.com/rafl/devel-partialdump>.
=head1 AUTHOR
Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
=head1 COPYRIGHT
Copyright (c) 2008, 2009 Yuval Kogman. All rights reserved
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
=cut
|