File Coverage

blib/lib/Specio/PartialDump.pm
Criterion Covered Total %
statement 73 80 91.2
branch 31 40 77.5
condition 9 12 75.0
subroutine 20 20 100.0
pod 0 1 0.0
total 133 153 86.9


line stmt bran cond sub pod time code
1             package Specio::PartialDump;
2              
3 32     32   217 use strict;
  32         61  
  32         1191  
4 32     32   139 use warnings;
  32         51  
  32         2225  
5              
6             our $VERSION = '0.53';
7              
8 32     32   183 use Scalar::Util qw( looks_like_number reftype blessed );
  32         50  
  32         2053  
9              
10 32     32   162 use Exporter qw( import );
  32         57  
  32         42306  
11              
12             our @EXPORT_OK = qw( partial_dump );
13              
14             my $MaxLength = 100;
15             my $MaxElements = 6;
16             my $MaxDepth = 2;
17              
18             sub partial_dump {
19 2485     2485 0 8029 my (@args) = @_;
20              
21 2485 50       8034 my $dump
22             = _should_dump_as_pairs(@args)
23             ? _dump_as_pairs( 1, @args )
24             : _dump_as_list( 1, @args );
25              
26 2485 100       14220 if ( length($dump) > $MaxLength ) {
27 2         5 my $max_length = $MaxLength - 3;
28 2 50       8 $max_length = 0 if $max_length < 0;
29 2         11 substr( $dump, $max_length, length($dump) - $max_length ) = '...';
30             }
31              
32 2485         20086 return $dump;
33             }
34              
35             sub _should_dump_as_pairs {
36 2485     2485   6222 my (@what) = @_;
37              
38 2485 50       18898 return if @what % 2 != 0; # must be an even list
39              
40 0         0 for ( my $i = 0; $i < @what; $i += 2 ) {
41 0 0       0 return if ref $what[$i]; # plain strings are keys
42             }
43              
44 0         0 return 1;
45             }
46              
47             sub _dump_as_pairs {
48 316     316   870 my ( $depth, @what ) = @_;
49              
50 316         561 my $truncated;
51 316 50 33     2111 if ( defined $MaxElements and ( @what / 2 ) > $MaxElements ) {
52 0         0 $truncated = 1;
53 0         0 @what = splice( @what, 0, $MaxElements * 2 );
54             }
55              
56 316 50       1006 return join(
57             ', ', _dump_as_pairs_recursive( $depth, @what ),
58             ( $truncated ? "..." : () )
59             );
60             }
61              
62             sub _dump_as_pairs_recursive {
63 508     508   1240 my ( $depth, @what ) = @_;
64              
65 508 100       4222 return unless @what;
66              
67 192         452 my ( $key, $value, @rest ) = @what;
68              
69             return (
70 192         622 ( _format_key( $depth, $key ) . ': ' . _format( $depth, $value ) ),
71             _dump_as_pairs_recursive( $depth, @rest ),
72             );
73             }
74              
75             sub _dump_as_list {
76 2751     2751   8514 my ( $depth, @what ) = @_;
77              
78 2751         4792 my $truncated;
79 2751 50       8474 if ( @what > $MaxElements ) {
80 0         0 $truncated = 1;
81 0         0 @what = splice( @what, 0, $MaxElements );
82             }
83              
84             return join(
85 2751 50       7851 ', ', ( map { _format( $depth, $_ ) } @what ),
  2789         8193  
86             ( $truncated ? "..." : () )
87             );
88             }
89              
90             sub _format {
91 3887     3887   8158 my ( $depth, $value ) = @_;
92              
93 3887 100       26736 defined($value)
    100          
    100          
    100          
94             ? (
95             ref($value)
96             ? (
97             blessed($value)
98             ? _format_object( $depth, $value )
99             : _format_ref( $depth, $value )
100             )
101             : (
102             looks_like_number($value)
103             ? _format_number( $depth, $value )
104             : _format_string( $depth, $value )
105             )
106             )
107             : _format_undef( $depth, $value ),;
108             }
109              
110             sub _format_key {
111 192     192   606 my ( undef, $key ) = @_;
112 192         582 return $key;
113             }
114              
115             sub _format_ref {
116 2076     2076   4571 my ( $depth, $ref ) = @_;
117              
118 2076 100       5042 if ( $depth > $MaxDepth ) {
119 22         79 return overload::StrVal($ref);
120             }
121             else {
122 2054         4799 my $reftype = reftype($ref);
123 2054 100 66     11187 $reftype = 'SCALAR'
124             if $reftype eq 'REF' || $reftype eq 'LVALUE';
125 2054         5673 my $method = "_format_" . lc $reftype;
126              
127 2054 100       16470 if ( my $sub = __PACKAGE__->can($method) ) {
128 1488         4287 return $sub->( $depth, $ref );
129             }
130             else {
131 566         2343 return overload::StrVal($ref);
132             }
133             }
134             }
135              
136             sub _format_array {
137 266     266   659 my ( $depth, $array ) = @_;
138              
139 266   100     1158 my $class = blessed($array) || '';
140 266 100       836 $class .= "=" if $class;
141              
142 266         1607 return $class . "[ " . _dump_as_list( $depth + 1, @$array ) . " ]";
143             }
144              
145             sub _format_hash {
146 316     316   813 my ( $depth, $hash ) = @_;
147              
148 316   100     1479 my $class = blessed($hash) || '';
149 316 100       1086 $class .= "=" if $class;
150              
151             return $class . "{ " . _dump_as_pairs(
152             $depth + 1,
153 316         1943 map { $_ => $hash->{$_} } sort keys %$hash
  192         821  
154             ) . " }";
155             }
156              
157             sub _format_scalar {
158 906     906   2050 my ( $depth, $scalar ) = @_;
159              
160 906   100     3555 my $class = blessed($scalar) || '';
161 906 100       3298 $class .= "=" if $class;
162              
163 906         4025 return $class . "\\" . _format( $depth + 1, $$scalar );
164             }
165              
166             sub _format_object {
167 1159     1159   3120 my ( $depth, $object ) = @_;
168              
169 1159         3788 return _format_ref( $depth, $object );
170             }
171              
172             sub _format_string {
173 505     505   1584 my ( undef, $str ) = @_;
174              
175             # FIXME use String::Escape ?
176              
177             # remove vertical whitespace
178 505         1706 $str =~ s/\n/\\n/g;
179 505         1280 $str =~ s/\r/\\r/g;
180              
181             # reformat nonprintables
182 505         2719 $str =~ s/(\P{IsPrint})/"\\x{" . sprintf("%x", ord($1)) . "}"/ge;
  4         38  
183              
184 505         1705 _quote($str);
185             }
186              
187             sub _quote {
188 505     505   1272 my ($str) = @_;
189              
190 505         4604 qq{"$str"};
191             }
192              
193 188     188   1733 sub _format_undef {"undef"}
194              
195             sub _format_number {
196 1118     1118   2774 my ( undef, $value ) = @_;
197 1118         10021 return "$value";
198             }
199              
200             # ABSTRACT: A partially rear-ended copy of Devel::PartialDump without prereqs
201              
202             1;
203              
204             __END__
205              
206             =pod
207              
208             =encoding UTF-8
209              
210             =head1 NAME
211              
212             Specio::PartialDump - A partially rear-ended copy of Devel::PartialDump without prereqs
213              
214             =head1 VERSION
215              
216             version 0.53
217              
218             =head1 SYNOPSIS
219              
220             use Specio::PartialDump qw( partial_dump );
221              
222             partial_dump( { foo => 42 } );
223             partial_dump(qw( a b c d e f g ));
224             partial_dump( foo => 42, bar => [ 1, 2, 3 ], );
225              
226             =head1 DESCRIPTION
227              
228             This is a copy of Devel::PartialDump with all the OO bits and prereqs
229             removed. You may want to use this module in your own code to generate nicely
230             formatted messages when a type constraint fails.
231              
232             This module optionally exports one sub, C<partial_dump>. This sub accepts any
233             number of arguments. If given more than one, it will assume that it's either
234             been given a list of key/value pairs (to build a hash) or a list of values (to
235             build an array) and dump them appropriately. Objects and references are
236             stringified in a sane way.
237              
238             =for Pod::Coverage partial_dump
239              
240             =head1 SUPPORT
241              
242             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
243              
244             =head1 SOURCE
245              
246             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
247              
248             =head1 AUTHOR
249              
250             Dave Rolsky <autarch@urth.org>
251              
252             =head1 COPYRIGHT AND LICENSE
253              
254             This software is copyright (c) 2008 by יובל קוג'מן (Yuval Kogman).
255              
256             This is free software; you can redistribute it and/or modify it under
257             the same terms as the Perl 5 programming language system itself.
258              
259             =cut