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

Perl::Critic::Policy::ValuesAndExpressions::ProhibitBarewordDoubleColon3pm

Don't use foo:: style barewords

Overview

This policy is part of the "Perl::Critic::Pulp" add-on. It asks you not to use the double-colon bareword like

    $class = Foo::Bar::;     # bad

but instead a plain string

    $class = 'Foo::Bar';     # ok

This is intended as a building block for a restricted coding style, or a matter of personal preference if you think the "::" is a bit obscure and that it's clearer to write a string when you mean a string. On that basis the policy is lowest severity and under the "cosmetic" theme (see "POLICY THEMES" in Perl::Critic).

Indirect Object Syntax

By default a double-colon is allowed in the indirect object syntax (see "Indirect Object Syntax" in perlobj).

    my $obj = new Foo::Bar:: $arg1,$arg2;   # ok

This is because "::" there is important to disambiguate a class name "Foo::Bar" from a function "Foo::Bar()", ie. function "Bar()" in package "Foo".

Whether you actually want indirect object syntax is a matter for other policies, like "ProhibitIndirectSyntax". If you don't want the double-colon bareword then change to arrow style "Foo::Bar->new($arg,...)".

Double-Colon Advantages

The "::" bareword is for use on package names, not general bareword quoting. If there's no such package at compile time a warning is given (see "Bareword "%s" refers to nonexistent package" in perldiag)

    my $class = No::Such::Package::;  # Perl warning

This warning can help pick up typos, though it relies on relevant packages being loaded at compile-time (ie. "BEGIN"). If the package is loaded by a "require" at runtime then the warning fires even though the code runs correctly. For reference, a warning isn't given for the indirect object syntax, which rather limits its benefit.

Disabling

If you don't care about this you can always disable "ProhibitBarewordDoubleColon" from your .perlcriticrc in the usual way (see "CONFIGURATION" in Perl::Critic),

    [-ValuesAndExpressions::ProhibitBarewordDoubleColon]

Configuration

"allow_indirect_syntax" (boolean, default true)

If true then allow double-colon in the indirect object syntax as shown above. If false then report double-colons everywhere as violations

    # bad under allow_indirect_syntax=false
    my $obj = new Foo::Bar:: $arg1,$arg2;

This can be controlled from your ~/.perlcriticrc in the usual way. For example

    [ValuesAndExpressions::ProhibitBarewordDoubleColon]
    allow_indirect_syntax=no

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.