|
NAMEPerl::Critic::Policy::CodeLayout::RequireFinalSemicolon - require a semicolon at the end of code blocksDESCRIPTIONThis policy is part of the "Perl::Critic::Pulp" add-on. 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 } The idea is that if you add more code you don't have to notice the previous line needs a terminator. It's also more like the C language, if you consider that a virtue. This is only a matter of style since the code runs the same either way, and on that basis this policy is low severity and under the "cosmetic" theme (see "POLICY THEMES" in Perl::Critic). Same Line Closing BraceBy 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 square { return $_[0] ** 2 } # ok Final Value ExpressionA semicolon is not required in places where the last statement is an expression giving a value.map { some_thing(); $_+123 # ok } @values; do { foo(); 1+2+3 # ok } This currently means do grep map sort # builtins reduce any all none notall first # List::Util pairfirst pairgrep pairmap mapp map_pairwise grepp grep_pairwise # List::Pairwise firstp first_pairwise lastp last_pairwise These module function names are always treated as expressions. There's no check for whether the respective module is actually in use. Fully qualified names like "List::Util::first" are recognised too. "do {} while" or "do {} until" loops are ordinary blocks, not expression blocks, so still require a semicolon on the last statement inside. do { foo() # bad } until ($condition); Try/Catch BlocksThe "Try", "TryCatch" and "Syntax::Feature::Try" modules all add "try" block forms. These are blocks not requiring a terminating semicolon, the same as an "if" etc doesn't.use TryCatch; sub foo { try { attempt_something(); } catch { error_recovery(); } # ok, no semi required here for TryCatch } The insides of the "try" and "catch" are the same as other blocks, but the "try" statement itself doesn't require a semicolon. (See policy "ValuesAndExpressions::ProhibitNullStatements" to notice one added unnecessarily.) For reference, "PPI" doesn't know "try"/"catch" specifically, so when they don't have a final semicolon the next statement runs together and the nature of those parts might be lost. This could upset things like recognition of "for" loops and could potentially make some perlcritic reports go wrong. The "try"/"catch" block exemption here is only for the modules with this block syntax. There are other try modules such as "Try::Tiny" and friends where a final semicolon is normal and necessary if more code follows (because their "try" and "catch" are ordinary function calls prototyped to take code blocks). use Try::Tiny; sub foo { try { attempt_something(); } catch { error_recovery(); } # bad, semi required here for Try::Tiny } DisablingIf 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] CONFIGURATION
BUGSIt'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 }; # hash ref normally # code block if foo() has prototype "PPI" tends to assume code. "RequireFinalSemicolon" currently assumes hashref so as to avoid false violations. Any "try", "catch" or "finally" are presumed to be code blocks (the various Try modules). Perhaps other common or particular functions or syntax with code blocks could be recognised. In general this sort of ambiguity is another good reason to avoid function prototypes. PPI as of its version 1.270 sometimes takes hashrefs in lists and arrarefs to be code blocks, eg. ppidump 'foo({%y,x=>1})' ppidump '[{%y,x=>1}]' ppidump '[{x=>1,%y}]' # ok, hash SEE ALSOPerl::Critic::Pulp, Perl::Critic, Perl::Critic::Policy::CodeLayout::RequireTrailingCommas, Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline, Perl::Critic::Policy::Subroutines::RequireFinalReturn, Perl::Critic::Policy::ValuesAndExpressions::ProhibitNullStatements, Perl::Critic::Policy::BuiltinFunctions::RequireSimpleSortBlockList::Util, List::Pairwise, Try, TryCatch, Syntax::Feature::Try HOME PAGE<http://user42.tuxfamily.org/perl-critic-pulp/index.html>COPYRIGHTCopyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin RydePerl-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>.
Visit the GSP FreeBSD Man Page Interface. |