Perl::...pOnHash(3pm)

πŸ“¦
version 1.0.4 Β· 4 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’… 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 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