Scroll to navigation

Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare(3) User Contributed Perl Documentation Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare(3)

NAME

Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare - logical not used with compare

DESCRIPTION

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

SEE ALSO

Perl::Critic::Pulp, Perl::Critic

HOME PAGE

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

COPYRIGHT

Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde

Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Perl-Critic-Pulp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.

2021-03-01 perl v5.40.0