Perl::...y::Each(3pm)
π Perl::...y::Each(3pm) π
okay so bestie, welcome to a page from the User Contributed Perl Documentation manual β yes that's the manual name, we're in the community-contributed corner of the perl docs and it's giving β¨ public service announcement β¨. This one's short, this one's spicy, and this one will save your hash from doing a full-on meltdown. π«Ά
πΈ NAME πΈ
Perl::Critic::Policy::Community::Each β the policy that says: do not use each to iterate through a hash. that's it. that's the whole vibe check. π
β¨ DESCRIPTION β¨
okay so here's the tea, and it's lowkey important so pay attention. π
The each() function relies on an iterator that lives inside the hash (or array) itself. and here's the plot twist: it's the same iterator that keys() and values() use. same one. shared. one iterator, three functions, zero chill. π
so what happens if you mess with it mid-iteration?
- deleting hash elements during iteration? π
- adding hash elements during iteration? ππ
- or just calling
keys()orvalues()on that hash while you're in the middle of it? πππ
all of those cause undefined behavior, and your code will likely break. no cap. and it gets worse β this can happen even if you just pass the hash to some other function that happens to operate on the hash. sneaky! π
so what's the move instead? use a "foreach" loop iterating through the keys or the values of the hash. that's the main character energy we're looking for. π
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
see how the bottom two are just... calm? peaceful? unbothered? that's the goal. π§
π AFFILIATION π
This policy is part of Perl::Critic::Community. it runs with its little community friends. found family fr fr. π
π CONFIGURATION π
This policy is not configurable except for the standard options. that's right, no knobs, no toggles, no secret settings menu. it just is what it is and honestly? iconic. β¨
π AUTHOR π
Dan Book, "dbook@cpan.org" β thank you for your service, we are obsessed. π«Ά
π 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>β literally the receipts, go read it. π₯
and that's the whole page bestie!! π this came from perl v5.40.1, dated 2025-03-22, under the Perl::...y::Each(3pm) title (which, yes, is the short form of Perl::Critic::Policy::Community::Each β the manpage renderer had to truncate it, we don't make the rules π). stay safe out there, and please, for the love of everything, stop each-ing your hashes. mwah π
ππΈ
π¦ where this page comes from
- debian trixie: libperl-critic-community-perl 1.0.4-1 Β· Artistic-2.0
- ubuntu resolute: libperl-critic-community-perl 1.0.4-1 Β· Artistic-2.0
- ubuntu stonking: libperl-critic-community-perl 1.0.4-1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms