Perl::...micolon(3pm)
π Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon π
okay bestie, welcome to Perl::...micolon(3pm) β¨ β yes that's the page name, it's cute, it's giving abbreviated slay. This one's all about the Perl::Critic::Pulp add-on and it wants a semicolon at the END of your code blocks. periodt.
πΈ NAME πΈ
Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon β require a semicolon at the end of code blocks π
β¨ DESCRIPTION β¨
So this policy is part of the Perl::Critic::Pulp add-on, and what it does is ask you β very politely, but like, firmly β to put a ; on the final statement of a subroutine or a block. π
sub foo {
do_something(); # ok
}
sub bar {
do_something() # bad
}
The whole idea? If you later add more code, you don't have to go back and notice that the previous line needs a terminator. Also it's more like C, if that's a thing you consider a virtue π (it's a vibe, honestly).
This is only a matter of style β the code runs exactly the same either way β and on that basis this policy is low severity and lives under the "cosmetic" theme (peep "POLICY THEMES" in Perl::Critic). π¦
π§ Same Line Closing Brace
By default (look at "CONFIGURATION" down below π), you do not need a semicolon when the closing brace is sitting on the same line as the last statement. Iconic for constants and lil one-liners. β¨
sub foo { 'my-constant-value' } # ok
sub square { return $_[0] ** 2 } # ok
π Final Value Expression
No semicolon required when the last statement is an expression that gives a value. She's got main character energy and she doesn't need no terminator. π
map { some_thing();
$_+123 # ok
} @values;
do {
foo();
1+2+3 # ok
}
Right now, "expression blocks" means these specifically:
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, ngl. Fully qualified names like List::Util::first are recognised too. π
watch out tho βdo {} whileordo {} untilloops are ordinary blocks, not expression blocks, so they still require a semicolon on the last statement inside.
do {
foo() # bad
} until ($condition);
π Try/Catch Blocks
The "Try", "TryCatch" and "Syntax::Feature::Try" modules all add try block forms. These are blocks that don't require a terminating semicolon β same deal as how an "if" etc doesn't need one. π«Ά
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 just like any other blocks, but the try statement itself doesn't require a semicolon. (If you want something to catch one that got added unnecessarily, check out policy "ValuesAndExpressions::ProhibitNullStatements" β it will notice.) π
For reference: "PPI" doesn't know try/catch specifically, so when they don't have a final semicolon, the next statement runs together with them and the nature of those parts might get lost. This could upset things like recognition of "for" loops, and could potentially make some perlcritic reports go wrong. π so like, lowkey important.
And listen β 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
}
π Disabling
If you simply do not care about any of this, you can always disable it from your .perlcriticrc file in the usual way (see "CONFIGURATION" in Perl::Critic):
[-CodeLayout::RequireFinalSemicolon]
βοΈ CONFIGURATION βοΈ
except_same_line(boolean, default true) β if true (which, again, is the default) then it won'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 (default, bestie, keep up π ) then it won't demand a semicolon at the end of an expression block, as described under"Final Value Expression"up above.
# ok under "except_expression_blocks=yes"
# bad under "except_expression_blocks=no"
do { 1+2+3 }
map { $_+1 } @array
grep {defined} @x
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 tend to be unusual anyway. (For an example, see policy "BuiltinFunctions::RequireSimpleSortBlock" to demand "sort" is only one line.) β
π BUGS π
It is 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 someday. In general, this sort of ambiguity is another really good reason to avoid function prototypes, fr fr. π§
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 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::RequireSimpleSortBlockList::Util,List::Pairwise,Try,TryCatch,Syntax::Feature::Try
π HOME PAGE π
<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
π COPYRIGHT π
Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 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>.
and that's the whole page bestie, mwah π this lil gem shipped with perl v5.40.1, dated 2025-10-21, and it's filed under Perl::...micolon(3pm) in the User Contributed Perl Documentation. go forth and semicolon responsibly π«Άβ¨π
π¦ where this page comes from
- ubuntu resolute: libperl-critic-pulp-perl 100-1 Β· GPL-3.0-or-later
- ubuntu stonking: libperl-critic-pulp-perl 100-1 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms