Provided by: libperl-critic-community-perl_1.0.2-2_all
NAME
Perl::Critic::Policy::Community::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 foreach my $i (0..$#array) { my $elem = $array[$i]; ... } # ok
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, <http://blogs.perl.org/users/rurban/2014/04/do-not-use-each.html>