Perl::Critic::Policy::Variables::ProhibitLoopOnHash(3pm)

Perl::Critic::Policy::Variables::ProhibitLoopOnHash3pm

Don't write loops on hashes, only on keys and values of hashes

Version

version 0.008

Overview

When "looping over hashes," we mean looping over hash keys or hash values. If you forgot to call "keys" or "values" you will accidentally loop over both.

    foreach my $foo (%hash) {...}        # not ok
    action() for %hash;                  # not ok
    foreach my $foo ( keys %hash ) {...} # ok
    action() for values %hash;           # ok

An effort is made to detect expressions:

    action() for %hash ? keys %hash : ();                             # ok
    action() for %{ $hash{'stuff'} } ? keys %{ $hash{'stuff'} } : (); # ok

(Granted, the second example there doesn't make much sense, but I have found a variation of it in real code.)

Configuration

This policy is not configurable except for the standard options.

Thanks

Thank you to Ruud H.G. Van Tol.

Releases

4 releases since Debian 10 (Buster); the text changed 2 times.

  1. 10
  2. 11
  3. 12–13

Author

Sawyer X, "[email protected]"

Author

Sawyer X

From libperl-critic-policy-variables-prohibitlooponhash-perl 0.008-2 in Debian 13 (Trixie). Parsed by carta from the page's man source.