| Perl-Critic-Pulp documentation | view source | Contained in the Perl-Critic-Pulp distribution. |
Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon - require a semicolon at the end of code blocks
This policy is part of the Perl::Critic::Pulp|Perl::Critic::Pulp
addon. It asks you to put a semicolon ; on the final statement of a
subroutine or block.
sub foo {
do_something(); # ok
}
sub bar {
do_something() # bad
}
This is only a matter of style since the code runs the same either way, and on that basis this policy is low priority and under the "cosmetic" theme (see POLICY THEMES in Perl::Critic).
The advantage of a semicolon is that if your add more code you don't have to notice the previous line needs a terminator. It's also more C-like, if you consider C-like to be a virtue.
By default (see CONFIGURATION below) a semicolon is not required when the closing brace is on the same line as the last statement. This is good for constants and one-liners.
sub foo { 'my-constant-value' } # ok
sub bar { return $x ** 2 } # ok
Nor is a semicolon required in places where the last statement is an
expression giving a value, which currently means a do, grep or map
block.
map { some_thing();
$_+123 # ok
} @values;
do {
foo();
1+2+3 # ok
}
However a do {} while or do {} until loop still requires a semicolon
like ordinary blocks.
do {
foo() # bad
} until ($condition);
The last statement of a sub{} is not considered an "expression" like a
do. Perhaps there could be an option to excuse all one-statement subs or
even all subs and have the policy just for nested code and control blocks.
For now the suggestion is that if a sub is big enough to need a separate
line for its result expression then write an actual return statement for
maximum clarity.
If you don't care about this you can always disable from your .perlcriticrc file in the usual way (see CONFIGURATION in Perl::Critic),
[-CodeLayout::RequireFinalSemicolon]
except_same_line (boolean, default true)If true (the default) then don't demand a semicolon if the closing brace is on the same line as the final statement.
sub foo { return 123 } # ok if "except_same_line=yes"
# bad if "except_same_line=no"
except_expression_blocks (boolean, default true)If true (the default) then don't demand a semicolon at the end of an
expression block, which currently means do, grep and map.
# ok under "except_expression_blocks=yes"
# bad under "except_expression_blocks=no"
do { 1+2+3 }
map { $_+1 } @array
grep {defined} @x
In the future this might also apply to first from List::Util and the
like, probably a hard-coded list of common things and perhaps configurable
extras.
It's very difficult to distinguish a code block from an anonymous hashref constructor if there might be a function prototype in force, eg.
foo { abc => 123 };
PPI tends to assume it's code, RequireFinalSemicolon instead assumes
hashref so as to avoid false violations. Perhaps particular functions with
prototypes could be recognised, but in general this sort of thing is another
good reason to avoid prototypes.
Perl::Critic::Pulp, Perl::Critic, Perl::Critic::Policy::CodeLayout::RequireTrailingCommas, Perl::Critic::Policy::Subroutines::RequireFinalReturn, Perl::Critic::Policy::ValuesAndExpressions::ProhibitNullStatements
http://user42.tuxfamily.org/perl-critic-pulp/index.html
Copyright 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>.
| Perl-Critic-Pulp documentation | view source | Contained in the Perl-Critic-Pulp distribution. |