---
name: "Perl::Critic::Policy::Community::LoopOnHash"
section: "3pm"
platform: "linux"
package: "libperl-critic-community-perl"
version: "1.0.4-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/Perl::Critic::Policy::Community::LoopOnHash"
---

# Perl::Critic::Policy::Community::LoopOnHash(3pm)

Don't loop over hashes

## NAME

Perl::Critic::Policy::Community::LoopOnHash - Don't loop over hashes

## DESCRIPTION

It's possible to loop over a hash as if it was a list, which results in alternating between the keys and values of the hash. Often, the intent was instead to loop over either the keys or the values of the hash.

```
 foreach my $foo (%hash) { ... }      # not ok
 action() for %hash;                  # not ok
 foreach my $foo (keys %hash) { ... } # ok
 action() for values %hash;           # ok
```

If you intended to loop over alternating keys and values, you can make this intent clear by first copying them to an array:

```
 foreach my $key_or_value (@{[%hash]}) { ... }
 foreach my $key_or_value (my @dummy = %hash) { ... }
```

This policy is a subclass of the policy Perl::Critic::Policy::Variables::ProhibitLoopOnHash, and performs the same function but in the `"community"` theme.

## AFFILIATION

This policy is part of Perl::Critic::Community.

## CONFIGURATION

This policy is not configurable except for the standard options.

## AUTHOR

Dan Book, `"dbook@cpan.org"`

## COPYRIGHT AND LICENSE

Copyright 2015, Dan Book.

This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.

## SEE ALSO

Perl::Critic
