| 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 | ▶ | use vars qw{$VERSION}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | BEGIN { | ||||||||||||||
| 40 | 7 | ▶ | $VERSION = '1.236'; | ||||||||||||
|
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<PPI::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<PPI::Dumper> object, or C<undef> if the constructor | ||||||||||||||
| 60 | is not passed a correct L<PPI::Element> root object. | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =over | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =item memaddr | ||||||||||||||
| 65 | |||||||||||||||
| 66 | Should the dumper print the memory addresses of each PDOM 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/false 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<PPI::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 | 72 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||
| 109 | 72 | ▶ | my $Element = _INSTANCE(shift, 'PPI::Element') or return undef; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | |||||||||||||||
| 111 | # Create the object | ||||||||||||||
| 112 | 72 | 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 | 72 | my @options = map { lc $_ } @_; # strict hashpairs # https://github.com/adamkennedy/PPI/issues/201 | |||||||||||||
| 127 | 72 | my %options = @options; | |||||||||||||
| 128 | 72 | foreach ( keys %{$self->{display}} ) { | |||||||||||||
| 129 | 504 | ▶ | if ( exists $options{$_} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | ▶ | if ( $_ eq 'indent' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 0 | $self->{display}->{indent} = $options{$_}; | |||||||||||||
| 132 | } else { | ||||||||||||||
| 133 | 0 | $self->{display}->{$_} = !! $options{$_}; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 72 | $self->{indent_string} = join '', (' ' x $self->{display}->{indent}); | |||||||||||||
| 139 | |||||||||||||||
| 140 | 72 | $self; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | |||||||||||||||
| 144 | |||||||||||||||
| 145 | |||||||||||||||
| 146 | |||||||||||||||
| 147 | ##################################################################### | ||||||||||||||
| 148 | # Main Interface Methods | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =pod | ||||||||||||||
| 151 | |||||||||||||||
| 152 | =head2 print | ||||||||||||||
| 153 | |||||||||||||||
| 154 | The C<print> method generates the dump and prints it to STDOUT. | ||||||||||||||
| 155 | |||||||||||||||
| 156 | Returns as for the internal print function. | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =cut | ||||||||||||||
| 159 | |||||||||||||||
| 160 | sub print { | ||||||||||||||
| 161 | 0 | ▶ | CORE::print(shift->string); | ||||||||||||
|
Subroutine
print: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | =pod | ||||||||||||||
| 165 | |||||||||||||||
| 166 | =head2 string | ||||||||||||||
| 167 | |||||||||||||||
| 168 | The C<string> method generates the dump and provides it as a | ||||||||||||||
| 169 | single string. | ||||||||||||||
| 170 | |||||||||||||||
| 171 | Returns a string or undef if there is an error while generating the dump. | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =cut | ||||||||||||||
| 174 | |||||||||||||||
| 175 | sub string { | ||||||||||||||
| 176 | 0 | ▶ | my $array_ref = shift->_dump or return undef; | ||||||||||||
|
Subroutine
string: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 177 | 0 | join '', map { "$_\n" } @$array_ref; | |||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =pod | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =head2 list | ||||||||||||||
| 183 | |||||||||||||||
| 184 | The C<list> method generates the dump and provides it as a raw | ||||||||||||||
| 185 | list, without trailing newlines. | ||||||||||||||
| 186 | |||||||||||||||
| 187 | Returns a list or the null list if there is an error while generating | ||||||||||||||
| 188 | the dump. | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =cut | ||||||||||||||
| 191 | |||||||||||||||
| 192 | sub list { | ||||||||||||||
| 193 | 72 | ▶ | my $array_ref = shift->_dump or return (); | ||||||||||||
|
Subroutine
list: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 194 | 72 | @$array_ref; | |||||||||||||
| 195 | } | ||||||||||||||
| 196 | |||||||||||||||
| 197 | |||||||||||||||
| 198 | |||||||||||||||
| 199 | |||||||||||||||
| 200 | |||||||||||||||
| 201 | ##################################################################### | ||||||||||||||
| 202 | # Generation Support Methods | ||||||||||||||
| 203 | |||||||||||||||
| 204 | sub _dump { | ||||||||||||||
| 205 | 1519 | ▶ | my $self = ref $_[0] ? shift : shift->new(shift); | ||||||||||||
|
Subroutine
_dump: called CC=12 SCAR=25.0
Branch: 1 branch
| |||||||||||||||
| 206 | 1519 | ▶ | my $Element = _INSTANCE($_[0], 'PPI::Element') ? shift : $self->{root}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 207 | 1519 | ▶ | my $indent = shift || ''; | ||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 208 | 1519 | ▶ | my $output = shift || []; | ||||||||||||
|
Condition100%2/2
shift() || []
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 209 | |||||||||||||||
| 210 | # Print the element if needed | ||||||||||||||
| 211 | 1519 | my $show = 1; | |||||||||||||
| 212 | 1519 | ▶ | if ( $Element->isa('PPI::Token::Whitespace') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 213 | 506 | ▶ | $show = 0 unless $self->{display}->{whitespace}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 214 | } elsif ( $Element->isa('PPI::Token::Comment') ) { | ||||||||||||||
| 215 | 9 | ▶ | $show = 0 unless $self->{display}->{comments}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 216 | } | ||||||||||||||
| 217 | 1519 | ▶ | push @$output, $self->_element_string( $Element, $indent ) if $show; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | |||||||||||||||
| 219 | # Recurse into our children | ||||||||||||||
| 220 | 1519 | ▶ | if ( $Element->isa('PPI::Node') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 221 | 419 | my $child_indent = $indent . $self->{indent_string}; | |||||||||||||
| 222 | 419 | foreach my $child ( @{$Element->{children}} ) { | |||||||||||||
| 223 | 1447 | $self->_dump( $child, $child_indent, $output ); | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | 1519 | $output; | |||||||||||||
| 228 | } | ||||||||||||||
| 229 | |||||||||||||||
| 230 | sub _element_string { | ||||||||||||||
| 231 | 1519 | ▶ | my $self = ref $_[0] ? shift : shift->new(shift); | ||||||||||||
|
Subroutine
_element_string: called CC=17 SCAR=34.2
Branch: 1 branch
| |||||||||||||||
| 232 | 1519 | ▶ | my $Element = _INSTANCE($_[0], 'PPI::Element') ? shift : $self->{root}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 233 | 1519 | ▶ | my $indent = shift || ''; | ||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 234 | 1519 | my $string = ''; | |||||||||||||
| 235 | |||||||||||||||
| 236 | # Add the memory location | ||||||||||||||
| 237 | 1519 | ▶ | if ( $self->{display}->{memaddr} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 238 | 0 | $string .= $Element->refaddr . ' '; | |||||||||||||
| 239 | } | ||||||||||||||
| 240 | |||||||||||||||
| 241 | # Add the location if such exists | ||||||||||||||
| 242 | 1519 | ▶ | if ( $self->{display}->{locations} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 243 | 0 | my $loc_string; | |||||||||||||
| 244 | 0 | ▶ | if ( $Element->isa('PPI::Token') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 245 | 0 | my $location = $Element->location; | |||||||||||||
| 246 | 0 | ▶ | if ($location) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 247 | 0 | $loc_string = sprintf("[ % 4d, % 3d, % 3d ] ", @$location); | |||||||||||||
| 248 | } | ||||||||||||||
| 249 | } | ||||||||||||||
| 250 | # Output location or pad with 20 spaces | ||||||||||||||
| 251 | 0 | ▶ | $string .= $loc_string || " " x 20; | ||||||||||||
|
Condition0%0/2
$loc_string || ' '
MC/DC0%0/1
$loc_string || ' '
$loc_string
Truth table0%0/2
A
A= $loc_string
| |||||||||||||||
| 252 | } | ||||||||||||||
| 253 | |||||||||||||||
| 254 | # Add the indent | ||||||||||||||
| 255 | 1519 | ▶ | if ( $self->{display}->{indent} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 256 | 1519 | $string .= $indent; | |||||||||||||
| 257 | } | ||||||||||||||
| 258 | |||||||||||||||
| 259 | # Add the class name | ||||||||||||||
| 260 | 1519 | ▶ | if ( $self->{display}->{class} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 261 | 1519 | $string .= ref $Element; | |||||||||||||
| 262 | } | ||||||||||||||
| 263 | |||||||||||||||
| 264 | 1519 | ▶ | if ( $Element->isa('PPI::Token') ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 265 | # Add the content | ||||||||||||||
| 266 | 1100 | ▶ | if ( $self->{display}->{content} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 267 | 1100 | my $content = $Element->content; | |||||||||||||
| 268 | 1100 | $content =~ s/\n/\\n/g; | |||||||||||||
| 269 | 1100 | $content =~ s/\t/\\t/g; | |||||||||||||
| 270 | 1100 | $content =~ s/\f/\\f/g; | |||||||||||||
| 271 | 1100 | $string .= " \t'$content'"; | |||||||||||||
| 272 | } | ||||||||||||||
| 273 | |||||||||||||||
| 274 | } elsif ( $Element->isa('PPI::Structure') ) { | ||||||||||||||
| 275 | # Add the content | ||||||||||||||
| 276 | 123 | ▶ | if ( $self->{display}->{content} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 277 | 123 | ▶ | my $start = $Element->start | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 278 | ? $Element->start->content | ||||||||||||||
| 279 | : '???'; | ||||||||||||||
| 280 | 123 | ▶ | my $finish = $Element->finish | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 281 | ? $Element->finish->content | ||||||||||||||
| 282 | : '???'; | ||||||||||||||
| 283 | 123 | $string .= " \t$start ... $finish"; | |||||||||||||
| 284 | } | ||||||||||||||
| 285 | } | ||||||||||||||
| 286 | |||||||||||||||
| 287 | 1519 | $string; | |||||||||||||
| 288 | } | ||||||||||||||
| 289 | |||||||||||||||
| 290 | 7 | 1; | |||||||||||||
| 291 | |||||||||||||||
| 292 | =pod | ||||||||||||||
| 293 | |||||||||||||||
| 294 | =head1 SUPPORT | ||||||||||||||
| 295 | |||||||||||||||
| 296 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||
| 297 | |||||||||||||||
| 298 | =head1 AUTHOR | ||||||||||||||
| 299 | |||||||||||||||
| 300 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||
| 301 | |||||||||||||||
| 302 | =head1 COPYRIGHT | ||||||||||||||
| 303 | |||||||||||||||
| 304 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||||||
| 305 | |||||||||||||||
| 306 | This program is free software; you can redistribute | ||||||||||||||
| 307 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 308 | |||||||||||||||
| 309 | The full text of the license can be found in the | ||||||||||||||
| 310 | LICENSE file included with this module. | ||||||||||||||
| 311 | |||||||||||||||
| 312 | =cut | ||||||||||||||