MDOM/Dumper.pm

filter: Statementstmt 72.5% 58 / 80 Branchbran 43.3% 26 / 60 Conditioncond 75.0% 6 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 80.0% 8 / 10 Podpod 100% 4 / 4 totaltotal 63.2% 105 / 166 SCAR 47.8
CC
37
Cov
61%
_element_string
53.1
new
19.6
_dump
12.2
CRAP 119.4
« MDOM/Node.pm ↑ Summary MDOM/Document/Gmake.pm »
1 package MDOM::Dumper;
2  
3 =pod
4  
5 =head1 NAME
6  
7 MDOM::Dumper - Dumping of MDOM trees
8  
9 =head1 SYNOPSIS
10  
11 # Load a document
12 my $Module = MDOM::Document->new( 'MyMakefile' );
13
14 # Create the dumper
15 my $Dumper = MDOM::Dumper->new( $Module );
16
17 # Dump the document
18 $Dumper->print;
19  
20 =head1 DESCRIPTION
21  
22 The MDOM trees in MDOM are quite complex, and getting a dump of their
23 structure for development and debugging purposes is important.
24  
25 This module provides that functionality.
26  
27 The process is relatively simple. Create a dumper object with a
28 particular set of options, and then call one of the dump methods to
29 generate the dump content itself.
30  
31 =head1 METHODS
32  
33 =cut
34  
35 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 14 ▶use Params::Util '_INSTANCE';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 14 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
39 BEGIN {
40 14 ▶ $VERSION = '0.007';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 }
42  
43  
44  
45  
46  
47 #####################################################################
48 # Constructor
49  
50 =pod
51  
52 =head2 new $Element, param => value, ...
53  
54 The C<new> constructor creates a dumper, and takes as argument a single
55 L<MDOM::Element> object of any type to serve as the root of the tree to
56 be dumped, and a number of key-E<gt>value parameters to control the output
57 format of the Dumper. Details of the parameters are listed below.
58  
59 Returns a new C<MDOM::Dumper> object, or C<undef> if the constructor
60 is not passed a correct L<MDOM::Element> root object.
61  
62 =over
63  
64 =item memaddr
65  
66 Should the dumper print the memory addresses of each MDOM element.
67 True/false value, off by default.
68  
69 =item indent
70  
71 Should the structures being dumped be indented. This value is numeric,
72 with the number representing the number of spaces to use when indenting
73 the dumper output. Set to '2' by default.
74  
75 =item class
76  
77 Should the dumper print the full class for each element.
78 True/false value, on by default.
79  
80 =item content
81  
82 Should the dumper show the content of each element. True/false value,
83 on by default.
84  
85 =item whitespace
86  
87 Should the dumper show whitespace tokens. By not showing the copious
88 numbers of whitespace tokens the structure of the code can often be
89 made much clearer. True/false value, on by default.
90  
91 =item comments
92  
93 Should the dumper show comment tokens. In situations where you have
94 a lot of comments, the code can often be made clearer by ignoring
95 comment tokens. True/value value, on by default.
96  
97 =item locations
98  
99 Should the dumper show the location of each token. The values shown are
100 [ line, rowchar, column ]. See L<MDOM::Element/"location"> for a description of
101 what these values really are. True/false value, off by default.
102  
103 =back
104  
105 =cut
106  
107 sub new {
108 69 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=29.8
Pod documented
109 69 ▶ my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Element = &_INSTANCE(shift(), "MDOM::Element") or return undef 0 69 69
110  
111 # Create the object
112 69 my $self = bless {
113 root => $Element,
114 display => {
115 memaddr => '', # Show the refaddr of the item
116 indent => 2, # Indent the structures
117 class => 1, # Show the object class
118 content => 1, # Show the object contents
119 whitespace => 1, # Show whitespace tokens
120 comments => 1, # Show comment tokens
121 locations => 0, # Show token locations
122 },
123 }, $class;
124  
125 # Handle the options
126 69 my %options = map { lc $_ } @_;
127 69 foreach ( keys %{$self->{display}} ) {
128 483 ▶ if ( exists $options{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{$_}) 0 483 483
129 0 ▶ if ( $_ eq 'indent' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'indent') { } 0 0 0
130 0 $self->{display}->{indent} = $options{$_};
131 } else {
132 0 $self->{display}->{$_} = !! $options{$_};
133 }
134 }
135 }
136  
137 69 $self->{indent_string} = join '', (' ' x $self->{display}->{indent});
138  
139 # Try to auto-call index_locations. If it failes, turn of locations display
140 69 ▶ if ( $self->{display}->{locations} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"locations"}) 0 69 69
141 0 ▶ my $Document = $Element->isa('MDOM::Document') ? $Element : $Element->top;
Branch: 1 branch
BranchTrueFalseTotal
$Element->isa('MDOM::Document') ? : 0 0 0
142 0 ▶ if ( $Document->isa('MDOM::Document') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Document->isa('MDOM::Document')) { } 0 0 0
143 0 $Document->index_locations();
144 } else {
145 0 $self->{display}->{locations} = 0;
146 }
147 }
148
149 69 $self;
150 }
151  
152  
153  
154  
155  
156 #####################################################################
157 # Main Interface Methods
158  
159 =pod
160  
161 =head2 print
162  
163 The C<print> method generates the dump and prints it to STDOUT.
164  
165 Returns as for the internal print function.
166  
167 =cut
168  
169 sub print {
170 0 ▶ CORE::print(shift->string);
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
171 }
172  
173 =pod
174  
175 =head2 string
176  
177 The C<string> method generates the dump and provides it as a
178 single string.
179  
180 Returns a string or undef if there is an error while generating the dump.
181  
182 =cut
183  
184 sub string {
185 69 ▶ my $array_ref = shift->_dump or return undef;
Subroutine string: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $array_ref = (shift())->_dump or return undef 0 69 69
186 69 join '', map { "$_\n" } @$array_ref;
187 }
188  
189 =pod
190  
191 =head2 list
192  
193 The C<list> method generates the dump and provides it as a raw
194 list, without trailing newlines.
195  
196 Returns a list or the null list if there is an error while generation
197 the dump.
198  
199 =cut
200  
201 sub list {
202 0 ▶ my $array_ref = shift->_dump or return ();
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $array_ref = (shift())->_dump or return () 0 0 0
203 0 @$array_ref;
204 }
205  
206  
207  
208  
209  
210 #####################################################################
211 # Generation Support Methods
212  
213 sub _dump {
214 861 ▶ my $self = ref $_[0] ? shift : shift->new(shift);
Subroutine _dump: called CC=12 SCAR=25.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 861 0 861
215 861 ▶ my $Element = _INSTANCE($_[0], 'MDOM::Element') ? shift : $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_[0], 'MDOM::Element') ? : 792 69 861
216 861 ▶ my $indent = shift || '';
Condition100%2/2
shift() || ''
l!l
79269
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
217 861 ▶ my $output = shift || [];
Condition100%2/2
shift() || []
l!l
79269
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
218  
219 # Print the element if needed
220 861 my $show = 1;
221 861 ▶ if ( $Element->isa('MDOM::Token::Whitespace') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('MDOM::Token::Whitespace')) { } 279 582 861
elsif ($Element->isa('MDOM::Token::Comment')) { } 16 566 582
222 279 ▶ $show = 0 unless $self->{display}->{whitespace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"display"}{"whitespace"} 0 279 279
223 } elsif ( $Element->isa('MDOM::Token::Comment') ) {
224 16 ▶ $show = 0 unless $self->{display}->{comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"display"}{"comments"} 0 16 16
225 }
226 861 ▶ push @$output, $self->_element_string( $Element, $indent ) if $show;
Branch: 1 branch
BranchTrueFalseTotal
if $show 861 0 861
227  
228 # Recurse into our children
229 861 ▶ if ( $Element->isa('MDOM::Node') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("MDOM::Node")) 187 674 861
230 187 my $child_indent = $indent . $self->{indent_string};
231 187 foreach my $child ( @{$Element->{children}} ) {
232 792 $self->_dump( $child, $child_indent, $output );
233 }
234 }
235  
236 861 $output;
237 }
238  
239 sub _element_string {
240 861 ▶ my $self = ref $_[0] ? shift : shift->new(shift);
Subroutine _element_string: called CC=17 SCAR=39.7
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 861 0 861
241 861 ▶ my $Element = _INSTANCE($_[0], 'MDOM::Element') ? shift : $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
&_INSTANCE($_[0], 'MDOM::Element') ? : 861 0 861
242 861 ▶ my $indent = shift || '';
Condition100%2/2
shift() || ''
l!l
79269
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
243 861 my $string = '';
244  
245 # Add the memory location
246 861 ▶ if ( $self->{display}->{memaddr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"memaddr"}) 0 861 861
247 0 $string .= $Element->refaddr . ' ';
248 }
249
250 # Add the location if such exists
251 861 ▶ if ( $self->{display}->{locations} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"locations"}) 0 861 861
252 0 my $loc_string;
253 0 ▶ if ( $Element->isa('MDOM::Token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Element->isa("MDOM::Token")) 0 0 0
254 0 my $location = $Element->location;
255 0 ▶ if ($location) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location) 0 0 0
256 0 $loc_string = sprintf("[ % 4d, % 3d, % 3d ] ", @$location);
257 }
258 }
259 # Output location or pad with 20 spaces
260 0 ▶ $string .= $loc_string || " " x 20;
Condition0%0/2
$loc_string || ' '
l!l
00
MC/DC0%0/1
$loc_string || ' '
$loc_string
Truth table0%0/2
A
A= $loc_string
Aresult
11
00
261 }
262
263 # Add the indent
264 861 ▶ if ( $self->{display}->{indent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"indent"}) 861 0 861
265 861 $string .= $indent;
266 }
267  
268 # Add the class name
269 861 ▶ if ( $self->{display}->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"class"}) 861 0 861
270 861 $string .= ref $Element;
271 }
272  
273 861 ▶ if ( $Element->isa('MDOM::Token') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Element->isa('MDOM::Token')) { } 674 187 861
elsif ($Element->isa('MDOM::Structure')) { } 0 187 187
274 # Add the content
275 674 ▶ if ( $self->{display}->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"content"}) 674 0 674
276 674 my $content = $Element->content;
277 674 $content =~ s/\n/\\n/g;
278 674 $content =~ s/\t/\\t/g;
279 674 $content =~ s/'/\\'/g;
280 674 $content =~ s/\r/\\r/g;
281 674 $string .= " \t'$content'";
282 }
283 } elsif ( $Element->isa('MDOM::Structure') ) {
284 # Add the content
285 0 ▶ if ( $self->{display}->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"display"}{"content"}) 0 0 0
286 0 ▶ my $start = $Element->start
Branch: 1 branch
BranchTrueFalseTotal
$Element->start ? : 0 0 0
287 ? $Element->start->content
288 : '???';
289 0 ▶ my $finish = $Element->finish
Branch: 1 branch
BranchTrueFalseTotal
$Element->finish ? : 0 0 0
290 ? $Element->finish->content
291 : '???';
292 0 $string .= " \t$start ... $finish";
293 }
294 }
295
296 861 $string;
297 }
298  
299 14 1;
300  
301 =pod
302  
303 =head1 SUPPORT
304  
305 See the L<support section|MDOM/SUPPORT> in the main module.
306  
307 =head1 AUTHOR
308  
309 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
310  
311 Yichun "agentzh" Zhang C<< <agentzh@gmail.com> >>
312  
313 =head1 COPYRIGHT
314  
315 Copyright 2001 - 2006 Adam Kennedy.
316  
317 This program is free software; you can redistribute
318 it and/or modify it under the same terms as Perl itself.
319  
320 The full text of the license can be found in the
321 LICENSE file included with this module.
322  
323 =cut
« MDOM/Node.pm ↑ Summary MDOM/Document/Gmake.pm »