logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Perl::Critic::Policy::Plicease::ProhibitArrayAssignAref - Don't assign an anonymous arrayref to an array

Author

       Original author: Kevin Ryde

       Current maintainer: Graham Ollis <plicease@cpan.org>

Description

       This policy is a fork of Perl::Critic::Policy::ValuesAndExpressions::ProhibitArrayAssignAref.  It differs
       from the original by not having a dependency on List::MoreUtils.  It is unfortunately still licensed as
       GPL3.

       It asks you not to assign an anonymous arrayref to an array

           @array = [ 1, 2, 3 ];       # bad

       The idea is that it's rather unclear whether an arrayref is intended, or might have meant to be a list
       like

           @array = ( 1, 2, 3 );

       This policy is under the "bugs" theme (see "POLICY THEMES" in Perl::Critic) for the chance "[]" is a
       mistake, and since even if it's correct it will likely make anyone reading it wonder.

       A single arrayref can still be assigned to an array, but with parens to make it clear,

           @array = ( [1,2,3] );       # ok

       Dereferences or array and hash slices (see "Slices" in perldata) are recognised as an array target and
       treated similarly,

           @$ref = [1,2,3];            # bad assign to deref
           @{$ref} = [1,2,3];          # bad assign to deref
           @x[1,2,3] = ['a','b','c'];  # bad assign to array slice
           @x{'a','b'} = [1,2];        # bad assign to hash slice

   ListAssignmentParens
       This policy is not a blanket requirement for "()" parens on array assignments.  It's normal and
       unambiguous to have a function call or "grep" etc without parens.

           @array = foo();                    # ok
           @array = grep {/\.txt$/} @array;   # ok

       The only likely problem from lack of parens in such cases is that the "," comma operator has lower
       precedence than "=" (see perlop), so something like

           @array = 1,2,3;   # oops, not a list

       means

           @array = (1);
           2;
           3;

       Normally the remaining literals in void context provoke a warning from Perl itself.

       An intentional single element assignment is quite common as a statement, for instance

           @ISA = 'My::Parent::Class';   # ok

       And for reference the range operator precedence is high enough,

           @array = 1..10;               # ok

       But of course parens are needed if concatenating some disjoint ranges with the comma operator,

           @array = (1..5, 10..15);      # parens needed

       The "qw" form gives a list too

           @array = qw(a b c);           # ok

Home Page

       <https://github.com/uperl/Perl-Critic-Policy-Plicease-ProhibitArrayAssignAref>

Name

       Perl::Critic::Policy::Plicease::ProhibitArrayAssignAref - Don't assign an anonymous arrayref to an array

See Also

       Perl::Critic
       Perl::Critic::Policy::ValuesAndExpressions::ProhibitArrayAssignAref

Version

       version 100.00

See Also