Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare(3pm)

Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare3pm

Logical not used with compare

Overview

This policy is part of the "Perl::Critic::Pulp" add-on. It picks up some cases of logical not "!" used with a comparison, like

    ! $x =~ /^[123]/  # bad
    ! $x + $y >= $z   # bad

In each case precedence means Perl parses this as "(!$x)", like

    (! $x) =~ /^[123]/
    (! $x) + $y >= $z

rather than a negated comparison. Usually this is a mistake, so this policy is under the "bugs" theme (see "POLICY THEMES" in Perl::Critic).

As a special case, "!" on both sides of "==" or "!=" is allowed, since it's quite a good way to compare booleans.

    !$x == !$y   # ok
    !$x != !$y   # ok

Limitations

User functions called without parentheses are assumed to be usual varargs style. But a prototype may mean that's not the case, letting a bad "!"-with-compare expression to go undetected.

    ! userfunc $x == 123   # indeterminate
    # without prototype would be ok:   ! (userfunc ($x==123))
    # with ($) prototype would be bad: (! userfunc($x)) == 123

Perl builtins with no args, and constant subs created with "use constant" or "sub FOO () {...}" in the file under test are recognised. Hopefully anything else too weird is rare.

    ! time == 1   # bad
    use constant FIVE => 5;
    ! FIVE < 1    # bad
    sub name () { "foo" }
    ! name =~ /bar/    # bad

Home page

http://user42.tuxfamily.org/perl-critic-pulp/index.html

Releases

4 releases since Debian 10 (Buster); the text changed 1 time.

  1. 10
  2. 11–13

From libperl-critic-pulp-perl 99-1 in Debian 13 (Trixie). Parsed by carta from the page's man source.