NAME

Perl::Critic::Policy::Freenode::Each - Don't use each to iterate through a hash

DESCRIPTION

The each() function relies on an iterator internal to a hash (or array), which is the same iterator used by keys() and values(). So deleting or adding hash elements during iteration, or just calling keys() or values() on the hash, will cause undefined behavior and the code will likely break. This could occur even by passing the hash to other functions which operate on the hash. Instead, use a foreach loop iterating through the keys or values of the hash.

while (my ($key, $value) = each %hash) { ... }                # not ok
foreach my $key (keys %hash) { my $value = $hash{$key}; ... } # ok

AFFILIATION

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

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, http://blogs.perl.org/users/rurban/2014/04/do-not-use-each.html