PPI/Dumper.pm

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