blib/lib/Data/Dumper/EasyOO.pm

filter: Statementstmt 95.8% 140 / 146 Branchbran 80.6% 50 / 62 Conditioncond 92.8% 13 / 14 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 19 / 19 Podpod 100% 4 / 4 totaltotal 92.0% 233 / 253 CC 57 SCAR 19.9
CC
57
Cov
91%
__DONT_TOUCH_THIS
16.2
import
10.4
import
10.4
CRAP 7.3
↑ Summary
1 #!perl
2  
3 package Data::Dumper::EasyOO;
4 22 ▶use Data::Dumper();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use 5.005_03;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 $VERSION = '0.0503';
11  
12 =head1 NAME
13  
14 Data::Dumper::EasyOO - wraps DD for easy use of various printing styles
15  
16 =head1 ABSTRACT
17  
18 EzDD is an object wrapper around Data::Dumper (henceforth just DD),
19 and uses an inner DD object to produce all its output. Its purpose is
20 to make DD's OO capabilities easier to use, ie to make it easy to:
21  
22 1. label your data meaningfully, not just as $VARx
23 2. make and reuse EzDD objects
24 3. customize print styles on any/all of them independently
25 4. provide essentially all of DD's functionality
26 5. do so with fewest keystrokes possible
27  
28 =head1 SYNOPSIS
29  
30 1st, an equivalent to DD's Dumper, which prints exactly like Dumper does
31  
32 use Data::Dumper::EasyOO;
33 print ezdump([1,3]);
34  
35 which prints:
36  
37 $VAR1 = [
38 1,
39 3
40 ];
41  
42 Here, we provide our own (meaningful) label, and use autoprinting, and
43 thereby drop the 'print' from all ezdump calls.
44  
45  
46 use Data::Dumper::EasyOO (autoprint => 1);
47 my $gl = { Joe => 'beer', Betsy => 'wine' });
48 ezdump ( guest_list => $gl);
49  
50 which prints:
51  
52 $guest_list = {
53 'Joe' => 'beer',
54 'Betsy' => 'wine'
55 };
56  
57  
58 And theres much more...
59  
60 =head1 DESCRIPTION
61  
62 EzDD wraps Data::Dumper, and uses an inner DD object to print/dump.
63 By default the output is identical to DD. That said, EzDD gives you a
64 nicer interface, thus encouraging you to tailor DD output the way you
65 like it.
66  
67 A primary design feature of EzDD is that you can choose your preferred
68 printing style in the 'use' statement. EzDD replaces the usual
69 'import' semantics with the same (property => value) pairs as are
70 available in new().
71  
72 You can think of the use statement as a way to set new()'s default
73 behavior once, and reuse those styles (or override and supplement
74 them) on EzDD objects you create thereafter.
75  
76 All of DD's style-setting methods are available in EzDD as both
77 properties to new(), and as object methods; its your choice.
78  
79 =head2 An easy use of ezdump()
80  
81 For maximum laziness support, ezdump() is exported into your
82 namespace, and supports the synopsis example. $ezdump is also
83 exported; it is the EzDD object that ezdump() uses to do its dumping,
84 and allows you to tailor ezdump()s print-style. It also lets you use
85 OO style if you prefer.
86  
87 Continuing from 2nd synopsis example...
88  
89 $ezdump->Set(sortkeys=>1);
90 ezdump ( guest_list => $gl );
91 print "\n";
92 $ezdump->Indent(1);
93 ezdump ( guest_list => $gl );
94  
95 which prints:
96  
97 $guest_list = {
98 'Betsy' => 'wine',
99 'Joe' => 'beer'
100 };
101  
102 $guest_list = {
103 'Betsy' => 'wine',
104 'Joe' => 'beer'
105 };
106  
107 The print-styles are set 2 times; 1st as a property setting, 2nd done
108 like a DD method. The styles accumulate and persist on the object.
109  
110  
111 =cut
112  
113 ;
114 ##############
115 # this (private) reference is passed to the closure to recover
116 # the underlying Data::Dumper object
117 21 my $magic = [];
118 21 my %cliPrefs; # stores style preferences for each client package
119  
120 # DD print-style options/methods/package-vars/attributes.
121 # Theyre delegated to the inner DD object, and 'importable' too.
122  
123 my @styleopts; # used to validate methods in Set()
124  
125 # 5.00503 shipped with DD v2.101
126 21 @styleopts = qw( indent purity pad varname useqq terse freezer
127 toaster deepcopy quotekeys bless );
128  
129 21 ▶push @styleopts, qw( maxdepth )
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Dumper::VERSION ge "2.102" 21 0 21
130 if $Data::Dumper::VERSION ge '2.102'; # with 5.6.1
131  
132 21 ▶push @styleopts, qw( pair useperl sortkeys deparse )
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Dumper::VERSION ge "2.121" 21 0 21
133 if $Data::Dumper::VERSION ge '2.121'; # with 5.6.2
134  
135 # DD methods; also delegated
136 21 my @ddmethods = qw ( Seen Values Names Reset );
137  
138 # EzDD-specific importable style preferences
139 21 my @okPrefs = qw( autoprint init _ezdd_noreset );
140  
141 ##############
142 28 ▶sub import {
Subroutine import: called CC=10 SCAR=23.5
143 # save EzDD client's preferences for use in new()
144 28 my ($pkg, @args) = @_;
145 28 my ($prop, $val, %args);
146 28 my ($alias, @aliases, @ezdds);
147 28 my $caller = caller();
148  
149 # handle aliases, multiples allowed (feeping creaturism)
150  
151 28 foreach my $idx (grep {$args[$_] eq 'alias'} reverse 0..$#args) {
152 6 ($idx, $alias) = splice(@args, $idx, 2);
153 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
154 6 *{$alias.'::new'} = \&{$pkg.'::new'};
155 6 *{$alias.'::import'} = \&{$pkg.'::import'};
156 6 push @aliases, $alias;
157 }
158 # quietly accept 'imports' of things we export anyway
159 28 foreach my $idx (grep {$args[$_] =~ /[\$\&]?ezdump$/} reverse 0..$#args) {
160 0 splice(@args, $idx, 1);
161 }
162  
163 28 ▶ while ($prop = shift(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop = shift @args) 7 28 35
164 7 $val = shift(@args);
165  
166 7 ▶ if (not grep { $_ eq $prop} @styleopts, @okPrefs) {
Branch: 2 branches
BranchTrueFalseTotal
if (not grep({$_ eq $prop;} @styleopts, @okPrefs)) { } 1 6 7
elsif ($prop ne 'init') { } 6 0 6
167 1 carp "unknown print-style: $prop";
168 1 next;
169 }
170 elsif ($prop ne 'init') {
171 6 $args{$prop} = $val;
172 6 push @ezdds, $val;
173 }
174 else {
175 0 ▶ carp "init arg must be a ref to a (scalar) variable"
Branch: 1 branch
BranchTrueFalseTotal
unless ref($val) =~ /SCALAR/ 0 0 0
176 unless ref($val) =~ /SCALAR/;
177  
178 0 ▶ carp "wont construct a new EzDD object into non-undef variable"
Branch: 1 branch
BranchTrueFalseTotal
if defined $$val 0 0 0
179 if defined $$val;
180  
181 0 $$val = Data::Dumper::EasyOO->new(%args);
182 }
183 }
184 28 $cliPrefs{$caller} = \%args; # save the allowed ones
185  
186 # export ezdump() unconditionally
187 # no warnings 'redefine';
188 3 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
189 3 ▶ carp $@, @_ unless $_[0] =~ / redefined/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ / redefined/ 0 3 3
190 28 };
191 22 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
192 28 my $ezdump = $pkg->new(%args);
193 28 ${$caller.'::ezdump'} = $ezdump; # export $ezdump = \&ezdump
194 28 *{$caller.'::ezdump'} = $ezdump; # export ezdump()
195  
196 28 ▶ return (1, \%args) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 27 28
197 27 ▶ return (\%args) if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 1 26 27
198 26 return;
199 }
200  
201 862 ▶sub Set {
Subroutine Set: called CC=7 SCAR=19.5
Pod documented
Pod documented
202 # sets internal state of private data dumper object
203 864 my ($ezdd, %cfg) = @_;
204 864 my $ddo = $ezdd;
205 864 ▶ $ddo = $ezdd->($magic) if ref $ezdd eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ezdd eq __PACKAGE__ 776 86 862
206  
207 864 ▶ $ddo->{_ezdd_noreset} = 1 if $cfg{_ezdd_noreset};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg{"_ezdd_noreset"} 1 861 862
208  
209 862 for my $item (keys %cfg) {
210 #print "$item => $cfg{$item}\n";
211 922 my $attr = lc $item;
212 922 my $meth = ucfirst $item;
213  
214 922 ▶ if (grep {$attr eq $_} @styleopts) {
Branch: 3 branches
BranchTrueFalseTotal
if (grep {$attr eq $_;} @styleopts) { } 823 99 922
elsif (grep {$item eq $_;} @ddmethods) { } 70 29 99
elsif (grep {$attr eq $_;} @okPrefs) { } 23 8 31
215 823 $ddo->$meth($cfg{$item});
216 }
217 396 elsif (grep {$item eq $_} @ddmethods) {
218 70 $ddo->$meth($cfg{$item});
219 }
220 87 elsif (grep {$attr eq $_} @okPrefs) {
221 25 $ddo->{$attr} = $cfg{$item};
222 }
223 6 else { carp "illegal method <$item>" }
224 }
225 862 return $ezdd;
226 }
227  
228 22 ▶use vars '$AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 744 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
231 746 my ($ezdd, $arg) = @_;
232 744 (my $meth = $AUTOLOAD) =~ s/.*:://;
233 744 ▶ return if $meth eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $meth eq "DESTROY" 50 696 746
234 694 my @vals = $ezdd->Set($meth => $arg);
235 694 ▶ return $ezdd unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 691 3 694
236 1 return $ezdd, @vals;
237 }
238  
239 8 ▶sub pp {
Subroutine pp: called CC=1 SCAR=0.0
Pod documented
240 8 my ($ezdd, @data) = @_;
241 8 $ezdd->(@data);
242 }
243  
244 # Im ambivalent about this BEGIN block. Its only use is to suppress
245 # redefined warnings issued when re-do{}'g the file, ie when purposely
246 # avoiding use or require (see t/redefined.t). A more normal
247 # re-importing is already supressed in import(), by the same
248 # (localized) handler.
249  
250 21 local $SIG{__WARN__};
251 22 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.1
252 36 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
253 36 ▶ carp $@, @_ unless $_[0] =~ / redefined/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ / redefined/ 36 0 36
254 22 };
255 22 *dump = \&pp; # causes warning if done outside begin block
256 }
257  
258 1 ▶sub _ez_ddo {
Subroutine _ez_ddo: called CC=1 SCAR=0.0
259 1 my ($ezdd) = @_;
260 1 return $ezdd->($magic);
261 }
262  
263 21 my $_privatePrinter; # visible only to new and closure object it makes
264  
265 86 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
266 88 my ($cls, %cfg) = @_;
267 88 ▶ my $prefs = $cliPrefs{caller()} || {};
Condition100%2/2
$cliPrefs{caller()} || {}
l!l
5729
MC/DC100%1/1
$cliPrefs{caller()} || {}
$cliPrefs{caller()}
Truth table100%2/2
A
A= $cliPrefs{caller()}
Aresult
11
00
268  
269 88 my $ddo = Data::Dumper->new([]); # inner obj w bogus data
270 88 Set($ddo, %$prefs, %cfg); # ctor-params override pkg-config
271  
272 #print "EzDD::new() ", Data::Dumper::Dumper [$prefs, \%cfg];
273  
274 1143 ▶ my $code = sub { # closure on $ddo
Subroutine __ANON__: called CC=1 SCAR=0.0
275 1143 &$_privatePrinter($ddo, @_);
276 90 };
277 # copy constructor
278 88 ▶ bless $code, ref $cls || $cls;
Condition100%3/3
ref $cls || $cls
l!l&&r!l&&!r
2831
MC/DC100%2/2
ref $cls || $cls
ref $cls $cls
Truth table100%3/3
A || B
A= ref $clsB= $cls
ABresult
1X1
011
000
279
280 88 ▶ if (ref $cls) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cls) 2 84 86
281 # clone its settings
282 4 my $ddo = $cls->($magic);
283 4 my %styles;
284 4 @styles{@styleopts,@okPrefs} = @$ddo{@styleopts,@okPrefs};
285 2 $code->Set(%styles,%cfg);
286 }
287 86 return $code;
288 }
289  
290 21 $_privatePrinter = \&__DONT_TOUCH_THIS;
291  
292 1142 ▶sub __DONT_TOUCH_THIS {
Subroutine __DONT_TOUCH_THIS: called CC=16 SCAR=27.9
293 1144 my ($ddo, @args) = @_;
294  
295 1144 ▶ unless ($ddo->{_ezdd_noreset}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ddo->{"_ezdd_noreset"}) 1139 5 1144
296 1141 $ddo->Reset; # clear seen
297 1141 $ddo->Names([]); # clear labels
298 1141 $ddo->Values([]); # clear data
299 }
300 1144 ▶ if (@args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 1077 65 1142
elsif (@args % 2) { } 6 59 65
301 # test for AUTOLOADs special access
302 1077 ▶ return $ddo if defined $args[0] and $args[0] == $magic;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args[0] and $args[0] == $magic 779 298 1077
Condition100%3/3
defined $args[0] and $args[0] == $magic
!ll&&!rl&&r
1299779
MC/DC100%2/2
defined $args[0] and $args[0] == $magic
defined $args[0] $args[0] == $magic
Truth table100%3/3
A and B
A= defined $args[0]B= $args[0] == $magic
ABresult
0X0
100
111
303
304 # else Regular usage
305 298 $ddo->{todump} = \@args;
306 }
307 elsif (@args % 2) {
308 # cant be a hash, must be array of data
309 6 $ddo->{todump} = \@args;
310 }
311 else {
312 # possible labelled usage,
313 # check that all 'labels' are scalars
314
315 59 my %rev = reverse @args;
316 61 ▶ if (grep {ref $_} values %rev) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_;} values %rev) { } 1 58 59
317 # odd elements are refs, must print as array
318 1 $ddo->{todump} = \@args;
319 }
320 else {
321 58 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 77 58 135
322 77 push @{$ddo->{names}}, shift @args;
323 77 push @{$ddo->{todump}}, shift @args;
324 }
325 }
326 }
327 363 ▶ PrintIt:
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 353 10 363
328 # return dump-str unless *void* context
329 return $ddo->Dump() if defined wantarray;
330
331 10 ▶ unless (defined $ddo->{autoprint}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ddo->{"autoprint"}) 2 8 10
332 2 carp "called in void context, without autoprint defined\n";
333 2 return;
334 }
335 8 my $auto = $ddo->{autoprint};
336 # do nothing if autoprint is 0
337 8 ▶ return unless $auto;
Branch: 1 branch
BranchTrueFalseTotal
unless $auto 0 8 8
338
339 # autoprint to STDOUT, STDERR, or HANDLE (IO or GLOB)
340
341 8 ▶ if (ref $auto and (ref $auto eq 'GLOB' or $auto->can("print"))) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $auto and ref $auto eq 'GLOB' || $auto->can('print')) { } 5 3 8
elsif ($auto == 1) { } 0 3 3
elsif ($auto == 2) { } 0 3 3
Condition83%5/6
ref $auto and ref $auto eq "GLOB" || $auto->can("print")
!ll&&!rl&&r
145
ref $auto eq "GLOB" or $auto->can("print")
l!l&&r!l&&!r
502
MC/DC66%2/3
ref $auto and ref $auto eq "GLOB" || $auto->can("print")
ref $auto ref $auto eq "GLOB" $auto->can("print")
Truth table75%3/4
A and B or C
A= ref $autoB= ref $auto eq "GLOB"C= $auto->can("print")
ABCresult
0XX0
1000
11X1
1011
342 5 print $auto $ddo->Dump();
343 }
344 elsif ($auto == 1) {
345 0 print STDOUT $ddo->Dump();
346 }
347 elsif ($auto == 2) {
348 0 print STDERR $ddo->Dump();
349 }
350 else {
351 3 carp "illegal autoprint value: $ddo->{autoprint}";
352 }
353 8 return;
354 };
355  
356  
357 21 1;
358  
359 __END__
↑ Summary