Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Utils/PPI.pm |
Statements | Executed 17 statements in 773µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 14µs | 14µs | BEGIN@10 | Perl::Critic::Utils::PPI::
1 | 1 | 1 | 7µs | 28µs | BEGIN@14 | Perl::Critic::Utils::PPI::
1 | 1 | 1 | 7µs | 12µs | BEGIN@18 | Perl::Critic::Utils::PPI::
1 | 1 | 1 | 7µs | 35µs | BEGIN@16 | Perl::Critic::Utils::PPI::
1 | 1 | 1 | 7µs | 10µs | BEGIN@12 | Perl::Critic::Utils::PPI::
1 | 1 | 1 | 6µs | 17µs | BEGIN@11 | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | _constant_name_from_constant_pragma | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | get_constant_name_element_from_declaring_statement | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | get_next_element_in_same_simple_statement | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | get_previous_module_used_on_same_line | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_in_subroutine | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_ppi_constant_element | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_ppi_expression_or_generic_statement | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_ppi_generic_statement | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_ppi_simple_statement | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_ppi_statement_subclass | Perl::Critic::Utils::PPI::
0 | 0 | 0 | 0s | 0s | is_subroutine_declaration | Perl::Critic::Utils::PPI::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | ############################################################################## | ||||
2 | # $URL$ | ||||
3 | # $Date$ | ||||
4 | # $Author$ | ||||
5 | # $Revision$ | ||||
6 | ############################################################################## | ||||
7 | |||||
8 | package Perl::Critic::Utils::PPI; | ||||
9 | |||||
10 | 2 | 36µs | 1 | 14µs | # spent 14µs within Perl::Critic::Utils::PPI::BEGIN@10 which was called:
# once (14µs+0s) by Perl::Critic::Utils::BEGIN@26 at line 10 # spent 14µs making 1 call to Perl::Critic::Utils::PPI::BEGIN@10 |
11 | 2 | 17µs | 2 | 28µs | # spent 17µs (6+11) within Perl::Critic::Utils::PPI::BEGIN@11 which was called:
# once (6µs+11µs) by Perl::Critic::Utils::BEGIN@26 at line 11 # spent 17µs making 1 call to Perl::Critic::Utils::PPI::BEGIN@11
# spent 11µs making 1 call to strict::import |
12 | 2 | 17µs | 2 | 14µs | # spent 10µs (7+4) within Perl::Critic::Utils::PPI::BEGIN@12 which was called:
# once (7µs+4µs) by Perl::Critic::Utils::BEGIN@26 at line 12 # spent 10µs making 1 call to Perl::Critic::Utils::PPI::BEGIN@12
# spent 4µs making 1 call to warnings::import |
13 | |||||
14 | 2 | 21µs | 2 | 48µs | # spent 28µs (7+21) within Perl::Critic::Utils::PPI::BEGIN@14 which was called:
# once (7µs+21µs) by Perl::Critic::Utils::BEGIN@26 at line 14 # spent 28µs making 1 call to Perl::Critic::Utils::PPI::BEGIN@14
# spent 21µs making 1 call to Exporter::import |
15 | |||||
16 | 2 | 21µs | 2 | 63µs | # spent 35µs (7+28) within Perl::Critic::Utils::PPI::BEGIN@16 which was called:
# once (7µs+28µs) by Perl::Critic::Utils::BEGIN@26 at line 16 # spent 35µs making 1 call to Perl::Critic::Utils::PPI::BEGIN@16
# spent 28µs making 1 call to Exporter::import |
17 | |||||
18 | 2 | 644µs | 2 | 17µs | # spent 12µs (7+5) within Perl::Critic::Utils::PPI::BEGIN@18 which was called:
# once (7µs+5µs) by Perl::Critic::Utils::BEGIN@26 at line 18 # spent 12µs making 1 call to Perl::Critic::Utils::PPI::BEGIN@18
# spent 5µs making 1 call to Exporter::import |
19 | |||||
20 | 1 | 600ns | our $VERSION = '1.121'; | ||
21 | |||||
22 | #----------------------------------------------------------------------------- | ||||
23 | |||||
24 | 1 | 2µs | our @EXPORT_OK = qw( | ||
25 | is_ppi_expression_or_generic_statement | ||||
26 | is_ppi_generic_statement | ||||
27 | is_ppi_statement_subclass | ||||
28 | is_ppi_simple_statement | ||||
29 | is_ppi_constant_element | ||||
30 | is_subroutine_declaration | ||||
31 | is_in_subroutine | ||||
32 | get_constant_name_element_from_declaring_statement | ||||
33 | get_next_element_in_same_simple_statement | ||||
34 | get_previous_module_used_on_same_line | ||||
35 | ); | ||||
36 | |||||
37 | 1 | 1µs | our %EXPORT_TAGS = ( | ||
38 | all => \@EXPORT_OK, | ||||
39 | ); | ||||
40 | |||||
41 | #----------------------------------------------------------------------------- | ||||
42 | |||||
43 | sub is_ppi_expression_or_generic_statement { | ||||
44 | my $element = shift; | ||||
45 | |||||
46 | return if not $element; | ||||
47 | return if not $element->isa('PPI::Statement'); | ||||
48 | return 1 if $element->isa('PPI::Statement::Expression'); | ||||
49 | |||||
50 | my $element_class = blessed($element); | ||||
51 | |||||
52 | return if not $element_class; | ||||
53 | return $element_class eq 'PPI::Statement'; | ||||
54 | } | ||||
55 | |||||
56 | #----------------------------------------------------------------------------- | ||||
57 | |||||
58 | sub is_ppi_generic_statement { | ||||
59 | my $element = shift; | ||||
60 | |||||
61 | my $element_class = blessed($element); | ||||
62 | |||||
63 | return if not $element_class; | ||||
64 | return if not $element->isa('PPI::Statement'); | ||||
65 | |||||
66 | return $element_class eq 'PPI::Statement'; | ||||
67 | } | ||||
68 | |||||
69 | #----------------------------------------------------------------------------- | ||||
70 | |||||
71 | sub is_ppi_statement_subclass { | ||||
72 | my $element = shift; | ||||
73 | |||||
74 | my $element_class = blessed($element); | ||||
75 | |||||
76 | return if not $element_class; | ||||
77 | return if not $element->isa('PPI::Statement'); | ||||
78 | |||||
79 | return $element_class ne 'PPI::Statement'; | ||||
80 | } | ||||
81 | |||||
82 | #----------------------------------------------------------------------------- | ||||
83 | |||||
84 | # Can not use hashify() here because Perl::Critic::Utils already depends on | ||||
85 | # this module. | ||||
86 | 1 | 6µs | 1 | 47µs | Readonly::Hash my %SIMPLE_STATEMENT_CLASS => map { $_ => 1 } qw< # spent 47µs making 1 call to Readonly::Hash |
87 | PPI::Statement | ||||
88 | PPI::Statement::Break | ||||
89 | PPI::Statement::Include | ||||
90 | PPI::Statement::Null | ||||
91 | PPI::Statement::Package | ||||
92 | PPI::Statement::Variable | ||||
93 | >; | ||||
94 | |||||
95 | sub is_ppi_simple_statement { | ||||
96 | my $element = shift or return; | ||||
97 | |||||
98 | my $element_class = blessed( $element ) or return; | ||||
99 | |||||
100 | return $SIMPLE_STATEMENT_CLASS{ $element_class }; | ||||
101 | } | ||||
102 | |||||
103 | #----------------------------------------------------------------------------- | ||||
104 | |||||
105 | sub is_ppi_constant_element { | ||||
106 | my $element = shift or return; | ||||
107 | |||||
108 | blessed( $element ) or return; | ||||
109 | |||||
110 | # TODO implement here documents once PPI::Token::HereDoc grows the | ||||
111 | # necessary PPI::Token::Quote interface. | ||||
112 | return | ||||
113 | $element->isa( 'PPI::Token::Number' ) | ||||
114 | || $element->isa( 'PPI::Token::Quote::Literal' ) | ||||
115 | || $element->isa( 'PPI::Token::Quote::Single' ) | ||||
116 | || $element->isa( 'PPI::Token::QuoteLike::Words' ) | ||||
117 | || ( | ||||
118 | $element->isa( 'PPI::Token::Quote::Double' ) | ||||
119 | || $element->isa( 'PPI::Token::Quote::Interpolate' ) ) | ||||
120 | && $element->string() !~ m< (?: \A | [^\\] ) (?: \\\\)* [\$\@] >smx | ||||
121 | ; | ||||
122 | } | ||||
123 | |||||
124 | #----------------------------------------------------------------------------- | ||||
125 | |||||
126 | sub is_subroutine_declaration { | ||||
127 | my $element = shift; | ||||
128 | |||||
129 | return if not $element; | ||||
130 | |||||
131 | return 1 if $element->isa('PPI::Statement::Sub'); | ||||
132 | |||||
133 | if ( is_ppi_generic_statement($element) ) { | ||||
134 | my $first_element = $element->first_element(); | ||||
135 | |||||
136 | return 1 if | ||||
137 | $first_element | ||||
138 | and $first_element->isa('PPI::Token::Word') | ||||
139 | and $first_element->content() eq 'sub'; | ||||
140 | } | ||||
141 | |||||
142 | return; | ||||
143 | } | ||||
144 | |||||
145 | #----------------------------------------------------------------------------- | ||||
146 | |||||
147 | sub is_in_subroutine { | ||||
148 | my ($element) = @_; | ||||
149 | |||||
150 | return if not $element; | ||||
151 | return 1 if is_subroutine_declaration($element); | ||||
152 | |||||
153 | while ( $element = $element->parent() ) { | ||||
154 | return 1 if is_subroutine_declaration($element); | ||||
155 | } | ||||
156 | |||||
157 | return; | ||||
158 | } | ||||
159 | |||||
160 | #----------------------------------------------------------------------------- | ||||
161 | |||||
162 | sub get_constant_name_element_from_declaring_statement { | ||||
163 | my ($element) = @_; | ||||
164 | |||||
165 | warnings::warnif( | ||||
166 | 'deprecated', | ||||
167 | 'Perl::Critic::Utils::PPI::get_constant_name_element_from_declaring_statement() is deprecated. Use PPIx::Utilities::Statement::get_constant_name_elements_from_declaring_statement() instead.', | ||||
168 | ); | ||||
169 | |||||
170 | return if not $element; | ||||
171 | return if not $element->isa('PPI::Statement'); | ||||
172 | |||||
173 | if ( $element->isa('PPI::Statement::Include') ) { | ||||
174 | my $pragma; | ||||
175 | if ( $pragma = $element->pragma() and $pragma eq 'constant' ) { | ||||
176 | return _constant_name_from_constant_pragma($element); | ||||
177 | } | ||||
178 | } | ||||
179 | elsif ( | ||||
180 | is_ppi_generic_statement($element) | ||||
181 | and $element->schild(0)->content() =~ m< \A Readonly \b >xms | ||||
182 | ) { | ||||
183 | return $element->schild(2); | ||||
184 | } | ||||
185 | |||||
186 | return; | ||||
187 | } | ||||
188 | |||||
189 | sub _constant_name_from_constant_pragma { | ||||
190 | my ($include) = @_; | ||||
191 | |||||
192 | my @arguments = $include->arguments() or return; | ||||
193 | |||||
194 | my $follower = $arguments[0]; | ||||
195 | return if not defined $follower; | ||||
196 | |||||
197 | return $follower; | ||||
198 | } | ||||
199 | |||||
200 | #----------------------------------------------------------------------------- | ||||
201 | |||||
202 | sub get_next_element_in_same_simple_statement { | ||||
203 | my $element = shift or return; | ||||
204 | |||||
205 | while ( $element and ( | ||||
206 | not is_ppi_simple_statement( $element ) | ||||
207 | or $element->parent() | ||||
208 | and $element->parent()->isa( 'PPI::Structure::List' ) ) ) { | ||||
209 | my $next; | ||||
210 | $next = $element->snext_sibling() and return $next; | ||||
211 | $element = $element->parent(); | ||||
212 | } | ||||
213 | return; | ||||
214 | |||||
215 | } | ||||
216 | |||||
217 | #----------------------------------------------------------------------------- | ||||
218 | |||||
219 | sub get_previous_module_used_on_same_line { | ||||
220 | my $element = shift or return; | ||||
221 | |||||
222 | my ( $line ) = @{ $element->location() || []}; | ||||
223 | |||||
224 | while (not is_ppi_simple_statement( $element )) { | ||||
225 | $element = $element->parent() or return; | ||||
226 | } | ||||
227 | |||||
228 | while ( $element = $element->sprevious_sibling() ) { | ||||
229 | ( @{ $element->location() || []} )[0] == $line or return; | ||||
230 | $element->isa( 'PPI::Statement::Include' ) | ||||
231 | and return $element->schild( 1 ); | ||||
232 | } | ||||
233 | |||||
234 | return; | ||||
235 | } | ||||
236 | |||||
237 | #----------------------------------------------------------------------------- | ||||
238 | |||||
239 | 1 | 6µs | 1; | ||
240 | |||||
241 | __END__ |