1 package PPI::Normal::Standard;
7 PPI::Normal::Standard - Provides standard document normalization functions
11 This module provides the default normalization methods for L<PPI::Normal>.
13 There is no reason for you to need to load this yourself.
15 B<Move along, nothing to see here>.
21 use vars qw{$VERSION};
30 #####################################################################
31 # Configuration and Registration
34 remove_insignificant_elements => 1,
35 remove_useless_attributes => 1,
36 remove_useless_pragma => 2,
37 remove_statement_separator => 2,
38 remove_useless_return => 2,
42 PPI::Normal->register(
43 map { /\D/ ? "PPI::Normal::Standard::$_" : $_ } @METHODS
44 ) or die "Failed to register PPI::Normal::Standard transforms";
51 #####################################################################
54 # Remove all insignificant elements
55 sub remove_insignificant_elements {
57 $Document->prune( sub { ! $_[1]->significant } );
60 # Remove custom attributes that are not relevant to normalization
61 sub remove_useless_attributes {
63 delete $Document->{tab_width};
65 ### FIXME - Add support for more things
72 #####################################################################
75 # Remove version dependencies and pragma
76 my $remove_pragma = map { $_ => 1 } qw{
77 strict warnings diagnostics less
79 sub remove_useless_pragma {
81 $Document->prune( sub {
82 return '' unless $_[1]->isa('PPI::Statement::Include');
83 return 1 if $_[1]->version;
84 return 1 if $remove_pragma->{$_[1]->pragma};
89 # Remove all semi-colons at the end of statements
90 sub remove_statement_separator {
92 $Document->prune( sub {
93 $_[1]->isa('PPI::Token::Structure') or return '';
94 $_[1]->content eq ';' or return '';
95 my $stmt = $_[1]->parent or return '';
96 $stmt->isa('PPI::Statement') or return '';
97 $_[1]->next_sibling and return '';
102 # In any block, the "return" in the last statement is not
103 # needed if there is only one and only one thing after the
105 sub remove_useless_return {
106 my $Document = shift;
107 $Document->prune( sub {
108 $_[1]->isa('PPI::Token::Word') or return '';
109 $_[1]->content eq 'return' or return '';
110 my $stmt = $_[1]->parent or return '';
111 $stmt->isa('PPI::Statement::Break') or return '';
112 $stmt->children == 2 or return '';
113 $stmt->next_sibling and return '';
114 my $block = $stmt->parent or return '';
115 $block->isa('PPI::Structure::Block') or return '';
126 See the L<support section|PPI/SUPPORT> in the main module.
130 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
134 Copyright 2005 - 2009 Adam Kennedy.
136 This program is free software; you can redistribute
137 it and/or modify it under the same terms as Perl itself.
139 The full text of the license can be found in the
140 LICENSE file included with this module.