File Coverage

blib/lib/Perl/Critic/Policy/Community/LoopOnHash.pm
Criterion Covered Total %
statement 13 14 92.8
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 20 22 90.9


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   365 use warnings;
  1         2  
  1         22  
4 1     1   3  
  1         5  
  1         23  
5             use Perl::Critic::Utils qw(:severities :classification :ppi);
6 1     1   6 use parent 'Perl::Critic::Policy::Variables::ProhibitLoopOnHash';
  1         3  
  1         52  
7 1     1   297  
  1         2  
  1         4  
8             our $VERSION = 'v1.0.3';
9              
10              
11 4     4 1 42296 1;
12 0     0 1    
13             =head1 NAME
14              
15             Perl::Critic::Policy::Community::LoopOnHash - Don't loop over hashes
16              
17             =head1 DESCRIPTION
18              
19             It's possible to loop over a hash as if it was a list, which results in
20             alternating between the keys and values of the hash. Often, the intent was
21             instead to loop over either the keys or the values of the hash.
22              
23             foreach my $foo (%hash) { ... } # not ok
24             action() for %hash; # not ok
25             foreach my $foo (keys %hash) { ... } # ok
26             action() for values %hash; # ok
27              
28             If you intended to loop over alternating keys and values, you can make this
29             intent clear by first copying them to an array:
30              
31             foreach my $key_or_value (@{[%hash]}) { ... }
32             foreach my $key_or_value (my @dummy = %hash) { ... }
33              
34             This policy is a subclass of the policy
35             L<Perl::Critic::Policy::Variables::ProhibitLoopOnHash>, and performs the same
36             function but in the C<community> theme.
37              
38             =head1 AFFILIATION
39              
40             This policy is part of L<Perl::Critic::Community>.
41              
42             =head1 CONFIGURATION
43              
44             This policy is not configurable except for the standard options.
45              
46             =head1 AUTHOR
47              
48             Dan Book, C<dbook@cpan.org>
49              
50             =head1 COPYRIGHT AND LICENSE
51              
52             Copyright 2015, Dan Book.
53              
54             This library is free software; you may redistribute it and/or modify it under
55             the terms of the Artistic License version 2.0.
56              
57             =head1 SEE ALSO
58              
59             L<Perl::Critic>