Perl::Critic::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration(3pm)

Perl::Critic::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration3pm

Do not write " my $foo .= 'bar'; ".

Affiliation

This Policy is part of the core Perl::Critic distribution.

Overview

Variable declarations that also do initialization with '=' are common. Perl also allows you to use operators like '.=', '+=', etc., but it it is more clear to not do so.

    my $foo .= 'bar';              # same as my $foo = 'bar';
    our $foo *= 2;                 # same as our $foo = 0;
    my ( $foo, $bar ) += ( 1, 2 ); # same as my ( $foo, $bar ) = ( undef, 2 );
    local $Carp::CarpLevel += 1;   # same as local $Carp::CarpLevel = 1;
    state $foo += 2;               # adds 2 every time it's encountered

Such constructs are usually the result of botched cut-and-paste, and often are bugs. Some produce warnings.

Configuration

There is an "allow_our" boolean option for this Policy. If set, augmented assignments are allowed when declaring "our" variables. Since "our" variables are globally accessible, some modules will want to allow users to initialize the variable prior to the module using the variable. Modules may also wish to use the same our variable in different scopes without declaring it at the outer scope.

With this option set, the following are flagged as indicated:

    our $DEBUG //= 1;           # ok

This can be enabled in your .perlcriticrc:

    [Perl::Critic::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration]
    allow_our = 1

Releases

8 releases since Debian 7 (Wheezy); the text changed 7 times.

  1. 7
  2. 8
  3. 9
  4. 10
  5. 11
  6. 12
  7. 13
  8. Arch

Author

Mike O'Regan

From perl-perl-critic 1.156-3 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.