/usr/share/perl5/Specio/PartialDump.pm is in libspecio-perl 0.33-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 | package Specio::PartialDump;
use strict;
use warnings;
our $VERSION = '0.33';
use Scalar::Util qw( looks_like_number reftype blessed );
use Exporter qw( import );
our @EXPORT_OK = qw( partial_dump );
my $MaxLength = 100;
my $MaxElements = 6;
my $MaxDepth = 2;
sub partial_dump {
my (@args) = @_;
my $dump
= _should_dump_as_pairs(@args)
? _dump_as_pairs( 1, @args )
: _dump_as_list( 1, @args );
if ( length($dump) > $MaxLength ) {
my $max_length = $MaxLength - 3;
$max_length = 0 if $max_length < 0;
substr( $dump, $max_length, length($dump) - $max_length ) = '...';
}
return $dump;
}
sub _should_dump_as_pairs {
my (@what) = @_;
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 ( $depth, @what ) = @_;
my $truncated;
if ( defined $MaxElements and ( @what / 2 ) > $MaxElements ) {
$truncated = 1;
@what = splice( @what, 0, $MaxElements * 2 );
}
return join(
', ', _dump_as_pairs_recursive( $depth, @what ),
( $truncated ? "..." : () )
);
}
sub _dump_as_pairs_recursive {
my ( $depth, @what ) = @_;
return unless @what;
my ( $key, $value, @rest ) = @what;
return (
( _format_key( $depth, $key ) . ': ' . _format( $depth, $value ) ),
_dump_as_pairs_recursive( $depth, @rest ),
);
}
sub _dump_as_list {
my ( $depth, @what ) = @_;
my $truncated;
if ( @what > $MaxElements ) {
$truncated = 1;
@what = splice( @what, 0, $MaxElements );
}
return join(
', ', ( map { _format( $depth, $_ ) } @what ),
( $truncated ? "..." : () )
);
}
sub _format {
my ( $depth, $value ) = @_;
defined($value)
? (
ref($value)
? (
blessed($value)
? _format_object( $depth, $value )
: _format_ref( $depth, $value )
)
: (
looks_like_number($value)
? _format_number( $depth, $value )
: _format_string( $depth, $value )
)
)
: _format_undef( $depth, $value ),
}
sub _format_key {
my ( undef, $key ) = @_;
return $key;
}
sub _format_ref {
my ( $depth, $ref ) = @_;
if ( $depth > $MaxDepth ) {
return overload::StrVal($ref);
}
else {
my $reftype = reftype($ref);
$reftype = 'SCALAR'
if $reftype eq 'REF' || $reftype eq 'LVALUE';
my $method = "_format_" . lc $reftype;
if ( my $sub = __PACKAGE__->can($method) ) {
return $sub->( $depth, $ref );
}
else {
return overload::StrVal($ref);
}
}
}
sub _format_array {
my ( $depth, $array ) = @_;
my $class = blessed($array) || '';
$class .= "=" if $class;
return $class . "[ " . _dump_as_list( $depth + 1, @$array ) . " ]";
}
sub _format_hash {
my ( $depth, $hash ) = @_;
my $class = blessed($hash) || '';
$class .= "=" if $class;
return $class . "{ " . _dump_as_pairs(
$depth + 1,
map { $_ => $hash->{$_} } sort keys %$hash
) . " }";
}
sub _format_scalar {
my ( $depth, $scalar ) = @_;
my $class = blessed($scalar) || '';
$class .= "=" if $class;
return $class . "\\" . _format( $depth + 1, $$scalar );
}
sub _format_object {
my ( $depth, $object ) = @_;
return _format_ref( $depth, $object );
}
sub _format_string {
my ( undef, $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;
_quote($str);
}
sub _quote {
my ($str) = @_;
qq{"$str"};
}
sub _format_undef {"undef"}
sub _format_number {
my ( undef, $value ) = @_;
return "$value";
}
# ABSTRACT: A partially rear-ended copy of Devel::PartialDump without prereqs
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Specio::PartialDump - A partially rear-ended copy of Devel::PartialDump without prereqs
=head1 VERSION
version 0.33
=head1 SYNOPSIS
use Specio::PartialDump qw( partial_dump );
partial_dump( { foo => 42 } );
partial_dump(qw( a b c d e f g ));
partial_dump( foo => 42, bar => [ 1, 2, 3 ], );
=head1 DESCRIPTION
This is a copy of Devel::PartialDump with all the OO bits and prereqs
removed. You may want to use this module in your own code to generate nicely
formatted messages when a type constraint fails.
This module optionally exports one sub, C<partial_dump>. This sub accepts any
number of arguments. If given more than one, it will assume that it's either
been given a list of key/value pairs (to build a hash) or a list of values (to
build an array) and dump them appropriately. Objects and references are
stringified in a sane way.
=for Pod::Coverage partial_dump
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman).
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 SUPPORT
Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
=head1 SOURCE
The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2012 - 2017 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license can be found in the
F<LICENSE> file included with this distribution.
=cut
|