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

Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt3pm

Disallow bareword before <

Overview

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.

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.