The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline - comma at end of list at newline

DESCRIPTION

This policy is part of the Perl::Critic::Pulp addon. It asks you to put a comma at the end of a list etc ending with a newline,

    @array = ($one,
              $two     # bad
             );

    @array = ($one,
              $two,    # ok
             );

This makes no difference to how the code runs, so this policy is under the "cosmetic" theme (see "POLICY THEMES" in Perl::Critic).

The idea is to make it easier when editing the code -- you don't have to remember a new comma when adding an item or cutting and pasting lines to re-arrange.

If the closing bracket is on the same line as the last element then no comma is required. A comma can be used if desired, but it's not required.

    $hashref = { abc => 123,
                 def => 456 };   # ok

Parens around an expression are not a list, so

    $foo = (1
            + 2
            + 3        # ok
           );

A single element paren expression is only considered a list when it's an array assignment or a function or method call.

Disabling

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

    [-CodeLayout::RequireTrailingCommaAtNewline]

Other Ways to Do It

This policy is a variation of CodeLayout::RequireTrailingCommas. That policy doesn't apply to function calls or hashref constructors, and you may find its requirement for a trailing comma in even one-line lists like @x=(1,2,) too much.

CONFIGURATION

except_function_calls (boolean, default false)

If true then function calls and method calls are not checked, allowing for instance

    foo (
      1,
      2     # ok under except_function_calls
    );

The idea is that if foo() takes only two arguments then you don't want to write a trailing comma as it might suggest something more could be added.

Whether you write calls spread out this way is a matter of personal preference. If you do then enable except_function_calls with the following in your .perlcriticrc file,

    [CodeLayout::RequireTrailingCommaAtNewline]
    except_function_calls=1

SEE ALSO

Perl::Critic::Pulp, Perl::Critic, Perl::Critic::Policy::CodeLayout::RequireTrailingCommas

HOME PAGE

http://user42.tuxfamily.org/perl-critic-pulp/index.html

COPYRIGHT

Copyright 2009, 2010, 2011 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>.