.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon 3pm" .TH Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon 3pm "2021-02-28" "perl v5.32.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon \- require a semicolon at the end of code blocks .SH "DESCRIPTION" .IX Header "DESCRIPTION" This policy is part of the \f(CW\*(C`Perl::Critic::Pulp\*(C'\fR add-on. It asks you to put a semicolon \f(CW\*(C`;\*(C'\fR on the final statement of a subroutine or block. .PP .Vb 3 \& sub foo { \& do_something(); # ok \& } \& \& sub bar { \& do_something() # bad \& } .Ve .PP 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. .PP 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 \*(L"cosmetic\*(R" theme (see \*(L"\s-1POLICY THEMES\*(R"\s0 in Perl::Critic). .SS "Same Line Closing Brace" .IX Subsection "Same Line Closing Brace" By default (see \*(L"\s-1CONFIGURATION\*(R"\s0 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. .PP .Vb 1 \& sub foo { \*(Aqmy\-constant\-value\*(Aq } # ok \& \& sub square { return $_[0] ** 2 } # ok .Ve .SS "Final Value Expression" .IX Subsection "Final Value Expression" A semicolon is not required in places where the last statement is an expression giving a value. .PP .Vb 3 \& map { some_thing(); \& $_+123 # ok \& } @values; \& \& do { \& foo(); \& 1+2+3 # ok \& } .Ve .PP This currently means .PP .Vb 1 \& 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 .Ve .PP 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 \f(CW\*(C`List::Util::first\*(C'\fR are recognised too. .PP \&\f(CW\*(C`do {} while\*(C'\fR or \f(CW\*(C`do {} until\*(C'\fR loops are ordinary blocks, not expression blocks, so still require a semicolon on the last statement inside. .PP .Vb 3 \& do { \& foo() # bad \& } until ($condition); .Ve .SS "Try/Catch Blocks" .IX Subsection "Try/Catch Blocks" The \f(CW\*(C`Try\*(C'\fR, \f(CW\*(C`TryCatch\*(C'\fR and \f(CW\*(C`Syntax::Feature::Try\*(C'\fR modules all add \f(CW\*(C`try\*(C'\fR block forms. These are blocks not requiring a terminating semicolon, the same as an \f(CW\*(C`if\*(C'\fR etc doesn't. .PP .Vb 8 \& use TryCatch; \& sub foo { \& try { \& attempt_something(); \& } catch { \& error_recovery(); \& } # ok, no semi required here for TryCatch \& } .Ve .PP The insides of the \f(CW\*(C`try\*(C'\fR and \f(CW\*(C`catch\*(C'\fR are the same as other blocks, but the \&\f(CW\*(C`try\*(C'\fR statement itself doesn't require a semicolon. (See policy \&\f(CW\*(C`ValuesAndExpressions::ProhibitNullStatements\*(C'\fR to notice one added unnecessarily.) .PP For reference, \f(CW\*(C`PPI\*(C'\fR doesn't know \f(CW\*(C`try\*(C'\fR/\f(CW\*(C`catch\*(C'\fR 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 \f(CW\*(C`for\*(C'\fR loops and could potentially make some perlcritic reports go wrong. .PP The \f(CW\*(C`try\*(C'\fR/\f(CW\*(C`catch\*(C'\fR block exemption here is only for the modules with this block syntax. There are other try modules such as \f(CW\*(C`Try::Tiny\*(C'\fR and friends where a final semicolon is normal and necessary if more code follows (because their \f(CW\*(C`try\*(C'\fR and \f(CW\*(C`catch\*(C'\fR are ordinary function calls prototyped to take code blocks). .PP .Vb 8 \& use Try::Tiny; \& sub foo { \& try { \& attempt_something(); \& } catch { \& error_recovery(); \& } # bad, semi required here for Try::Tiny \& } .Ve .SS "Disabling" .IX Subsection "Disabling" If you don't care about this you can always disable from your \&\fI.perlcriticrc\fR file in the usual way (see \*(L"\s-1CONFIGURATION\*(R"\s0 in Perl::Critic), .PP .Vb 1 \& [\-CodeLayout::RequireFinalSemicolon] .Ve .SH "CONFIGURATION" .IX Header "CONFIGURATION" .ie n .IP """except_same_line"" (boolean, default true)" 4 .el .IP "\f(CWexcept_same_line\fR (boolean, default true)" 4 .IX Item "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. .Sp .Vb 2 \& sub foo { return 123 } # ok if "except_same_line=yes" \& # bad if "except_same_line=no" .Ve .ie n .IP """except_expression_blocks"" (boolean, default true)" 4 .el .IP "\f(CWexcept_expression_blocks\fR (boolean, default true)" 4 .IX Item "except_expression_blocks (boolean, default true)" If true (the default) then don't demand a semicolon at the end of an expression block, as described under \*(L"Final Value Expression\*(R" above. .Sp .Vb 5 \& # ok under "except_expression_blocks=yes" \& # bad under "except_expression_blocks=no" \& do { 1+2+3 } \& map { $_+1 } @array \& grep {defined} @x .Ve .Sp The statements and functions for this exception are currently hard coded. Maybe in the future they could be configurable, though multi-line expressions in this sort of thing tends to be unusual anyway. (See policy \&\f(CW\*(C`BuiltinFunctions::RequireSimpleSortBlock\*(C'\fR for example to demand \f(CW\*(C`sort\*(C'\fR is only one line.) .SH "BUGS" .IX Header "BUGS" It's very difficult to distinguish a code block from an anonymous hashref constructor if there might be a function prototype in force, eg. .PP .Vb 2 \& foo { abc => 123 }; # hash ref normally \& # code block if foo() has prototype .Ve .PP \&\f(CW\*(C`PPI\*(C'\fR tends to assume code. \f(CW\*(C`RequireFinalSemicolon\*(C'\fR currently assumes hashref so as to avoid false violations. Any \f(CW\*(C`try\*(C'\fR, \f(CW\*(C`catch\*(C'\fR or \f(CW\*(C`finally\*(C'\fR 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. .PP \&\s-1PPI\s0 as of its version 1.270 sometimes takes hashrefs in lists and arrarefs to be code blocks, eg. .PP .Vb 2 \& ppidump \*(Aqfoo({%y,x=>1})\*(Aq \& ppidump \*(Aq[{%y,x=>1}]\*(Aq \& \& ppidump \*(Aq[{x=>1,%y}]\*(Aq # ok, hash .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Perl::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::RequireSimpleSortBlock .PP List::Util, List::Pairwise, Try, TryCatch, Syntax::Feature::Try .SH "HOME PAGE" .IX Header "HOME PAGE" .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde .PP Perl-Critic-Pulp is free software; you can redistribute it and/or modify it under the terms of the \s-1GNU\s0 General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. .PP Perl-Critic-Pulp is distributed in the hope that it will be useful, but \&\s-1WITHOUT ANY WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS FOR A PARTICULAR PURPOSE.\s0 See the \s-1GNU\s0 General Public License for more details. .PP You should have received a copy of the \s-1GNU\s0 General Public License along with Perl-Critic-Pulp. If not, see .