Perl::...lReturn(3pm)
π NAME π
okay so this is Perl::...lReturn(3pm), fresh from the User Contributed Perl Documentation corner of the internet π«Ά. It's the page for Perl::Critic::Policy::Subroutines::RequireFinalReturn β the policy whose whole thing is: end every path through a subroutine with an explicit "return" statement. periodt π
.
π¦ AFFILIATION π¦
This Policy is part of the core Perl::Critic distribution. iconic behavior, honestly β¨.
β¨ DESCRIPTION β¨
Require all subroutines to terminate explicitly with one of the following: return, carp, croak, die, exec, exit, goto, or throw. That's the whole checklist, bestie π.
Subroutines without explicit return statements at their ends can be confusing. It can be challenging to deduce what the return value will be. Like, the vibe is mysterious, but not in a fun way π.
Furthermore, if the programmer did not mean for there to be a significant return value, and omits a return statement, some of the subroutine's inner data can leak to the outside. Consider this case:
package Password;
# every time the user guesses the password wrong, its value
# is rotated by one character
my $password;
sub set_password {
$password = shift;
}
sub check_password {
my $guess = shift;
if ($guess eq $password) {
unlock_secrets();
} else {
$password = (substr $password, 1).(substr $password, 0, 1);
}
}
1;
In this case, the last statement in check_password() is the assignment. The result of that assignment is the implicit return value, so a wrong guess returns the right password π! Adding a return; at the end of that subroutine solves the problem.
The only exception allowed is an empty subroutine.
Bestie, be careful when fixing problems identified by this Policy; don't blindly put a return; statement at the end of every subroutine. That is not the slay. π¨
π CONFIGURATION π
If you've created your own terminal functions that behave like die or exit, then you can configure Perl::Critic to recognize those functions as well. Just put something like this in your .perlcriticrc:
[Subroutines::RequireFinalReturn]
terminal_funcs = quit abort bailout
If you've created your own terminal methods, then you can configure Perl::Critic to recognize those methods as well, but the class won't be considered. For example if you define throw_exception as terminal, then any method of that name will be terminal, regardless of class. Just put something like this in your .perlcriticrc:
[Subroutines::RequireFinalReturn]
terminal_methods = throw_exception
π BUGS π
We do not look for returns inside ternary operators. That construction is too complicated to analyze right now. Besides, a better form is the return outside of the ternary like this: "return foo ? 1 : bar ? 2 : 3". So yeah, ternary return-hunting is not on the menu π« .
π AUTHOR π
Chris Dolan <cdolan@cpan.org>
π COPYRIGHT π
Copyright (c) 2005-2023 Chris Dolan.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.
xoxo, your manual bestie π
β this page came from perl v5.42.0, dated 2024-10-23.
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms