Perl::Critic::Policy::Freenode::Each - Don't use each to iterate through a hash
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.
each()
keys()
values()
foreach
while (my ($key, $value) = each %hash) { ... } # not ok foreach my $key (keys %hash) { my $value = $hash{$key}; ... } # ok
This policy is part of Perl::Critic::Freenode.
This policy is not configurable except for the standard options.
Dan Book, dbook@cpan.org
dbook@cpan.org
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.
Perl::Critic, http://blogs.perl.org/users/rurban/2014/04/do-not-use-each.html
To install Perl::Critic::Freenode, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Critic::Freenode
CPAN shell
perl -MCPAN -e shell install Perl::Critic::Freenode
For more information on module installation, please visit the detailed CPAN module installation guide.