← 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:10 2016

Filename/Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/PPI/Token/HereDoc.pm
StatementsExecuted 39 statements in 966µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111115µs138µsPPI::Token::HereDoc::::__TOKENIZER__on_charPPI::Token::HereDoc::__TOKENIZER__on_char
11120µs43µsPPI::Token::HereDoc::::BEGIN@87PPI::Token::HereDoc::BEGIN@87
11115µs15µsPPI::Token::HereDoc::::BEGIN@91PPI::Token::HereDoc::BEGIN@91
11110µs59µsPPI::Token::HereDoc::::BEGIN@90PPI::Token::HereDoc::BEGIN@90
2217µs7µsPPI::Token::HereDoc::::heredocPPI::Token::HereDoc::heredoc
1116µs6µsPPI::Token::HereDoc::::BEGIN@88PPI::Token::HereDoc::BEGIN@88
3215µs5µsPPI::Token::HereDoc::::CORE:matchPPI::Token::HereDoc::CORE:match (opcode)
1111µs1µsPPI::Token::HereDoc::::CORE:substPPI::Token::HereDoc::CORE:subst (opcode)
0000s0sPPI::Token::HereDoc::::terminatorPPI::Token::HereDoc::terminator
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package PPI::Token::HereDoc;
2
3=pod
4
5=head1 NAME
6
7PPI::Token::HereDoc - Token class for the here-doc
8
9=head1 INHERITANCE
10
11 PPI::Token::HereDoc
12 isa PPI::Token
13 isa PPI::Element
14
15=head1 DESCRIPTION
16
17Here-docs are incredibly handy when writing Perl, but incredibly tricky
18when parsing it, primarily because they don't follow the general flow of
19input.
20
21They jump ahead and nab lines directly off the input buffer. Whitespace
22and newlines may not matter in most Perl code, but they matter in here-docs.
23
24They are also tricky to store as an object. They look sort of like an
25operator and a string, but they don't act like it. And they have a second
26section that should be something like a separate token, but isn't because a
27strong can span from above the here-doc content to below it.
28
29So when parsing, this is what we do.
30
31Firstly, the PPI::Token::HereDoc object, does not represent the C<<< << >>>
32operator, or the "END_FLAG", or the content, or even the terminator.
33
34It represents all of them at once.
35
36The token itself has only the declaration part as its "content".
37
38 # This is what the content of a HereDoc token is
39 <<FOO
40
41 # Or this
42 <<"FOO"
43
44 # Or even this
45 << 'FOO'
46
47That is, the "operator", any whitespace separator, and the quoted or bare
48terminator. So when you call the C<content> method on a HereDoc token, you
49get '<< "FOO"'.
50
51As for the content and the terminator, when treated purely in "content" terms
52they do not exist.
53
54The content is made available with the C<heredoc> method, and the name of
55the terminator with the C<terminator> method.
56
57To make things work in the way you expect, PPI has to play some games
58when doing line/column location calculation for tokens, and also during
59the content parsing and generation processes.
60
61Documents cannot simply by recreated by stitching together the token
62contents, and involve a somewhat more expensive procedure, but the extra
63expense should be relatively negligible unless you are doing huge
64quantities of them.
65
66Please note that due to the immature nature of PPI in general, we expect
67C<HereDocs> to be a rich (bad) source of corner-case bugs for quite a while,
68but for the most part they should more or less DWYM.
69
70=head2 Comparison to other string types
71
72Although technically it can be considered a quote, for the time being
73C<HereDocs> are being treated as a completely separate C<Token> subclass,
74and will not be found in a search for L<PPI::Token::Quote> or
75L<PPI::Token::QuoteLike objects>.
76
77This may change in the future, with it most likely to end up under
78QuoteLike.
79
80=head1 METHODS
81
82Although it has the standard set of C<Token> methods, C<HereDoc> objects
83have a relatively large number of unique methods all of their own.
84
85=cut
86
87235µs266µs
# spent 43µs (20+23) within PPI::Token::HereDoc::BEGIN@87 which was called: # once (20µs+23µs) by PPI::Token::BEGIN@70 at line 87
use strict;
# spent 43µs making 1 call to PPI::Token::HereDoc::BEGIN@87 # spent 23µs making 1 call to strict::import
88232µs16µs
# spent 6µs within PPI::Token::HereDoc::BEGIN@88 which was called: # once (6µs+0s) by PPI::Token::BEGIN@70 at line 88
use PPI::Token ();
# spent 6µs making 1 call to PPI::Token::HereDoc::BEGIN@88
89
90250µs2108µs
# spent 59µs (10+49) within PPI::Token::HereDoc::BEGIN@90 which was called: # once (10µs+49µs) by PPI::Token::BEGIN@70 at line 90
use vars qw{$VERSION @ISA};
# spent 59µs making 1 call to PPI::Token::HereDoc::BEGIN@90 # spent 49µs making 1 call to vars::import
91
# spent 15µs within PPI::Token::HereDoc::BEGIN@91 which was called: # once (15µs+0s) by PPI::Token::BEGIN@70 at line 94
BEGIN {
921600ns $VERSION = '1.215';
93122µs @ISA = 'PPI::Token';
941706µs115µs}
# spent 15µs making 1 call to PPI::Token::HereDoc::BEGIN@91
95
- -
100#####################################################################
101# PPI::Token::HereDoc Methods
102
103=pod
104
105=head2 heredoc
106
107The C<heredoc> method is the authoritative method for accessing the contents
108of the C<HereDoc> object.
109
110It returns the contents of the here-doc as a list of newline-terminated
111strings. If called in scalar context, it returns the number of lines in
112the here-doc, B<excluding> the terminator line.
113
114=cut
115
116
# spent 7µs within PPI::Token::HereDoc::heredoc which was called 2 times, avg 3µs/call: # once (4µs+0s) by PPI::Document::serialize at line 464 of PPI/Document.pm # once (3µs+0s) by PPI::Document::index_locations at line 627 of PPI/Document.pm
sub heredoc {
117 wantarray
118 ? @{shift->{_heredoc}}
119212µs : scalar @{shift->{_heredoc}};
120}
121
122=pod
123
124=head2 terminator
125
126The C<terminator> method returns the name of the terminating string for the
127here-doc.
128
129Returns the terminating string as an unescaped string (in the rare case
130the terminator has an escaped quote in it).
131
132=cut
133
134sub terminator {
135 shift->{_terminator};
136}
137
- -
142#####################################################################
143# Tokenizer Methods
144
145# Parse in the entire here-doc in one call
146
# spent 138µs (115+23) within PPI::Token::HereDoc::__TOKENIZER__on_char which was called: # once (115µs+23µs) by PPI::Token::Operator::__TOKENIZER__on_char at line 102 of PPI/Token/Operator.pm
sub __TOKENIZER__on_char {
1471600ns my $t = $_[1];
148
149 # We are currently located on the first char after the <<
150
151 # Handle the most common form first for simplicity and speed reasons
152 ### FIXME - This regex, and this method in general, do not yet allow
153 ### for the null here-doc, which terminates at the first
154 ### empty line.
15511µs my $rest_of_line = substr( $t->{line}, $t->{line_cursor} );
15619µs12µs unless ( $rest_of_line =~ /^( \s* (?: "[^"]*" | '[^']*' | `[^`]*` | \\?\w+ ) )/x ) {
# spent 2µs making 1 call to PPI::Token::HereDoc::CORE:match
157 # Degenerate to a left-shift operation
158 $t->{token}->set_class('Operator');
159 return $t->_finalize_token->__TOKENIZER__on_char( $t );
160 }
161
162 # Add the rest of the token, work out what type it is,
163 # and suck in the content until the end.
1641500ns my $token = $t->{token};
165154µs $token->{content} .= $1;
16611µs $t->{line_cursor} += length $1;
167
168 # Find the terminator, clean it up and determine
169 # the type of here-doc we are dealing with.
1701900ns my $content = $token->{content};
17118µs23µs if ( $content =~ /^\<\<(\w+)$/ ) {
# spent 3µs making 2 calls to PPI::Token::HereDoc::CORE:match, avg 1µs/call
172 # Bareword
173 $token->{_mode} = 'interpolate';
174 $token->{_terminator} = $1;
175
176 } elsif ( $content =~ /^\<\<\s*\'(.*)\'$/ ) {
177 # ''-quoted literal
17811µs $token->{_mode} = 'literal';
17911µs $token->{_terminator} = $1;
18017µs11µs $token->{_terminator} =~ s/\\'/'/g;
# spent 1µs making 1 call to PPI::Token::HereDoc::CORE:subst
181
182 } elsif ( $content =~ /^\<\<\s*\"(.*)\"$/ ) {
183 # ""-quoted literal
184 $token->{_mode} = 'interpolate';
185 $token->{_terminator} = $1;
186 $token->{_terminator} =~ s/\\"/"/g;
187
188 } elsif ( $content =~ /^\<\<\s*\`(.*)\`$/ ) {
189 # ``-quoted command
190 $token->{_mode} = 'command';
191 $token->{_terminator} = $1;
192 $token->{_terminator} =~ s/\\`/`/g;
193
194 } elsif ( $content =~ /^\<\<\\(\w+)$/ ) {
195 # Legacy forward-slashed bareword
196 $token->{_mode} = 'literal';
197 $token->{_terminator} = $1;
198
199 } else {
200 # WTF?
201 return undef;
202 }
203
204 # Define $line outside of the loop, so that if we encounter the
205 # end of the file, we have access to the last line still.
2061500ns my $line;
207
208 # Suck in the HEREDOC
20911µs $token->{_heredoc} = [];
21011µs my $terminator = $token->{_terminator} . "\n";
21112µs13µs while ( defined($line = $t->_get_line) ) {
# spent 3µs making 1 call to PPI::Tokenizer::_get_line
2126900ns if ( $line eq $terminator ) {
213 # Keep the actual termination line for consistency
214 # when we are re-assembling the file
2151600ns $token->{_terminator_line} = $line;
216
217 # The HereDoc is now fully parsed
21816µs25µs return $t->_finalize_token->__TOKENIZER__on_char( $t );
# spent 2µs making 1 call to PPI::Token::Whitespace::__TOKENIZER__on_char # spent 2µs making 1 call to PPI::Tokenizer::_finalize_token
219 }
220
221 # Add the line
22259µs510µs push @{$token->{_heredoc}}, $line;
# spent 10µs making 5 calls to PPI::Tokenizer::_get_line, avg 2µs/call
223 }
224
225 # End of file.
226 # Error: Didn't reach end of here-doc before end of file.
227 # $line might be undef if we get NO lines.
228 if ( defined $line and $line eq $token->{_terminator} ) {
229 # If the last line matches the terminator
230 # but is missing the newline, we want to allow
231 # it anyway (like perl itself does). In this case
232 # perl would normally throw a warning, but we will
233 # also ignore that as well.
234 pop @{$token->{_heredoc}};
235 $token->{_terminator_line} = $line;
236 } else {
237 # The HereDoc was not properly terminated.
238 $token->{_terminator_line} = undef;
239
240 # Trim off the trailing whitespace
241 if ( defined $token->{_heredoc}->[-1] and $t->{source_eof_chop} ) {
242 chop $token->{_heredoc}->[-1];
243 $t->{source_eof_chop} = '';
244 }
245 }
246
247 # Set a hint for PPI::Document->serialize so it can
248 # inexpensively repair it if needed when writing back out.
249 $token->{_damaged} = 1;
250
251 # The HereDoc is not fully parsed
252 $t->_finalize_token->__TOKENIZER__on_char( $t );
253}
254
25513µs1;
256
257=pod
258
259=head1 TO DO
260
261- Implement PPI::Token::Quote interface compatibility
262
263- Check CPAN for any use of the null here-doc or here-doc-in-s///e
264
265- Add support for the null here-doc
266
267- Add support for here-doc in s///e
268
269=head1 SUPPORT
270
271See the L<support section|PPI/SUPPORT> in the main module.
272
273=head1 AUTHOR
274
275Adam Kennedy E<lt>adamk@cpan.orgE<gt>
276
277=head1 COPYRIGHT
278
279Copyright 2001 - 2011 Adam Kennedy.
280
281This program is free software; you can redistribute
282it and/or modify it under the same terms as Perl itself.
283
284The full text of the license can be found in the
285LICENSE file included with this module.
286
287=cut
 
# spent 5µs within PPI::Token::HereDoc::CORE:match which was called 3 times, avg 2µs/call: # 2 times (3µs+0s) by PPI::Token::HereDoc::__TOKENIZER__on_char at line 171, avg 1µs/call # once (2µs+0s) by PPI::Token::HereDoc::__TOKENIZER__on_char at line 156
sub PPI::Token::HereDoc::CORE:match; # opcode
# spent 1µs within PPI::Token::HereDoc::CORE:subst which was called: # once (1µs+0s) by PPI::Token::HereDoc::__TOKENIZER__on_char at line 180
sub PPI::Token::HereDoc::CORE:subst; # opcode