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             package Perl::Critic::Policy::Community::Each;
2              
3 1     1   466 use strict;
  1         3  
  1         29  
4 1     1   6 use warnings;
  1         1  
  1         27  
5              
6 1     1   5 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         3  
  1         49  
7 1     1   352 use parent 'Perl::Critic::Policy';
  1         2  
  1         6  
8              
9             our $VERSION = 'v1.0.2';
10              
11 1     1   85 use constant DESC => 'each() called';
  1         2  
  1         66  
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 8577 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 27701 sub applies_to { 'PPI::Token::Word' }
18              
19             sub violates {
20 9     9 1 470 my ($self, $elem) = @_;
21 9 100 66     24 return () unless $elem eq 'each' and is_function_call $elem;
22 2         787 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>