table of contents
Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt(3) | User Contributed Perl Documentation | Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt(3) |
NAME¶
Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt - disallow bareword before <
DESCRIPTION¶
This policy is part of the "Perl::Critic::Pulp" add-on. It prohibits a bareword before a "<" to keep you out of trouble with autoloaded or unprototyped constant subs since a "<" in that case is interpreted as the start of a "<..>" glob or readline instead of a less-than. This policy is under the "bugs" theme (see "POLICY THEMES" in Perl::Critic).
use POSIX; DBL_MANT_DIG < 32 # bad, perl 5.8 thinks <> func <*.c> # ok, actual glob time < 2e9 # ok, builtins parse ok use constant FOO => 16; FOO < 32 # ok, your own const sub BAR () { 64 } BAR < 32 # ok, your own prototyped sub
The fix for something like "DBL_MANT_DIG < 10" is parens either around or after, like
(DBL_MANT_DIG) < 10 # ok DBL_MANT_DIG() < 10 # ok
whichever you think is less worse. The latter emphasises it's really a sub.
The key is whether the constant sub in question is defined and has a prototype at the time the code is compiled. ConstantBeforeLt makes the pessimistic assumption that anything except "use constant" and prototyped subs in your own file shouldn't be relied on.
In practice the most likely problems are with the "POSIX" module constants of Perl 5.8.x and earlier, since they were unprototyped. The default code generated by "h2xs" (as of Perl 5.10.0) is similar autoloaded unprototyped constants so modules using the bare output of that suffer too.
If you're confident the modules you use don't play tricks with their constants (including only using POSIX on Perl 5.10.0 or higher) then you might find ConstantBeforeLt too pessimistic. It normally triggers rather rarely anyway, but you can always disable it altogether in your .perlcriticrc file (see "CONFIGURATION" in Perl::Critic),
[-ValuesAndExpressions::ConstantBeforeLt]
OTHER NOTES¶
Bareword file handles might be misinterpreted by this policy as constants, but in practice "<" doesn't get used with anything taking a bare filehandle.
A constant used before it's defined, like
if (FOO < 123) { ... } # bad ... use constant FOO => 456;
is reported by ConstantBeforeLt since it might be an imported constant sub, even if it's much more likely to be a simple mis-ordering, which "use strict" picks up anyway when it runs.
SEE ALSO¶
Perl::Critic::Pulp, Perl::Critic
HOME PAGE¶
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 |