Perl::...pOnHash(3pm)
π NAME π
Perl::Critic::Policy::Community::LoopOnHash β bestie, pls stop looping over hashes like they're lists π
This page lives in the User Contributed Perl Documentation section of the manual, and it's about a Perl::Critic policy, so get your linting goggles on π₯½β¨
πΈ DESCRIPTION πΈ
okay so here's the tea π΅ β you can loop over a hash as if it were a list, and Perl will happily let you do it, no cap. But what you actually get is the keys and the values alternating, over and over. Key, value, key, value. Like a little conga line nobody asked for π
And usually? That was not the vibe you were going for. Most of the time you meant to loop over just the keys, or just the values. The way that people write %hash in a foreach when they meant keys %hash... it's giving chaos π
So here's what's not ok:
foreach my $foo (%hash) { ... } # not ok
action() for %hash; # not ok
foreach my $foo (keys %hash) { ... } # ok
action() for values %hash; # ok
See how the top two are lowkey sus and the bottom two ate? π
Now β if you genuinely did mean to loop over alternating keys and values (main character behavior, honestly), that's fine, just make your intent loud and clear by first copying them into an array. Then everyone reading your code knows it was on purpose, not an accident π
foreach my $key_or_value (@{[%hash]}) { ... }
foreach my $key_or_value (my @dummy = %hash) { ... }
This policy is a subclass of the policy Perl::Critic::Policy::Variables::ProhibitLoopOnHash, and it does the exact same thing β same function, no extra features β except it lives in the "community" theme. Same energy, different aesthetic β¨
π¦ AFFILIATION π¦
This policy is part of Perl::Critic::Community. It's in the group chat π«Ά
π CONFIGURATION π
Not configurable, bestie. This policy is not configurable except for the standard options. That's it, that's the whole section π
β AUTHOR β
Dan Book, dbook@cpan.org β thank you for your service, legend π
π 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β the mothership, go read it, iykyk π
and that's a wrap, bestie β¨ this page is from perl v5.40.1, dated 2025-03-22, and it's the Perl::...pOnHash(3pm) manual page β go forth and lint responsibly, ily, 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