line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Community::Each; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
479
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Perl::Critic::Utils qw(:severities :classification :ppi); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
81
|
|
7
|
1
|
|
|
1
|
|
403
|
use parent 'Perl::Critic::Policy'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = 'v1.0.1'; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
73
|
use constant DESC => 'each() called'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
89
|
|
12
|
1
|
|
|
1
|
|
8
|
use constant EXPL => 'The each function may cause undefined behavior when operating on the hash while iterating. Use a foreach loop over the hash\'s keys or values instead.'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
192
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
0
|
8550
|
sub supported_parameters { () } |
15
|
2
|
|
|
2
|
1
|
30
|
sub default_severity { $SEVERITY_LOW } |
16
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { 'community' } |
17
|
2
|
|
|
2
|
1
|
27977
|
sub applies_to { 'PPI::Token::Word' } |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub violates { |
20
|
9
|
|
|
9
|
1
|
553
|
my ($self, $elem) = @_; |
21
|
9
|
100
|
66
|
|
|
28
|
return () unless $elem eq 'each' and is_function_call $elem; |
22
|
2
|
|
|
|
|
788
|
return $self->violation(DESC, EXPL, $elem); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
1; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 NAME |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Perl::Critic::Policy::Community::Each - Don't use each to iterate through a |
30
|
|
|
|
|
|
|
hash |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 DESCRIPTION |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
The C<each()> function relies on an iterator internal to a hash (or array), |
35
|
|
|
|
|
|
|
which is the same iterator used by C<keys()> and C<values()>. So deleting or |
36
|
|
|
|
|
|
|
adding hash elements during iteration, or just calling C<keys()> or C<values()> |
37
|
|
|
|
|
|
|
on the hash, will cause undefined behavior and the code will likely break. This |
38
|
|
|
|
|
|
|
could occur even by passing the hash to other functions which operate on the |
39
|
|
|
|
|
|
|
hash. Instead, use a C<foreach> loop iterating through the keys or values of |
40
|
|
|
|
|
|
|
the hash. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
while (my ($key, $value) = each %hash) { ... } # not ok |
43
|
|
|
|
|
|
|
foreach my $key (keys %hash) { my $value = $hash{$key}; ... } # ok |
44
|
|
|
|
|
|
|
foreach my $i (0..$#array) { my $elem = $array[$i]; ... } # ok |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 AFFILIATION |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
This policy is part of L<Perl::Critic::Community>. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 CONFIGURATION |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
This policy is not configurable except for the standard options. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 AUTHOR |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Dan Book, C<dbook@cpan.org> |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Copyright 2015, Dan Book. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
This library is free software; you may redistribute it and/or modify it under |
63
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 SEE ALSO |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
L<Perl::Critic>, L<http://blogs.perl.org/users/rurban/2014/04/do-not-use-each.html> |