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