Devel::PartialDump - Partial dumping of data structures, optimized for argument


Devel-PartialDump documentation Contained in the Devel-PartialDump distribution.

Index


Code Index:

NAME

Top

Devel::PartialDump - Partial dumping of data structures, optimized for argument printing.

SYNOPSIS

Top

	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

DESCRIPTION

Top

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 warn, 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.

SAMPLE OUTPUT

Top

"foo"
    "foo"

"foo" => "bar"
    foo: "bar"

foo => "bar", gorch => [ 1, "bah" ]
    foo: "bar", gorch: [ 1, "bah" ]

[ { foo => ["bar"] } ]
    [ { foo: ARRAY(0x9b265d0) } ]

[ 1 .. 10 ]
    [ 1, 2, 3, 4, 5, 6, ... ]

"foo\nbar"
    "foo\nbar"

"foo" . chr(1)
    "foo\x{1}"

ATTRIBUTES

Top

max_length

The maximum character length of the dump.

Anything bigger than this will be truncated.

Not defined by default.

max_elements

The maximum number of elements (array elements or pairs in a hash) to print.

Defualts to 6.

max_depth

The maximum level of recursion.

Defaults to 2.

stringify

Whether or not to let objects stringify themeslves, instead of using StrVal in overload to avoid sideffects.

Defaults to false (no overloading).

pairs

Whether or not to autodetect named args as pairs in the main 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.

EXPORTS

Top

All exports are optional, nothing is exported by default.

This module uses Sub::Exporter, so exports can be renamed, curried, etc.

warn
show
show_scalar
croak
carp
confess
cluck
dump

See the various methods for behavior documentation.

These methods will use $Devel::PartialDump::default_dumper as the invocant if the first argument is not blessed and isa Devel::PartialDump, so they can be used as functions too.

Particularly 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);

$some_data will be have some of it's data dumped.

$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 warn export as a drop in replacement for CORE::warn.

METHODS

Top

warn @blah

A warpper for dump that prints strings plainly.

show @blah
show_scalar $x

Like warn, but instead of returning the value from 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 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 $ (as opposed to taking a list).

This is similar to the venerable Ingy's fabulous and amazing XXX module.

carp
croak
confess
cluck

Drop in replacements for Carp exports, that format their arguments like warn.

dump @stuff

Returns a one line, human readable, concise dump of @stuff.

If called in void context, will warn with the dump.

Truncates the dump according to max_length if specified.

dump_as_list $depth, @stuff
dump_as_pairs $depth, @stuff

Dump @stuff using the various formatting functions.

Dump as pairs returns comma delimited pairs with => between the key and the value.

Dump as list returns a comma delimited dump of the values.

frmat $depth, $value
format_key $depth, $key
format_object $depth, $object
format_ref $depth, $Ref
format_array $depth, $array_ref
format_hash $depth, $hash_ref
format_undef $depth, undef
format_string $depth, $string
format_number $depth, $number
quote $string

The various formatting methods.

You can override these to provide a custom format.

format_array and format_hash recurse with $depth + 1 into dump_as_list and dump_as_pairs respectively.

format_ref delegates to format_array and format_hash and does the max_depth tracking. It will simply stringify the ref if the recursion limit has been reached.

VERSION CONTROL

Top

This module is maintained using git. You can get the latest version from http://github.com/rafl/devel-partialdump.

AUTHOR

Top

Yuval Kogman <nothingmuch@woobling.org>

COPYRIGHT

Top


Devel-PartialDump documentation Contained in the Devel-PartialDump distribution.

#!/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__