File Coverage

blib/lib/Perl/Critic/Policy/Community/Each.pm
Criterion Covered Total %
statement 24 25 96.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 4 5 80.0
total 42 46 91.3


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