line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop; |
2
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
26714
|
use 5.010001; |
|
40
|
|
|
|
|
172
|
|
4
|
40
|
|
|
40
|
|
277
|
use strict; |
|
40
|
|
|
|
|
113
|
|
|
40
|
|
|
|
|
851
|
|
5
|
40
|
|
|
40
|
|
209
|
use warnings; |
|
40
|
|
|
|
|
109
|
|
|
40
|
|
|
|
|
1193
|
|
6
|
40
|
|
|
40
|
|
256
|
use Readonly; |
|
40
|
|
|
|
|
115
|
|
|
40
|
|
|
|
|
2218
|
|
7
|
|
|
|
|
|
|
|
8
|
40
|
|
|
40
|
|
311
|
use List::Util qw< first >; |
|
40
|
|
|
|
|
123
|
|
|
40
|
|
|
|
|
2915
|
|
9
|
|
|
|
|
|
|
|
10
|
40
|
|
|
40
|
|
357
|
use Perl::Critic::Utils qw{ :severities }; |
|
40
|
|
|
|
|
103
|
|
|
40
|
|
|
|
|
2210
|
|
11
|
40
|
|
|
40
|
|
5491
|
use parent 'Perl::Critic::Policy'; |
|
40
|
|
|
|
|
141
|
|
|
40
|
|
|
|
|
300
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.146'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Readonly::Scalar my $DESC => q{Readline inside "for" loop}; |
18
|
|
|
|
|
|
|
Readonly::Scalar my $EXPL => [ 211 ]; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
21
|
|
|
|
|
|
|
|
22
|
91
|
|
|
91
|
0
|
1620
|
sub supported_parameters { return () } |
23
|
80
|
|
|
80
|
1
|
379
|
sub default_severity { return $SEVERITY_HIGH } |
24
|
92
|
|
|
92
|
1
|
417
|
sub default_themes { return qw< core bugs pbp > } |
25
|
34
|
|
|
34
|
1
|
115
|
sub applies_to { return qw< PPI::Statement::Compound > } |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub violates { |
30
|
21
|
|
|
21
|
1
|
64
|
my ( $self, $elem, undef ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
21
|
100
|
|
|
|
72
|
return if $elem->type() ne 'foreach'; |
33
|
|
|
|
|
|
|
|
34
|
13
|
50
|
|
46
|
|
851
|
my $list = first { $_->isa('PPI::Structure::List') } $elem->schildren() |
|
46
|
|
|
|
|
364
|
|
35
|
|
|
|
|
|
|
or return; |
36
|
|
|
|
|
|
|
|
37
|
13
|
100
|
|
|
|
180
|
if ( |
38
|
|
|
|
|
|
|
my $readline = $list->find_first('PPI::Token::QuoteLike::Readline') |
39
|
|
|
|
|
|
|
) { |
40
|
6
|
|
|
|
|
1381
|
return $self->violation( $DESC, $EXPL, $readline ); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
7
|
|
|
|
|
2281
|
return; #ok! |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
__END__ |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=pod |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 NAME |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Perl::Critic::Policy::InputOutput::ProhibitReadlineInForLoop - Write C<< while( $line = <> ){...} >> instead of C<< for(<>){...} >>. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 AFFILIATION |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
This Policy is part of the core L<Perl::Critic|Perl::Critic> |
61
|
|
|
|
|
|
|
distribution. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 DESCRIPTION |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Using the readline operator in a C<for> or C<foreach> loop is very |
67
|
|
|
|
|
|
|
slow. The iteration list of the loop creates a list context, which |
68
|
|
|
|
|
|
|
causes the readline operator to read the entire input stream before |
69
|
|
|
|
|
|
|
iteration even starts. Instead, just use a C<while> loop, which only |
70
|
|
|
|
|
|
|
reads one line at a time. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
for my $line ( <$file_handle> ){ do_something($line) } #not ok |
73
|
|
|
|
|
|
|
while ( my $line = <$file_handle> ){ do_something($line) } #ok |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 CONFIGURATION |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This Policy is not configurable except for the standard options. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 AUTHOR |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com> |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 COPYRIGHT |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
90
|
|
|
|
|
|
|
it under the same terms as Perl itself. The full text of this license |
91
|
|
|
|
|
|
|
can be found in the LICENSE file included with this module. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Local Variables: |
96
|
|
|
|
|
|
|
# mode: cperl |
97
|
|
|
|
|
|
|
# cperl-indent-level: 4 |
98
|
|
|
|
|
|
|
# fill-column: 78 |
99
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
100
|
|
|
|
|
|
|
# c-indentation-style: bsd |
101
|
|
|
|
|
|
|
# End: |
102
|
|
|
|
|
|
|
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |