Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / PPI / Normal / Standard.pm
1 package PPI::Normal::Standard;
2
3 =pod
4
5 =head1 NAME
6
7 PPI::Normal::Standard - Provides standard document normalization functions
8
9 =head1 DESCRIPTION
10
11 This module provides the default normalization methods for L<PPI::Normal>.
12
13 There is no reason for you to need to load this yourself.
14
15 B<Move along, nothing to see here>.
16
17 =cut
18
19 use strict;
20
21 use vars qw{$VERSION};
22 BEGIN {
23         $VERSION = '1.206';
24 }
25
26
27
28
29
30 #####################################################################
31 # Configuration and Registration
32
33 my @METHODS = (
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,
39 );
40
41 sub import {
42         PPI::Normal->register(
43                 map { /\D/ ? "PPI::Normal::Standard::$_" : $_ } @METHODS
44         ) or die "Failed to register PPI::Normal::Standard transforms";
45 }
46
47
48
49
50
51 #####################################################################
52 # Level 1 Transforms
53
54 # Remove all insignificant elements
55 sub remove_insignificant_elements {
56         my $Document = shift;
57         $Document->prune( sub { ! $_[1]->significant } );
58 }
59
60 # Remove custom attributes that are not relevant to normalization
61 sub remove_useless_attributes {
62         my $Document = shift;
63         delete $Document->{tab_width};
64
65         ### FIXME - Add support for more things
66 }
67
68
69
70
71
72 #####################################################################
73 # Level 2 Transforms
74
75 # Remove version dependencies and pragma
76 my $remove_pragma = map { $_ => 1 } qw{
77         strict warnings diagnostics     less
78         };
79 sub remove_useless_pragma {
80         my $Document = shift;
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};
85                 '';
86         } );
87 }
88
89 # Remove all semi-colons at the end of statements
90 sub remove_statement_separator {
91         my $Document = shift;
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 '';
98                 1;
99         } );
100 }
101
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
104 # return.
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 '';
116                 1;
117         } );
118 }
119
120 1;
121
122 =pod
123
124 =head1 SUPPORT
125
126 See the L<support section|PPI/SUPPORT> in the main module.
127
128 =head1 AUTHOR
129
130 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
131
132 =head1 COPYRIGHT
133
134 Copyright 2005 - 2009 Adam Kennedy.
135
136 This program is free software; you can redistribute
137 it and/or modify it under the same terms as Perl itself.
138
139 The full text of the license can be found in the
140 LICENSE file included with this module.
141
142 =cut