← Index
NYTProf Performance Profile   « line view »
For /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/bin/perlcritic
  Run on Sat Mar 19 22:12:22 2016
Reported on Sat Mar 19 22:14:12 2016

Filename/Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/PPI/Structure.pm
StatementsExecuted 198598 statements in 567ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1157811144ms735msPPI::Structure::::tokensPPI::Structure::tokens (recurses: max depth 6, inclusive time 664ms)
57891171.9ms139msPPI::Structure::::newPPI::Structure::new
623233271.5ms71.5msPPI::Structure::::startPPI::Structure::start
565342166.5ms66.5msPPI::Structure::::finishPPI::Structure::finish
22444440.6ms116msPPI::Structure::::contentPPI::Structure::content (recurses: max depth 5, inclusive time 46.0ms)
284112.27ms2.43msPPI::Structure::::bracesPPI::Structure::braces
111247µs321µsPPI::Structure::::BEGIN@109PPI::Structure::BEGIN@109
2322218µs484µsPPI::Structure::::locationPPI::Structure::location
111195µs272µsPPI::Structure::::BEGIN@107PPI::Structure::BEGIN@107
111166µs239µsPPI::Structure::::BEGIN@104PPI::Structure::BEGIN@104
111163µs237µsPPI::Structure::::BEGIN@105PPI::Structure::BEGIN@105
111156µs242µsPPI::Structure::::BEGIN@108PPI::Structure::BEGIN@108
111150µs220µsPPI::Structure::::BEGIN@110PPI::Structure::BEGIN@110
111145µs213µsPPI::Structure::::BEGIN@106PPI::Structure::BEGIN@106
111144µs217µsPPI::Structure::::BEGIN@111PPI::Structure::BEGIN@111
111143µs214µsPPI::Structure::::BEGIN@112PPI::Structure::BEGIN@112
231196µs108µsPPI::Structure::::first_elementPPI::Structure::first_element
11113µs25µsPPI::Structure::::BEGIN@91PPI::Structure::BEGIN@91
1119µs9µsPPI::Structure::::BEGIN@98PPI::Structure::BEGIN@98
1119µs48µsPPI::Structure::::BEGIN@97PPI::Structure::BEGIN@97
1117µs29µsPPI::Structure::::BEGIN@93PPI::Structure::BEGIN@93
1113µs3µsPPI::Structure::::BEGIN@92PPI::Structure::BEGIN@92
1113µs3µsPPI::Structure::::BEGIN@95PPI::Structure::BEGIN@95
1113µs3µsPPI::Structure::::BEGIN@94PPI::Structure::BEGIN@94
0000s0sPPI::Structure::::_completePPI::Structure::_complete
0000s0sPPI::Structure::::completePPI::Structure::complete
0000s0sPPI::Structure::::elementsPPI::Structure::elements
0000s0sPPI::Structure::::insert_afterPPI::Structure::insert_after
0000s0sPPI::Structure::::insert_beforePPI::Structure::insert_before
0000s0sPPI::Structure::::last_elementPPI::Structure::last_element
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package PPI::Structure;
2
3=pod
4
5=head1 NAME
6
7PPI::Structure - The base class for Perl braced structures
8
9=head1 INHERITANCE
10
11 PPI::Structure
12 isa PPI::Node
13 isa PPI::Element
14
15=head1 DESCRIPTION
16
17PPI::Structure is the root class for all Perl bracing structures. This
18covers all forms of C< [ ... ] >, C< { ... } >, and C< ( ... ) > brace
19types, and includes cases where only one half of the pair exist.
20
21The class PPI::Structure itself is full abstract and no objects of that
22type should actually exist in the tree.
23
24=head2 Elements vs Children
25
26A B<PPI::Structure> has an unusual existance. Unlike a L<PPI::Document>
27or L<PPI::Statement>, which both simply contain other elements, a
28structure B<both> contains and consists of content.
29
30That is, the brace tokens are B<not> considered to be "children" of the
31structure, but are part of it.
32
33In practice, this will mean that while the -E<gt>elements and -E<gt>tokens
34methods (and related) B<will> return a list with the brace tokens at either
35end, the -E<gt>children method explicitly will B<not> return the brace.
36
37=head1 STRUCTURE CLASSES
38
39Excluding the transient L<PPI::Structure::Unknown> that exists briefly
40inside the parser, there are eight types of structure.
41
42=head2 L<PPI::Structure::List>
43
44This covers all round braces used for function arguments, in C<foreach>
45loops, literal lists, and braces used for precedence-ordering purposes.
46
47=head2 L<PPI::Structure::For>
48
49Although B<not> used for the C<foreach> loop list, this B<is> used for
50the special case of the round-brace three-part semicolon-seperated C<for>
51loop expression (the traditional C style for loop).
52
53=head2 L<PPI::Structure::Given>
54
55This is for the expression being matched in switch statements.
56
57=head2 L<PPI::Structure::When>
58
59This is for the matching expression in "when" statements.
60
61=head2 L<PPI::Structure::Condition>
62
63This round-brace structure covers boolean conditional braces, such as
64for C<if> and C<while> blocks.
65
66=head2 L<PPI::Structure::Block>
67
68This curly-brace and common structure is used for all form of code
69blocks. This includes those for C<if>, C<do> and similar, as well
70as C<grep>, C<map>, C<sort>, C<sub> and (labelled or anonymous)
71scoping blocks.
72
73=head2 L<PPI::Structure::Constructor>
74
75This class covers brace structures used for the construction of
76anonymous C<ARRAY> and C<HASH> references.
77
78=head2 L<PPI::Structure::Subscript>
79
80This class covers square-braces and curly-braces used after a
81-E<gt> pointer to access the subscript of an C<ARRAY> or C<HASH>.
82
83=head1 METHODS
84
85C<PPI::Structure> itself has very few methods. Most of the time, you will be
86working with the more generic L<PPI::Element> or L<PPI::Node> methods, or one
87of the methods that are subclass-specific.
88
89=cut
90
91219µs238µs
# spent 25µs (13+12) within PPI::Structure::BEGIN@91 which was called: # once (13µs+12µs) by PPI::BEGIN@22 at line 91
use strict;
# spent 25µs making 1 call to PPI::Structure::BEGIN@91 # spent 12µs making 1 call to strict::import
92218µs13µs
# spent 3µs within PPI::Structure::BEGIN@92 which was called: # once (3µs+0s) by PPI::BEGIN@22 at line 92
use Scalar::Util ();
# spent 3µs making 1 call to PPI::Structure::BEGIN@92
93218µs252µs
# spent 29µs (7+23) within PPI::Structure::BEGIN@93 which was called: # once (7µs+23µs) by PPI::BEGIN@22 at line 93
use Params::Util qw{_INSTANCE};
# spent 29µs making 1 call to PPI::Structure::BEGIN@93 # spent 23µs making 1 call to Exporter::import
94215µs13µs
# spent 3µs within PPI::Structure::BEGIN@94 which was called: # once (3µs+0s) by PPI::BEGIN@22 at line 94
use PPI::Node ();
# spent 3µs making 1 call to PPI::Structure::BEGIN@94
95224µs13µs
# spent 3µs within PPI::Structure::BEGIN@95 which was called: # once (3µs+0s) by PPI::BEGIN@22 at line 95
use PPI::Exception ();
# spent 3µs making 1 call to PPI::Structure::BEGIN@95
96
97233µs286µs
# spent 48µs (9+39) within PPI::Structure::BEGIN@97 which was called: # once (9µs+39µs) by PPI::BEGIN@22 at line 97
use vars qw{$VERSION @ISA *_PARENT};
# spent 48µs making 1 call to PPI::Structure::BEGIN@97 # spent 39µs making 1 call to vars::import
98
# spent 9µs within PPI::Structure::BEGIN@98 which was called: # once (9µs+0s) by PPI::BEGIN@22 at line 102
BEGIN {
991300ns $VERSION = '1.215';
10015µs @ISA = 'PPI::Node';
10114µs *_PARENT = *PPI::Element::_PARENT;
102114µs19µs}
# spent 9µs making 1 call to PPI::Structure::BEGIN@98
103
1042106µs1239µs
# spent 239µs (166+73) within PPI::Structure::BEGIN@104 which was called: # once (166µs+73µs) by PPI::BEGIN@22 at line 104
use PPI::Structure::Block ();
# spent 239µs making 1 call to PPI::Structure::BEGIN@104
105293µs1237µs
# spent 237µs (163+74) within PPI::Structure::BEGIN@105 which was called: # once (163µs+74µs) by PPI::BEGIN@22 at line 105
use PPI::Structure::Condition ();
# spent 237µs making 1 call to PPI::Structure::BEGIN@105
106288µs1213µs
# spent 213µs (145+68) within PPI::Structure::BEGIN@106 which was called: # once (145µs+68µs) by PPI::BEGIN@22 at line 106
use PPI::Structure::Constructor ();
# spent 213µs making 1 call to PPI::Structure::BEGIN@106
107286µs1272µs
# spent 272µs (195+77) within PPI::Structure::BEGIN@107 which was called: # once (195µs+77µs) by PPI::BEGIN@22 at line 107
use PPI::Structure::For ();
# spent 272µs making 1 call to PPI::Structure::BEGIN@107
108293µs1242µs
# spent 242µs (156+86) within PPI::Structure::BEGIN@108 which was called: # once (156µs+86µs) by PPI::BEGIN@22 at line 108
use PPI::Structure::Given ();
# spent 242µs making 1 call to PPI::Structure::BEGIN@108
109289µs1321µs
# spent 321µs (247+74) within PPI::Structure::BEGIN@109 which was called: # once (247µs+74µs) by PPI::BEGIN@22 at line 109
use PPI::Structure::List ();
# spent 321µs making 1 call to PPI::Structure::BEGIN@109
110292µs1220µs
# spent 220µs (150+71) within PPI::Structure::BEGIN@110 which was called: # once (150µs+71µs) by PPI::BEGIN@22 at line 110
use PPI::Structure::Subscript ();
# spent 220µs making 1 call to PPI::Structure::BEGIN@110
111291µs1217µs
# spent 217µs (144+73) within PPI::Structure::BEGIN@111 which was called: # once (144µs+73µs) by PPI::BEGIN@22 at line 111
use PPI::Structure::Unknown ();
# spent 217µs making 1 call to PPI::Structure::BEGIN@111
1122613µs1214µs
# spent 214µs (143+71) within PPI::Structure::BEGIN@112 which was called: # once (143µs+71µs) by PPI::BEGIN@22 at line 112
use PPI::Structure::When ();
# spent 214µs making 1 call to PPI::Structure::BEGIN@112
113
- -
118#####################################################################
119# Constructor
120
121
# spent 139ms (71.9+67.6) within PPI::Structure::new which was called 5789 times, avg 24µs/call: # 5789 times (71.9ms+67.6ms) by PPI::Lexer::_lex_statement at line 650 of PPI/Lexer.pm, avg 24µs/call
sub new {
12257891.82ms my $class = shift;
123578912.8ms578955.8ms my $Token = PPI::Token::__LEXER__opens($_[0]) ? shift : return undef;
# spent 55.8ms making 5789 calls to PPI::Token::__LEXER__opens, avg 10µs/call
124
125 # Create the object
126578912.3ms my $self = bless {
127 children => [],
128 start => $Token,
129 }, $class;
130
131 # Set the start braces parent link
132578937.9ms1157811.8ms Scalar::Util::weaken(
# spent 6.88ms making 5789 calls to Scalar::Util::weaken, avg 1µs/call # spent 4.95ms making 5789 calls to Scalar::Util::refaddr, avg 854ns/call
133 $_PARENT{Scalar::Util::refaddr $Token} = $self
134 );
135
136578911.6ms $self;
137}
138
- -
143#####################################################################
144# PPI::Structure API methods
145
146=pod
147
148=head2 start
149
150For lack of better terminology (like "open" and "close") that has not
151already in use for some other more important purpose, the two individual
152braces for the structure are known within PPI as the "start" and "finish"
153braces (at least for method purposes).
154
155The C<start> method returns the start brace for the structure (i.e. the
156opening brace).
157
158Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
159structure does not have a starting brace.
160
161Under normal parsing circumstances this should never occur, but may happen
162due to manipulation of the PDOM tree.
163
164=cut
165
16662323168ms
# spent 71.5ms within PPI::Structure::start which was called 62323 times, avg 1µs/call: # 45436 times (50.8ms+0s) by PPI::Node::find at line 395 of PPI/Node.pm, avg 1µs/call # 11098 times (12.6ms+0s) by PPI::Node::find_first at line 452 of PPI/Node.pm, avg 1µs/call # 5789 times (8.13ms+0s) by PPI::Lexer::_lex_structure at line 1356 of PPI/Lexer.pm, avg 1µs/call
sub start { $_[0]->{start} }
167
168=pod
169
170=head2 finish
171
172The C<finish> method returns the finish brace for the structure (i.e. the
173closing brace).
174
175Returns the brace as a L<PPI::Token::Structure> or C<undef> if the
176structure does not have a finishing brace. This can be quite common if
177the document is not complete (for example, from an editor where the user
178may be halfway through typeing a subroutine).
179
180=cut
181
18256534134ms
# spent 66.5ms within PPI::Structure::finish which was called 56534 times, avg 1µs/call: # 45436 times (53.3ms+0s) by PPI::Node::find at line 393 of PPI/Node.pm, avg 1µs/call # 11098 times (13.2ms+0s) by PPI::Node::find_first at line 450 of PPI/Node.pm, avg 1µs/call
sub finish { $_[0]->{finish} }
183
184=pod
185
186=head2 braces
187
188The C<braces> method is a utility method which returns the brace type,
189regardless of whether has both braces defined, or just the starting
190brace, or just the ending brace.
191
192Returns on of the three strings C<'[]'>, C<'{}'>, or C<'()'>, or C<undef>
193on error (primarily not having a start brace, as mentioned above).
194
195=cut
196
197
# spent 2.43ms (2.27+162µs) within PPI::Structure::braces which was called 284 times, avg 9µs/call: # 284 times (2.27ms+162µs) by PPI::Lexer::_continues at line 805 of PPI/Lexer.pm, avg 9µs/call
sub braces {
1982841.04ms284162µs my $self = $_[0]->{start} ? shift : return undef;
# spent 162µs making 284 calls to PPI::Util::TRUE, avg 571ns/call
199 return {
2002841.58ms '[' => '[]',
201 '(' => '()',
202 '{' => '{}',
203 }->{ $self->{start}->{content} };
204}
205
206=pod
207
208=head1 complete
209
210The C<complete> method is a convenience method that returns true if
211the both braces are defined for the structure, or false if only one
212brace is defined.
213
214Unlike the top level C<complete> method which checks for completeness
215in depth, the structure complete method ONLY confirms completeness
216for the braces, and does not recurse downwards.
217
218=cut
219
220sub complete {
221 !! ($_[0]->{start} and $_[0]->{finish});
222}
223
- -
228#####################################################################
229# PPI::Node overloaded methods
230
231# For us, the "elements" concept includes the brace tokens
232sub elements {
233 my $self = shift;
234
235 if ( wantarray ) {
236 # Return a list in array context
237 return ( $self->{start} || (), @{$self->{children}}, $self->{finish} || () );
238 } else {
239 # Return the number of elements in scalar context.
240 # This is memory-cheaper than creating another big array
241 return scalar(@{$self->{children}})
242 + ($self->{start} ? 1 : 0)
243 + ($self->{finish} ? 1 : 0);
244 }
245}
246
247# For us, the first element is probably the opening brace
248
# spent 108µs (96+12) within PPI::Structure::first_element which was called 23 times, avg 5µs/call: # 23 times (96µs+12µs) by PPI::Structure::location at line 264, avg 5µs/call
sub first_element {
249 # Technically, if we have no children and no opening brace,
250 # then the first element is the closing brace.
25123126µs2312µs $_[0]->{start} or $_[0]->{children}->[0] or $_[0]->{finish};
# spent 12µs making 23 calls to PPI::Util::TRUE, avg 513ns/call
252}
253
254# For us, the last element is probably the closing brace
255sub last_element {
256 # Technically, if we have no children and no closing brace,
257 # then the last element is the opening brace
258 $_[0]->{finish} or $_[0]->{children}->[-1] or $_[0]->{start};
259}
260
261# Location is same as the start token, if any
262
# spent 484µs (218+266) within PPI::Structure::location which was called 23 times, avg 21µs/call: # 15 times (140µs+164µs) by PPI::Element::logical_line_number at line 932 of PPI/Element.pm, avg 20µs/call # 8 times (78µs+102µs) by PPI::Node::location at line 685 of PPI/Node.pm, avg 23µs/call
sub location {
263239µs my $self = shift;
26423109µs46121µs my $first = $self->first_element or return undef;
# spent 108µs making 23 calls to PPI::Structure::first_element, avg 5µs/call # spent 13µs making 23 calls to PPI::Util::TRUE, avg 548ns/call
2652385µs23145µs $first->location;
# spent 145µs making 23 calls to PPI::Element::location, avg 6µs/call
266}
267
- -
272#####################################################################
273# PPI::Element overloaded methods
274
275# Get the full set of tokens, including start and finish
276
# spent 735ms (144+591) within PPI::Structure::tokens which was called 11578 times, avg 64µs/call: # 11578 times (144ms+591ms) by PPI::Node::tokens at line 666 of PPI/Node.pm, avg 64µs/call
sub tokens {
277115782.52ms my $self = shift;
27811578109ms3473413.2ms my @tokens = (
# spent 13.2ms making 23156 calls to PPI::Util::TRUE, avg 569ns/call # spent 1.24s making 11578 calls to PPI::Node::tokens, avg 107µs/call, recursion: max depth 14, sum of overlapping time 1.24s
279 $self->{start} || (),
280 $self->SUPER::tokens(@_),
281 $self->{finish} || (),
282 );
2831157837.2ms @tokens;
284}
285
286# Like the token method ->content, get our merged contents.
287# This will recurse downwards through everything
288### Reimplement this using List::Utils stuff
289
# spent 116ms (40.6+75.0) within PPI::Structure::content which was called 2244 times, avg 51µs/call: # 945 times (16.3ms+22.8ms) by Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations::_is_conditional at line 50 of Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm, avg 41µs/call # 724 times (11.8ms+-11.7ms) by PPI::Node::content at line 671 of PPI/Node.pm, avg 110ns/call # 568 times (12.0ms+58.2ms) by PPI::Lexer::_curly at line 1190 of PPI/Lexer.pm, avg 124µs/call # 7 times (435µs+5.75ms) by PPI::Statement::Compound::type at line 202 of PPI/Statement/Compound.pm, avg 883µs/call
sub content {
2902244501µs my $self = shift;
29122449.99ms44884.24ms my $content = $self->{start} ? $self->{start}->content : '';
# spent 2.97ms making 2244 calls to PPI::Token::content, avg 1µs/call # spent 1.27ms making 2244 calls to PPI::Util::TRUE, avg 564ns/call
29222441.90ms foreach my $child ( @{$self->{children}} ) {
29341488.75ms414881.8ms $content .= $child->content;
# spent 110ms making 1929 calls to PPI::Node::content, avg 57µs/call, recursion: max depth 5, sum of overlapping time 30.8ms # spent 3.02ms making 2219 calls to PPI::Token::content, avg 1µs/call
294 }
29522449.33ms44884.18ms $content .= $self->{finish}->content if $self->{finish};
# spent 2.87ms making 2244 calls to PPI::Token::content, avg 1µs/call # spent 1.31ms making 2244 calls to PPI::Util::TRUE, avg 586ns/call
29622444.83ms $content;
297}
298
299# Is the structure completed
300sub _complete {
301 !! ( defined $_[0]->{finish} );
302}
303
304# You can insert either another structure, or a token
305sub insert_before {
306 my $self = shift;
307 my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
308 if ( $Element->isa('PPI::Structure') ) {
309 return $self->__insert_before($Element);
310 } elsif ( $Element->isa('PPI::Token') ) {
311 return $self->__insert_before($Element);
312 }
313 '';
314}
315
316# As above, you can insert either another structure, or a token
317sub insert_after {
318 my $self = shift;
319 my $Element = _INSTANCE(shift, 'PPI::Element') or return undef;
320 if ( $Element->isa('PPI::Structure') ) {
321 return $self->__insert_after($Element);
322 } elsif ( $Element->isa('PPI::Token') ) {
323 return $self->__insert_after($Element);
324 }
325 '';
326}
327
32812µs1;
329
330=pod
331
332=head1 SUPPORT
333
334See the L<support section|PPI/SUPPORT> in the main module.
335
336=head1 AUTHOR
337
338Adam Kennedy E<lt>adamk@cpan.orgE<gt>
339
340=head1 COPYRIGHT
341
342Copyright 2001 - 2011 Adam Kennedy.
343
344This program is free software; you can redistribute
345it and/or modify it under the same terms as Perl itself.
346
347The full text of the license can be found in the
348LICENSE file included with this module.
349
350=cut