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 {} while or do {} until loops 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::RequireSimpleSortBlock
  • List::Util, List::Pairwise, Try, TryCatch, Syntax::Feature::Try

🏠 HOME PAGE 🏠

<http://user42.tuxfamily.org/perl-critic-pulp/index.html>


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