NAME
Perl::Critic::Policy::CodeLayout::inprogressProhibitFatCommaAfterNewline - new enough Test::More for its functions
DESCRIPTION
This policy is part of the Perl::Critic::Pulp
addon. It asks you not to put a newline between a fat comma and a bareword it's meant to quote.
my %h = (foo # bad
=> 123);
In Perl 5.6 and earlier such a foo
is not quoted, instead being a function call.
Or in Perl 5.8 and up builtins like print
there are not quoted, instead executing as an expression.
my %h = (print # bad, "print" executes
=>
'123');
# h is key "1" value 123
For reference, a "-foo" of a Perl builtin unquoted too, the "-" which otherwise quotes a bareword doesn't affect Perl builtins,
my %h = (-print # bad, "print" execute and negate
=>
'123');
# h is key "-1" value 123
Avoiding Problems
The idea of this policy is to avoid problems by keeping the =>
on the same line as the word.
my %h = (foo => # ok
123);
If you do want a newline then a string or expression can be used
my %h = ('foo' # ok
=>
123);
Or if it really is a function call you wanted (and implicitly get in 5.6 and earlier), then parens in the usual way ensure that.
my %h = (foo() # ok
=>
123);
In Perl 5.6, use strict
(strict "subs") will throw an error for an unquoted bareword foo
, but if you've got a function or constant called foo
then it will execute, probably giving the wrong result. One way that can go wrong is an accidental redefinition of a constant,
use constant FOO => 'something';
# makes a constant subr called something()
use constant FOO
=> 'some value';
If FOO
is a number or other invalid name for a subr then new enough versions of the constant
module will pick it up at runtime, but a word like "something" here quietly expands.
Perl 5.8 up looks ahead across newlines for a =>
quoting a bareword before reckoning it a function call, which means most barewords are fine. But the same is not done for Perl builtins like print
, they instead execute and many will do so successfully, or silently, leaving a return value instead of the presumably intended word string.
Disabling
As always if you don't care about this then you can disable inprogressProhibitFatCommaAfterNewline
from your .perlcriticrc in the usual way (see "CONFIGURATION" in Perl::Critic),
[-CodeLayout::inprogressProhibitFatCommaAfterNewline]
Perhaps this policy could report only cases where =>
doesn't quote, so in 5.8 up most words are ok, only report unquoted builtins. But currently it's a blanket prohibition
SEE ALSO
Perl::Critic::Pulp, Perl::Critic, perlop
HOME PAGE
http://user42.tuxfamily.org/perl-critic-pulp/index.html
COPYRIGHT
Copyright 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/>.