-
-
23 Dec 2021 21:09:21 UTC
- Distribution: Perl-Critic-Community
- Module version: v1.0.2
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues
- Testers
- Kwalitee
Bus factor: 1- 89.37% Coverage
- License: artistic_2
- Perl: v5.10.1
- Activity
24 month- Tools
- Download (46.08KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
++ed by:2 non-PAUSE usersand 6 contributors-
Amory Meltzer
-
David Cantrell
-
Graham Knop
-
Ryan Voots
-
Tomasz Konojacki
-
William Taylor
- Dependencies
- Carp
- Exporter
- List::Util
- PPI
- Path::Tiny
- Perl::Critic
- Perl::Critic::Policy::Objects::ProhibitIndirectSyntax
- Perl::Critic::Policy::Subroutines::ProhibitAmpersandSigils
- Perl::Critic::Policy::ValuesAndExpressions::ProhibitArrayAssignAref
- Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations
- Perl::Critic::Policy::Variables::ProhibitLoopOnHash
- Perl::Critic::Policy::Variables::RequireLexicalLoopIterators
- Scalar::Util
- parent
- version
- Reverse dependencies
- CPAN Testers List
- Dependency graph
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 bykeys()
andvalues()
. So deleting or adding hash elements during iteration, or just callingkeys()
orvalues()
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 aforeach
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
Module Install Instructions
To install Perl::Critic::Community, copy and paste the appropriate command in to your terminal.
cpanm Perl::Critic::Community
perl -MCPAN -e shell install Perl::Critic::Community
For more information on module installation, please visit the detailed CPAN module installation guide.