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

Filename/Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Document.pm
StatementsExecuted 1207006 statements in 1.44s
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11055211515ms515msPerl::Critic::Document::::__ANON__[:531]Perl::Critic::Document::__ANON__[:531]
11055211374ms513msPerl::Critic::Document::::_is_a_version_statementPerl::Critic::Document::_is_a_version_statement
36007525.7ms3.17sPerl::Critic::Document::::findPerl::Critic::Document::find
4608618.40ms11.4msPerl::Critic::Document::::CORE:matchPerl::Critic::Document::CORE:match (opcode)
149226.30ms1.50sPerl::Critic::Document::::highest_explicit_perl_versionPerl::Critic::Document::highest_explicit_perl_version
576435.67ms4.48sPerl::Critic::Document::::AUTOLOADPerl::Critic::Document::AUTOLOAD
144115.08ms15.6msPerl::Critic::Document::::_disable_shebang_fixPerl::Critic::Document::_disable_shebang_fix
144114.93ms16.5sPerl::Critic::Document::::_init_from_external_sourcePerl::Critic::Document::_init_from_external_source
144113.67ms10.4msPerl::Critic::Document::::_determine_is_modulePerl::Critic::Document::_determine_is_module
288222.10ms82.3msPerl::Critic::Document::::find_firstPerl::Critic::Document::find_first
144112.01ms16.5sPerl::Critic::Document::::newPerl::Critic::Document::new
144112.00ms2.35msPerl::Critic::Document::::add_annotationPerl::Critic::Document::add_annotation
144111.82ms1.78sPerl::Critic::Document::::process_annotationsPerl::Critic::Document::process_annotations
144111.41ms1.90msPerl::Critic::Document::::filenamePerl::Critic::Document::filename
144111.15ms1.15msPerl::Critic::Document::::_caching_finderPerl::Critic::Document::_caching_finder
1111.04ms1.88msPerl::Critic::Document::::BEGIN@24Perl::Critic::Document::BEGIN@24
111886µs38.0msPerl::Critic::Document::::BEGIN@28Perl::Critic::Document::BEGIN@28
111706µs1.42msPerl::Critic::Document::::BEGIN@22Perl::Critic::Document::BEGIN@22
14411674µs674µsPerl::Critic::Document::::_init_commonPerl::Critic::Document::_init_common
28821651µs651µsPerl::Critic::Document::::CORE:regcompPerl::Critic::Document::CORE:regcomp (opcode)
14411638µs757µsPerl::Critic::Document::::_is_ppi_docPerl::Critic::Document::_is_ppi_doc
14411372µs372µsPerl::Critic::Document::::CORE:qrPerl::Critic::Document::CORE:qr (opcode)
14411345µs345µsPerl::Critic::Document::::is_modulePerl::Critic::Document::is_module
11147µs52µsPerl::Critic::Document::::BEGIN@12Perl::Critic::Document::BEGIN@12
11126µs26µsPerl::Critic::Document::::BEGIN@10Perl::Critic::Document::BEGIN@10
11118µs48µsPerl::Critic::Document::::BEGIN@16Perl::Critic::Document::BEGIN@16
31112µs12µsPerl::Critic::Document::::line_is_disabled_for_policyPerl::Critic::Document::line_is_disabled_for_policy
11112µs49µsPerl::Critic::Document::::BEGIN@25Perl::Critic::Document::BEGIN@25
11110µs46µsPerl::Critic::Document::::BEGIN@17Perl::Critic::Document::BEGIN@17
1119µs9µsPerl::Critic::Document::::BEGIN@21Perl::Critic::Document::BEGIN@21
1119µs34µsPerl::Critic::Document::::BEGIN@14Perl::Critic::Document::BEGIN@14
1119µs21µsPerl::Critic::Document::::BEGIN@519Perl::Critic::Document::BEGIN@519
1118µs25µsPerl::Critic::Document::::BEGIN@18Perl::Critic::Document::BEGIN@18
1118µs294µsPerl::Critic::Document::::BEGIN@26Perl::Critic::Document::BEGIN@26
3118µs8µsPerl::Critic::Document::::add_suppressed_violationPerl::Critic::Document::add_suppressed_violation
1117µs20µsPerl::Critic::Document::::BEGIN@11Perl::Critic::Document::BEGIN@11
1116µs6µsPerl::Critic::Document::::BEGIN@20Perl::Critic::Document::BEGIN@20
0000s0sPerl::Critic::Document::::__ANON__[:374]Perl::Critic::Document::__ANON__[:374]
0000s0sPerl::Critic::Document::::_find_use_re_modifiers_in_scope_from_elementPerl::Critic::Document::_find_use_re_modifiers_in_scope_from_element
0000s0sPerl::Critic::Document::::_modules_usedPerl::Critic::Document::_modules_used
0000s0sPerl::Critic::Document::::_new_for_parent_documentPerl::Critic::Document::_new_for_parent_document
0000s0sPerl::Critic::Document::::_nodes_by_namespacePerl::Critic::Document::_nodes_by_namespace
0000s0sPerl::Critic::Document::::annotationsPerl::Critic::Document::annotations
0000s0sPerl::Critic::Document::::element_is_in_lexical_scope_after_statement_containingPerl::Critic::Document::element_is_in_lexical_scope_after_statement_containing
0000s0sPerl::Critic::Document::::find_anyPerl::Critic::Document::find_any
0000s0sPerl::Critic::Document::::is_programPerl::Critic::Document::is_program
0000s0sPerl::Critic::Document::::isaPerl::Critic::Document::isa
0000s0sPerl::Critic::Document::::namespacesPerl::Critic::Document::namespaces
0000s0sPerl::Critic::Document::::ppi_documentPerl::Critic::Document::ppi_document
0000s0sPerl::Critic::Document::::ppix_regexp_from_elementPerl::Critic::Document::ppix_regexp_from_element
0000s0sPerl::Critic::Document::::subdocuments_for_namespacePerl::Critic::Document::subdocuments_for_namespace
0000s0sPerl::Critic::Document::::suppressed_violationsPerl::Critic::Document::suppressed_violations
0000s0sPerl::Critic::Document::::uses_modulePerl::Critic::Document::uses_module
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1##############################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6##############################################################################
7
8package Perl::Critic::Document;
9
10244µs126µs
# spent 26µs within Perl::Critic::Document::BEGIN@10 which was called: # once (26µs+0s) by Perl::Critic::BEGIN@26 at line 10
use 5.006001;
# spent 26µs making 1 call to Perl::Critic::Document::BEGIN@10
11230µs232µs
# spent 20µs (7+13) within Perl::Critic::Document::BEGIN@11 which was called: # once (7µs+13µs) by Perl::Critic::BEGIN@26 at line 11
use strict;
# spent 20µs making 1 call to Perl::Critic::Document::BEGIN@11 # spent 13µs making 1 call to strict::import
12224µs258µs
# spent 52µs (47+5) within Perl::Critic::Document::BEGIN@12 which was called: # once (47µs+5µs) by Perl::Critic::BEGIN@26 at line 12
use warnings;
# spent 52µs making 1 call to Perl::Critic::Document::BEGIN@12 # spent 6µs making 1 call to warnings::import
13
14224µs259µs
# spent 34µs (9+25) within Perl::Critic::Document::BEGIN@14 which was called: # once (9µs+25µs) by Perl::Critic::BEGIN@26 at line 14
use Carp qw< confess >;
# spent 34µs making 1 call to Perl::Critic::Document::BEGIN@14 # spent 25µs making 1 call to Exporter::import
15
16229µs257µs
# spent 48µs (18+30) within Perl::Critic::Document::BEGIN@16 which was called: # once (18µs+30µs) by Perl::Critic::BEGIN@26 at line 16
use List::Util qw< reduce >;
# spent 48µs making 1 call to Perl::Critic::Document::BEGIN@16 # spent 9µs making 1 call to List::Util::import
17224µs282µs
# spent 46µs (10+36) within Perl::Critic::Document::BEGIN@17 which was called: # once (10µs+36µs) by Perl::Critic::BEGIN@26 at line 17
use Scalar::Util qw< blessed refaddr weaken >;
# spent 46µs making 1 call to Perl::Critic::Document::BEGIN@17 # spent 36µs making 1 call to Exporter::import
18224µs241µs
# spent 25µs (8+16) within Perl::Critic::Document::BEGIN@18 which was called: # once (8µs+16µs) by Perl::Critic::BEGIN@26 at line 18
use version;
# spent 25µs making 1 call to Perl::Critic::Document::BEGIN@18 # spent 16µs making 1 call to version::import
19
20220µs16µs
# spent 6µs within Perl::Critic::Document::BEGIN@20 which was called: # once (6µs+0s) by Perl::Critic::BEGIN@26 at line 20
use PPI::Document;
# spent 6µs making 1 call to Perl::Critic::Document::BEGIN@20
21224µs19µs
# spent 9µs within Perl::Critic::Document::BEGIN@21 which was called: # once (9µs+0s) by Perl::Critic::BEGIN@26 at line 21
use PPI::Document::File;
# spent 9µs making 1 call to Perl::Critic::Document::BEGIN@21
22295µs21.45ms
# spent 1.42ms (706µs+711µs) within Perl::Critic::Document::BEGIN@22 which was called: # once (706µs+711µs) by Perl::Critic::BEGIN@26 at line 22
use PPIx::Utilities::Node qw< split_ppi_node_by_namespace >;
# spent 1.42ms making 1 call to Perl::Critic::Document::BEGIN@22 # spent 37µs making 1 call to Exporter::import
23
24292µs11.88ms
# spent 1.88ms (1.04+838µs) within Perl::Critic::Document::BEGIN@24 which was called: # once (1.04ms+838µs) by Perl::Critic::BEGIN@26 at line 24
use Perl::Critic::Annotation;
# spent 1.88ms making 1 call to Perl::Critic::Document::BEGIN@24
25227µs286µs
# spent 49µs (12+37) within Perl::Critic::Document::BEGIN@25 which was called: # once (12µs+37µs) by Perl::Critic::BEGIN@26 at line 25
use Perl::Critic::Exception::Parse qw< throw_parse >;
# spent 49µs making 1 call to Perl::Critic::Document::BEGIN@25 # spent 37µs making 1 call to Exporter::import
26226µs2580µs
# spent 294µs (8+286) within Perl::Critic::Document::BEGIN@26 which was called: # once (8µs+286µs) by Perl::Critic::BEGIN@26 at line 26
use Perl::Critic::Utils qw< :booleans :characters shebang_line >;
# spent 294µs making 1 call to Perl::Critic::Document::BEGIN@26 # spent 286µs making 1 call to Exporter::import
27
2831.59ms238.0ms
# spent 38.0ms (886µs+37.1) within Perl::Critic::Document::BEGIN@28 which was called: # once (886µs+37.1ms) by Perl::Critic::BEGIN@26 at line 28
use PPIx::Regexp 0.010 qw< >;
# spent 38.0ms making 1 call to Perl::Critic::Document::BEGIN@28 # spent 12µs making 1 call to UNIVERSAL::VERSION
29
30#-----------------------------------------------------------------------------
31
321600nsour $VERSION = '1.121';
33
34#-----------------------------------------------------------------------------
35
361100nsour $AUTOLOAD;
37
# spent 4.48s (5.67ms+4.48) within Perl::Critic::Document::AUTOLOAD which was called 576 times, avg 7.78ms/call: # 144 times (1.68ms+3.24s) by Perl::Critic::Document::_init_from_external_source at line 123, avg 22.5ms/call # 144 times (1.60ms+1.23s) by Perl::Critic::Statistics::accumulate at line 64 of Perl/Critic/Statistics.pm, avg 8.57ms/call # 144 times (1.41ms+4.73ms) by Perl::Critic::Document::_disable_shebang_fix at line 545, avg 43µs/call # 144 times (974µs+841µs) by Perl::Critic::Utils::shebang_line at line 1154 of Perl/Critic/Utils.pm, avg 13µs/call
sub AUTOLOAD { ## no critic (ProhibitAutoloading,ArgUnpacking)
385764.22ms5762.36ms my ( $function_name ) = $AUTOLOAD =~ m/ ([^:\']+) \z /xms;
# spent 2.36ms making 576 calls to Perl::Critic::Document::CORE:match, avg 4µs/call
39576275µs return if $function_name eq 'DESTROY';
40576224µs my $self = shift;
415763.02ms5764.48s return $self->{_doc}->$function_name(@_);
# spent 3.24s making 144 calls to PPI::Document::index_locations, avg 22.5ms/call # spent 1.23s making 144 calls to PPI::Document::serialize, avg 8.55ms/call # spent 4.12ms making 144 calls to PPI::Node::schild, avg 29µs/call # spent 361µs making 144 calls to PPI::Node::first_element, avg 3µs/call
42}
43
44#-----------------------------------------------------------------------------
45
46
# spent 16.5s (2.01ms+16.5) within Perl::Critic::Document::new which was called 144 times, avg 115ms/call: # 144 times (2.01ms+16.5s) by Perl::Critic::critique at line 104 of Perl/Critic.pm, avg 115ms/call
sub new {
47144224µs my ($class, @args) = @_;
48
49144217µs my $self = bless {}, $class;
50
51144287µs144674µs $self->_init_common();
# spent 674µs making 144 calls to Perl::Critic::Document::_init_common, avg 5µs/call
52144303µs14416.5s $self->_init_from_external_source(@args);
# spent 16.5s making 144 calls to Perl::Critic::Document::_init_from_external_source, avg 115ms/call
53
54144675µs return $self;
55}
56
57#-----------------------------------------------------------------------------
58
59sub _new_for_parent_document {
60 my ($class, $ppi_document, $parent_document) = @_;
61
62 my $self = bless {}, $class;
63
64 $self->_init_common();
65
66 $self->{_doc} = $ppi_document;
67 $self->{_is_module} = $parent_document->is_module();
68
69 return $self;
70}
71
72#-----------------------------------------------------------------------------
73
74
# spent 674µs within Perl::Critic::Document::_init_common which was called 144 times, avg 5µs/call: # 144 times (674µs+0s) by Perl::Critic::Document::new at line 51, avg 5µs/call
sub _init_common {
7514447µs my ($self) = @_;
76
77144184µs $self->{_annotations} = [];
7814497µs $self->{_suppressed_violations} = [];
7914468µs $self->{_disabled_line_map} = {};
80
81144400µs return;
82}
83
84#-----------------------------------------------------------------------------
85
86
# spent 16.5s (4.93ms+16.5) within Perl::Critic::Document::_init_from_external_source which was called 144 times, avg 115ms/call: # 144 times (4.93ms+16.5s) by Perl::Critic::Document::new at line 52, avg 115ms/call
sub _init_from_external_source { ## no critic (Subroutines::RequireArgUnpacking)
8714451µs my $self = shift;
8814433µs my %args;
89
90144113µs if (@_ == 1) {
91 warnings::warnif(
92 'deprecated',
93 'Perl::Critic::Document->new($source) deprecated, use Perl::Critic::Document->new(-source => $source) instead.' ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
94 );
95 %args = ('-source' => shift);
96 } else {
97144228µs %args = @_;
98 }
99
100144107µs my $source_code = $args{'-source'};
101
102 # $source_code can be a file name, or a reference to a
103 # PPI::Document, or a reference to a scalar containing source
104 # code. In the last case, PPI handles the translation for us.
105
106144825µs28813.3s my $ppi_document =
# spent 13.3s making 144 calls to PPI::Document::File::new, avg 92.0ms/call # spent 757µs making 144 calls to Perl::Critic::Document::_is_ppi_doc, avg 5µs/call
107 _is_ppi_doc($source_code)
108 ? $source_code
109 : ref $source_code
110 ? PPI::Document->new($source_code)
111 : PPI::Document::File->new($source_code);
112
113 # Bail on error
11414449µs if (not defined $ppi_document) {
115 my $errstr = PPI::Document::errstr();
116 my $file = ref $source_code ? undef : $source_code;
117 throw_parse
118 message => qq<Can't parse code: $errstr>,
119 file_name => $file;
120 }
121
122144185µs $self->{_doc} = $ppi_document;
123144881µs1443.24s $self->index_locations();
# spent 3.24s making 144 calls to Perl::Critic::Document::AUTOLOAD, avg 22.5ms/call
124144495µs14415.6ms $self->_disable_shebang_fix();
# spent 15.6ms making 144 calls to Perl::Critic::Document::_disable_shebang_fix, avg 109µs/call
125144185µs $self->{_filename_override} = $args{'-filename-override'};
126144847µs28810.8ms $self->{_is_module} = $self->_determine_is_module(\%args);
# spent 10.4ms making 144 calls to Perl::Critic::Document::_determine_is_module, avg 72µs/call # spent 329µs making 144 calls to Readonly::Scalar::FETCH, avg 2µs/call
127
128144627µs return;
129}
130
131#-----------------------------------------------------------------------------
132
133
# spent 757µs (638+119) within Perl::Critic::Document::_is_ppi_doc which was called 144 times, avg 5µs/call: # 144 times (638µs+119µs) by Perl::Critic::Document::_init_from_external_source at line 106, avg 5µs/call
sub _is_ppi_doc {
13414455µs my ($ref) = @_;
135144783µs144119µs return blessed($ref) && $ref->isa('PPI::Document');
# spent 119µs making 144 calls to Scalar::Util::blessed, avg 828ns/call
136}
137
138#-----------------------------------------------------------------------------
139
140sub ppi_document {
141 my ($self) = @_;
142 return $self->{_doc};
143}
144
145#-----------------------------------------------------------------------------
146
147sub isa {
148 my ($self, @args) = @_;
149 return $self->SUPER::isa(@args)
150 || ( (ref $self) && $self->{_doc} && $self->{_doc}->isa(@args) );
151}
152
153#-----------------------------------------------------------------------------
154
155
# spent 3.17s (25.7ms+3.15) within Perl::Critic::Document::find which was called 3600 times, avg 882µs/call: # 2736 times (13.0ms+4.29ms) by Perl::Critic::_critique at line 171 of Perl/Critic.pm, avg 6µs/call # 144 times (6.23ms+1.66s) by Perl::Critic::Annotation::create_annotations at line 34 of Perl/Critic/Annotation.pm, avg 11.6ms/call # 144 times (791µs+1.49s) by Perl::Critic::Document::highest_explicit_perl_version at line 355, avg 10.3ms/call # 144 times (3.48ms+150µs) by Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict::_find_isnt_include_or_package at line 126 of Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm, avg 25µs/call # 144 times (958µs+280µs) by Perl::Critic::Statistics::accumulate at line 60 of Perl/Critic/Statistics.pm, avg 9µs/call # 144 times (616µs+148µs) by Perl::Critic::Statistics::accumulate at line 52 of Perl/Critic/Statistics.pm, avg 5µs/call # 144 times (569µs+90µs) by Perl::Critic::Document::find_first at line 198, avg 5µs/call
sub find {
15636001.66ms my ($self, $wanted, @more_args) = @_;
157
158 # This method can only find elements by their class names. For
159 # other types of searches, delegate to the PPI::Document
160360015.9ms36001.49s if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
# spent 1.49s making 144 calls to PPI::Node::find, avg 10.3ms/call # spent 5.19ms making 3456 calls to Perl::Critic::Document::CORE:match, avg 2µs/call
161 return $self->{_doc}->find($wanted, @more_args);
162 }
163
164 # Build the class cache if it doesn't exist. This happens at most
165 # once per Perl::Critic::Document instance. %elements of will be
166 # populated as a side-effect of calling the $finder_sub coderef
167 # that is produced by the caching_finder() closure.
16834561.33ms if ( !$self->{_elements_of} ) {
169
170144302µs my %cache = ( 'PPI::Document' => [ $self ] );
171
172 # The cache refers to $self, and $self refers to the cache. This
173 # creates a circular reference that leaks memory (i.e. $self is not
174 # destroyed until execution is complete). By weakening the reference,
175 # we allow perl to collect the garbage properly.
176144850µs144422µs weaken( $cache{'PPI::Document'}->[0] );
# spent 422µs making 144 calls to Scalar::Util::weaken, avg 3µs/call
177
178144354µs1441.15ms my $finder_coderef = _caching_finder( \%cache );
# spent 1.15ms making 144 calls to Perl::Critic::Document::_caching_finder, avg 8µs/call
179144334µs1441.66s $self->{_doc}->find( $finder_coderef );
# spent 1.66s making 144 calls to PPI::Node::find, avg 11.5ms/call
1801443.47ms $self->{_elements_of} = \%cache;
181 }
182
183 # find() must return false-but-defined on fail
18434568.64ms return $self->{_elements_of}->{$wanted} || q{};
185}
186
187#-----------------------------------------------------------------------------
188
189
# spent 82.3ms (2.10+80.2) within Perl::Critic::Document::find_first which was called 288 times, avg 286µs/call: # 144 times (942µs+79.3ms) by Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict::violates at line 58 of Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm, avg 558µs/call # 144 times (1.16ms+839µs) by Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage::violates at line 48 of Perl/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm, avg 14µs/call
sub find_first {
190288196µs my ($self, $wanted, @more_args) = @_;
191
192 # This method can only find elements by their class names. For
193 # other types of searches, delegate to the PPI::Document
1942881.39ms28879.5ms if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
# spent 79.3ms making 144 calls to PPI::Node::find_first, avg 551µs/call # spent 180µs making 144 calls to Perl::Critic::Document::CORE:match, avg 1µs/call
195 return $self->{_doc}->find_first($wanted, @more_args);
196 }
197
198144230µs144659µs my $result = $self->find($wanted);
# spent 659µs making 144 calls to Perl::Critic::Document::find, avg 5µs/call
199144497µs return $result ? $result->[0] : $result;
200}
201
202#-----------------------------------------------------------------------------
203
204sub find_any {
205 my ($self, $wanted, @more_args) = @_;
206
207 # This method can only find elements by their class names. For
208 # other types of searches, delegate to the PPI::Document
209 if ( ( ref $wanted ) || !$wanted || $wanted !~ m/ \A PPI:: /xms ) {
210 return $self->{_doc}->find_any($wanted, @more_args);
211 }
212
213 my $result = $self->find($wanted);
214 return $result ? 1 : $result;
215}
216
217#-----------------------------------------------------------------------------
218
219sub namespaces {
220 my ($self) = @_;
221
222 return keys %{ $self->_nodes_by_namespace() };
223}
224
225#-----------------------------------------------------------------------------
226
227sub subdocuments_for_namespace {
228 my ($self, $namespace) = @_;
229
230 my $subdocuments = $self->_nodes_by_namespace()->{$namespace};
231
232 return $subdocuments ? @{$subdocuments} : ();
233}
234
235#-----------------------------------------------------------------------------
236
237sub ppix_regexp_from_element {
238 my ( $self, $element ) = @_;
239
240 if ( blessed( $element ) && $element->isa( 'PPI::Element' ) ) {
241 my $addr = refaddr( $element );
242 return $self->{_ppix_regexp_from_element}{$addr}
243 if exists $self->{_ppix_regexp_from_element}{$addr};
244 return ( $self->{_ppix_regexp_from_element}{$addr} =
245 PPIx::Regexp->new( $element,
246 default_modifiers =>
247 $self->_find_use_re_modifiers_in_scope_from_element(
248 $element ),
249 ) );
250 } else {
251 return PPIx::Regexp->new( $element );
252 }
253}
254
255sub _find_use_re_modifiers_in_scope_from_element {
256 my ( $self, $elem ) = @_;
257 my @found;
258 foreach my $use_re ( @{ $self->find( 'PPI::Statement::Include' ) || [] } )
259 {
260 're' eq $use_re->module()
261 or next;
262 $self->element_is_in_lexical_scope_after_statement_containing(
263 $elem, $use_re )
264 or next;
265 my $prefix = 'no' eq $use_re->type() ? q{-} : $EMPTY;
266 push @found,
267 map { "$prefix$_" }
268 grep { m{ \A / }smx }
269 map {
270 $_->isa( 'PPI::Token::Quote' ) ? $_->string() :
271 $_->isa( 'PPI::Token::QuoteLike::Words' ) ? $_->literal() :
272 $_->content() }
273 $use_re->schildren();
274 }
275 return \@found;
276}
277
278#-----------------------------------------------------------------------------
279
280# This got hung on the Perl::Critic::Document, rather than living in
281# Perl::Critic::Utils::PPI, because of the possibility that caching of scope
282# objects would turn out to be desirable.
283
284sub element_is_in_lexical_scope_after_statement_containing {
285 my ( $self, $inner_elem, $outer_elem ) = @_;
286
287 # If the outer element defines a scope, we're true if and only if
288 # the outer element contains the inner element.
289 $outer_elem->scope()
290 and return $inner_elem->descendant_of( $outer_elem );
291
292 # In the more general case:
293
294 # The last element of the statement containing the outer element
295 # must be before the inner element. If not, we know we're false,
296 # without walking the parse tree.
297
298 my $stmt = $outer_elem->statement()
299 or return;
300 my $last_elem = $stmt->last_element()
301 or return;
302
303 my $stmt_loc = $last_elem->location()
304 or return;
305
306 my $inner_loc = $inner_elem->location()
307 or return;
308
309 $stmt_loc->[0] > $inner_loc->[0]
310 and return;
311 $stmt_loc->[0] == $inner_loc->[0]
312 and $stmt_loc->[1] > $inner_loc->[1]
313 and return;
314
315 # Since we know the inner element is after the outer element, find
316 # the element that defines the scope of the statement that contains
317 # the outer element.
318
319 my $parent = $stmt;
320 while ( ! $parent->scope() ) {
321 $parent = $parent->parent()
322 or return;
323 }
324
325 # We're true if and only if the scope of the outer element contains
326 # the inner element.
327
328 return $inner_elem->descendant_of( $parent );
329
330}
331
332#-----------------------------------------------------------------------------
333
334
# spent 1.90ms (1.41+487µs) within Perl::Critic::Document::filename which was called 144 times, avg 13µs/call: # 144 times (1.41ms+487µs) by Perl::Critic::Document::_determine_is_module at line 566, avg 13µs/call
sub filename {
33514456µs my ($self) = @_;
336
33714495µs if (defined $self->{_filename_override}) {
338 return $self->{_filename_override};
339 }
340 else {
34114454µs my $doc = $self->{_doc};
3421441.23ms288487µs return $doc->can('filename') ? $doc->filename() : undef;
# spent 318µs making 144 calls to PPI::Document::File::filename, avg 2µs/call # spent 169µs making 144 calls to UNIVERSAL::can, avg 1µs/call
343 }
344}
345
346#-----------------------------------------------------------------------------
347
348
# spent 1.50s (6.30ms+1.49) within Perl::Critic::Document::highest_explicit_perl_version which was called 149 times, avg 10.1ms/call: # 144 times (6.18ms+1.42s) by Perl::Critic::Policy::Variables::RequireLexicalLoopIterators::prepare_to_scan_document at line 41 of Perl/Critic/Policy/Variables/RequireLexicalLoopIterators.pm, avg 9.89ms/call # 5 times (121µs+74.3ms) by Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen::violates at line 47 of Perl/Critic/Policy/InputOutput/ProhibitTwoArgOpen.pm, avg 14.9ms/call
sub highest_explicit_perl_version {
34914949µs my ($self) = @_;
350
35114984µs my $highest_explicit_perl_version =
352 $self->{_highest_explicit_perl_version};
353
354149116µs if ( not exists $self->{_highest_explicit_perl_version} ) {
355144286µs1441.49s my $includes = $self->find( \&_is_a_version_statement );
# spent 1.49s making 144 calls to Perl::Critic::Document::find, avg 10.3ms/call
356
357144351µs if ($includes) {
358 # Note: this doesn't use List::Util::max() because that function
359 # doesn't use the overloaded ">=" etc of a version object. The
360 # reduce() style lets version.pm take care of all comparing.
361 #
362 # For reference, max() ends up looking at the string converted to
363 # an NV, or something like that. An underscore like "5.005_04"
364 # provokes a warning and is chopped off at "5.005" thus losing the
365 # minor part from the comparison.
366 #
367 # An underscore "5.005_04" is supposed to mean an alpha release
368 # and shouldn't be used in a perl version. But it's shown in
369 # perlfunc under "use" (as a number separator), and appears in
370 # several modules supplied with perl 5.10.0 (like version.pm
371 # itself!). At any rate if version.pm can understand it then
372 # that's enough for here.
373 $highest_explicit_perl_version =
374 reduce { $a >= $b ? $a : $b }
3751421.32ms2844.83ms map { version->new( $_->version() ) }
# spent 4.21ms making 142 calls to PPI::Statement::Include::version, avg 30µs/call # spent 625µs making 142 calls to version::new, avg 4µs/call
3761423.60ms142480µs @{$includes};
# spent 480µs making 142 calls to List::Util::reduce, avg 3µs/call
377 }
378 else {
3792800ns $highest_explicit_perl_version = undef;
380 }
381
382144407µs $self->{_highest_explicit_perl_version} =
383 $highest_explicit_perl_version;
384 }
385
3861491.44ms147590µs return $highest_explicit_perl_version if $highest_explicit_perl_version;
# spent 590µs making 147 calls to version::(bool, avg 4µs/call
38727µs return;
388}
389
390#-----------------------------------------------------------------------------
391
392sub uses_module {
393 my ($self, $module_name) = @_;
394
395 return exists $self->_modules_used()->{$module_name};
396}
397
398#-----------------------------------------------------------------------------
399
400
# spent 1.78s (1.82ms+1.78) within Perl::Critic::Document::process_annotations which was called 144 times, avg 12.4ms/call: # 144 times (1.82ms+1.78s) by Perl::Critic::_gather_violations at line 128 of Perl/Critic.pm, avg 12.4ms/call
sub process_annotations {
40114454µs my ($self) = @_;
402
403144755µs1441.77s my @annotations = Perl::Critic::Annotation->create_annotations($self);
# spent 1.77s making 144 calls to Perl::Critic::Annotation::create_annotations, avg 12.3ms/call
404144352µs1442.35ms $self->add_annotation(@annotations);
# spent 2.35ms making 144 calls to Perl::Critic::Document::add_annotation, avg 16µs/call
405144330µs return $self;
406}
407
408#-----------------------------------------------------------------------------
409
410
# spent 12µs within Perl::Critic::Document::line_is_disabled_for_policy which was called 3 times, avg 4µs/call: # 3 times (12µs+0s) by Perl::Critic::_critique at line 186 of Perl/Critic.pm, avg 4µs/call
sub line_is_disabled_for_policy {
4113900ns my ($self, $line, $policy) = @_;
41232µs my $policy_name = ref $policy || $policy;
413
414 # HACK: This Policy is special. If it is active, it cannot be
415 # disabled by a "## no critic" annotation. Rather than create a general
416 # hook in Policy.pm for enabling this behavior, we chose to hack
417 # it here, since this isn't the kind of thing that most policies do
418
4193800ns return 0 if $policy_name eq
420 'Perl::Critic::Policy::Miscellanea::ProhibitUnrestrictedNoCritic';
421
422313µs return 1 if $self->{_disabled_line_map}->{$line}->{$policy_name};
423 return 1 if $self->{_disabled_line_map}->{$line}->{ALL};
424 return 0;
425}
426
427#-----------------------------------------------------------------------------
428
429
# spent 2.35ms (2.00+351µs) within Perl::Critic::Document::add_annotation which was called 144 times, avg 16µs/call: # 144 times (2.00ms+351µs) by Perl::Critic::Document::process_annotations at line 404, avg 16µs/call
sub add_annotation {
43014487µs my ($self, @annotations) = @_;
431
432 # Add annotation to our private map for quick lookup
433144133µs for my $annotation (@annotations) {
434
4355191µs51104µs my ($start, $end) = $annotation->effective_range();
# spent 104µs making 51 calls to Perl::Critic::Annotation::effective_range, avg 2µs/call
43651192µs102247µs my @affected_policies = $annotation->disables_all_policies ?
# spent 152µs making 51 calls to Perl::Critic::Annotation::disabled_policies, avg 3µs/call # spent 96µs making 51 calls to Perl::Critic::Annotation::disables_all_policies, avg 2µs/call
437 qw(ALL) : $annotation->disabled_policies();
438
439 # TODO: Find clever way to do this with hash slices
4405184µs for my $line ($start .. $end) {
441485158µs for my $policy (@affected_policies) {
442510516µs $self->{_disabled_line_map}->{$line}->{$policy} = 1;
443 }
444 }
445 }
446
447144120µs push @{ $self->{_annotations} }, @annotations;
448144367µs return $self;
449}
450
451#-----------------------------------------------------------------------------
452
453sub annotations {
454 my ($self) = @_;
455 return @{ $self->{_annotations} };
456}
457
458#-----------------------------------------------------------------------------
459
460
# spent 8µs within Perl::Critic::Document::add_suppressed_violation which was called 3 times, avg 3µs/call: # 3 times (8µs+0s) by Perl::Critic::_critique at line 187 of Perl/Critic.pm, avg 3µs/call
sub add_suppressed_violation {
4613900ns my ($self, $violation) = @_;
46232µs push @{$self->{_suppressed_violations}}, $violation;
46337µs return $self;
464}
465
466#-----------------------------------------------------------------------------
467
468sub suppressed_violations {
469 my ($self) = @_;
470 return @{ $self->{_suppressed_violations} };
471}
472
473#-----------------------------------------------------------------------------
474
475sub is_program {
476 my ($self) = @_;
477
478 return not $self->is_module();
479}
480
481#-----------------------------------------------------------------------------
482
483
# spent 345µs within Perl::Critic::Document::is_module which was called 144 times, avg 2µs/call: # 144 times (345µs+0s) by Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage::prepare_to_scan_document at line 39 of Perl/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm, avg 2µs/call
sub is_module {
48414447µs my ($self) = @_;
485
486144432µs return $self->{_is_module};
487}
488
489#-----------------------------------------------------------------------------
490# PRIVATE functions & methods
491
492
# spent 513ms (374+139) within Perl::Critic::Document::_is_a_version_statement which was called 110552 times, avg 5µs/call: # 110552 times (374ms+139ms) by PPI::Node::find at line 381 of PPI/Node.pm, avg 5µs/call
sub _is_a_version_statement {
49311055225.1ms my (undef, $element) = @_;
494
495110552579ms110552109ms return 0 if not $element->isa('PPI::Statement::Include');
# spent 90.8ms making 107866 calls to UNIVERSAL::isa, avg 842ns/call # spent 18.2ms making 2683 calls to PPI::Structure::List::isa, avg 7µs/call # spent 19µs making 3 calls to PPI::Structure::For::isa, avg 6µs/call
4969821.27ms98229.8ms return 1 if $element->version();
# spent 29.8ms making 982 calls to PPI::Statement::Include::version, avg 30µs/call
4978401.42ms return 0;
498}
499
500#-----------------------------------------------------------------------------
501
502
# spent 1.15ms within Perl::Critic::Document::_caching_finder which was called 144 times, avg 8µs/call: # 144 times (1.15ms+0s) by Perl::Critic::Document::find at line 178, avg 8µs/call
sub _caching_finder {
50314460µs my $cache_ref = shift; # These vars will persist for the life
50414487µs my %isa_cache = (); # of the code ref that this sub returns
505
506
507 # Gather up all the PPI elements and sort by @ISA. Note: if any
508 # instances used multiple inheritance, this implementation would
509 # lead to multiple copies of $element in the $elements_of lists.
510 # However, PPI::* doesn't do multiple inheritance, so we are safe
511
512
# spent 515ms within Perl::Critic::Document::__ANON__[/Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Perl/Critic/Document.pm:531] which was called 110552 times, avg 5µs/call: # 110552 times (515ms+0s) by PPI::Node::find at line 381 of PPI/Node.pm, avg 5µs/call
return sub {
51311055227.9ms my (undef, $element) = @_;
51411055237.2ms my $classes = $isa_cache{ref $element};
51511055210.8ms if ( !$classes ) {
51642302.71ms $classes = [ ref $element ];
517 # Use a C-style loop because we append to the classes array inside
51842309.88ms for ( my $i = 0; $i < @{$classes}; $i++ ) { ## no critic(ProhibitCStyleForLoops)
5192462µs234µs
# spent 21µs (9+13) within Perl::Critic::Document::BEGIN@519 which was called: # once (9µs+13µs) by Perl::Critic::BEGIN@26 at line 519
no strict 'refs'; ## no critic(ProhibitNoStrict)
# spent 21µs making 1 call to Perl::Critic::Document::BEGIN@519 # spent 13µs making 1 call to strict::unimport
5201669521.6ms push @{$classes}, @{"$classes->[$i]::ISA"};
521166957.14ms $cache_ref->{$classes->[$i]} ||= [];
522 }
52342301.83ms $isa_cache{$classes->[0]} = $classes;
524 }
525
52611055247.5ms for my $class ( @{$classes} ) {
527356657160ms push @{$cache_ref->{$class}}, $element;
528 }
529
530110552427ms return 0; # 0 tells find() to keep traversing, but not to store this $element
5311441.06ms };
532}
533
534#-----------------------------------------------------------------------------
535
536
# spent 15.6ms (5.08+10.5) within Perl::Critic::Document::_disable_shebang_fix which was called 144 times, avg 109µs/call: # 144 times (5.08ms+10.5ms) by Perl::Critic::Document::_init_from_external_source at line 124, avg 109µs/call
sub _disable_shebang_fix {
53714464µs my ($self) = @_;
538
539 # When you install a program using ExtUtils::MakeMaker or Module::Build, it
540 # inserts some magical code into the top of the file (just after the
541 # shebang). This code allows people to call your program using a shell,
542 # like `sh my_script`. Unfortunately, this code causes several Policy
543 # violations, so we disable them as if they had "## no critic" annotations.
544
5451441.37ms2886.31ms my $first_stmnt = $self->schild(0) || return;
# spent 6.13ms making 144 calls to Perl::Critic::Document::AUTOLOAD, avg 43µs/call # spent 180µs making 144 calls to PPI::Util::TRUE, avg 1µs/call
546
547 # Different versions of MakeMaker and Build use slightly different shebang
548 # fixing strings. This matches most of the ones I've found in my own Perl
549 # distribution, but it may not be bullet-proof.
550
551144720µs144372µs my $fixin_rx = qr<^eval 'exec .* \$0 \$[{]1[+]"\$@"}'\s*[\r\n]\s*if.+;>ms; ## no critic (ExtendedFormatting)
# spent 372µs making 144 calls to Perl::Critic::Document::CORE:qr, avg 3µs/call
5521443.61ms4326.91ms if ( $first_stmnt =~ $fixin_rx ) {
# spent 3.48ms making 144 calls to Perl::Critic::Document::CORE:match, avg 24µs/call # spent 3.04ms making 144 calls to PPI::Node::content, avg 21µs/call # spent 383µs making 144 calls to Perl::Critic::Document::CORE:regcomp, avg 3µs/call
553 my $line = $first_stmnt->location->[0];
554 $self->{_disabled_line_map}->{$line}->{ALL} = 1;
555 $self->{_disabled_line_map}->{$line + 1}->{ALL} = 1;
556 }
557
558144469µs return $self;
559}
560
561#-----------------------------------------------------------------------------
562
563
# spent 10.4ms (3.67+6.75) within Perl::Critic::Document::_determine_is_module which was called 144 times, avg 72µs/call: # 144 times (3.67ms+6.75ms) by Perl::Critic::Document::_init_from_external_source at line 126, avg 72µs/call
sub _determine_is_module {
56414464µs my ($self, $args) = @_;
565
566144388µs1441.90ms my $file_name = $self->filename();
# spent 1.90ms making 144 calls to Perl::Critic::Document::filename, avg 13µs/call
567144250µs if (
568 defined $file_name
569 and ref $args->{'-program-extensions'} eq 'ARRAY'
570 ) {
571144173µs foreach my $ext ( @{ $args->{'-program-extensions'} } ) {
572 my $regex =
573 ref $ext eq 'Regexp'
574 ? $ext
575144139µs : qr< @{ [ quotemeta $ext ] } \z >xms;
576
577144986µs288375µs return $FALSE if $file_name =~ m/$regex/smx;
# spent 268µs making 144 calls to Perl::Critic::Document::CORE:regcomp, avg 2µs/call # spent 106µs making 144 calls to Perl::Critic::Document::CORE:match, avg 740ns/call
578 }
579 }
580
581144417µs1444.02ms return $FALSE if shebang_line($self);
# spent 4.02ms making 144 calls to Perl::Critic::Utils::shebang_line, avg 28µs/call
582144393µs144123µs return $FALSE if defined $file_name && $file_name =~ m/ [.] PL \z /smx;
# spent 123µs making 144 calls to Perl::Critic::Document::CORE:match, avg 854ns/call
583
584144698µs return $TRUE;
585}
586
587#-----------------------------------------------------------------------------
588
589sub _nodes_by_namespace {
590 my ($self) = @_;
591
592 my $nodes = $self->{_nodes_by_namespace};
593
594 return $nodes if $nodes;
595
596 my $ppi_document = $self->ppi_document();
597 if (not $ppi_document) {
598 return $self->{_nodes_by_namespace} = {};
599 }
600
601 my $raw_nodes_map = split_ppi_node_by_namespace($ppi_document);
602
603 my %wrapped_nodes;
604 while ( my ($namespace, $raw_nodes) = each %{$raw_nodes_map} ) {
605 $wrapped_nodes{$namespace} = [
606 map { __PACKAGE__->_new_for_parent_document($_, $self) }
607 @{$raw_nodes}
608 ];
609 }
610
611 return $self->{_nodes_by_namespace} = \%wrapped_nodes;
612}
613
614#-----------------------------------------------------------------------------
615
616# Note: must use exists on return value to determine membership because all
617# the values are false, unlike the result of hashify().
618sub _modules_used {
619 my ($self) = @_;
620
621 my $mapping = $self->{_modules_used};
622
623 return $mapping if $mapping;
624
625 my $includes = $self->find('PPI::Statement::Include');
626 if (not $includes) {
627 return $self->{_modules_used} = {};
628 }
629
630 my %mapping;
631 for my $module (
632 grep { $_ } map { $_->module() || $_->pragma() } @{$includes}
633 ) {
634 # Significanly ess memory than $h{$k} => 1. Thanks Mr. Lembark.
635 $mapping{$module} = ();
636 }
637
638 return $self->{_modules_used} = \%mapping;
639}
640
641#-----------------------------------------------------------------------------
642
64313µs1;
644
645__END__
 
# spent 11.4ms (8.40+3.04) within Perl::Critic::Document::CORE:match which was called 4608 times, avg 2µs/call: # 3456 times (5.19ms+0s) by Perl::Critic::Document::find at line 160, avg 2µs/call # 576 times (2.36ms+0s) by Perl::Critic::Document::AUTOLOAD at line 38, avg 4µs/call # 144 times (440µs+3.04ms) by Perl::Critic::Document::_disable_shebang_fix at line 552, avg 24µs/call # 144 times (180µs+0s) by Perl::Critic::Document::find_first at line 194, avg 1µs/call # 144 times (123µs+0s) by Perl::Critic::Document::_determine_is_module at line 582, avg 854ns/call # 144 times (106µs+0s) by Perl::Critic::Document::_determine_is_module at line 577, avg 740ns/call
sub Perl::Critic::Document::CORE:match; # opcode
# spent 372µs within Perl::Critic::Document::CORE:qr which was called 144 times, avg 3µs/call: # 144 times (372µs+0s) by Perl::Critic::Document::_disable_shebang_fix at line 551, avg 3µs/call
sub Perl::Critic::Document::CORE:qr; # opcode
# spent 651µs within Perl::Critic::Document::CORE:regcomp which was called 288 times, avg 2µs/call: # 144 times (383µs+0s) by Perl::Critic::Document::_disable_shebang_fix at line 552, avg 3µs/call # 144 times (268µs+0s) by Perl::Critic::Document::_determine_is_module at line 577, avg 2µs/call
sub Perl::Critic::Document::CORE:regcomp; # opcode