| 1 | package Perl::Critic::Policy::Community::LoopOnHash; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy::Variables::ProhibitLoopOnHash'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |
| 10 | |||
| 11 | 4 | ▶ | sub default_severity { $SEVERITY_HIGH } |
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||
| 12 | 0 | ▶ | sub default_themes { 'community' } |
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 13 | |||
| 14 | 1 | 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> | ||