1Perl::Critic::Policy::SUPuseberrrlo:uC:toCinrntiertsii:bc:u:Rt:eePqdouliPireceryFl:i:nDSaoulcbRuremoteuuntrtinan(te3is)o:n:RequireFinalReturn(3)
2
3
4
6 Perl::Critic::Policy::Subroutines::RequireFinalReturn - End every path
7 through a subroutine with an explicit "return" statement.
8
10 This Policy is part of the core Perl::Critic distribution.
11
13 Require all subroutines to terminate explicitly with one of the
14 following: "return", "carp", "croak", "die", "exec", "exit", "goto", or
15 "throw".
16
17 Subroutines without explicit return statements at their ends can be
18 confusing. It can be challenging to deduce what the return value will
19 be.
20
21 Furthermore, if the programmer did not mean for there to be a
22 significant return value, and omits a return statement, some of the
23 subroutine's inner data can leak to the outside. Consider this case:
24
25 package Password;
26 # every time the user guesses the password wrong, its value
27 # is rotated by one character
28 my $password;
29 sub set_password {
30 $password = shift;
31 }
32 sub check_password {
33 my $guess = shift;
34 if ($guess eq $password) {
35 unlock_secrets();
36 } else {
37 $password = (substr $password, 1).(substr $password, 0, 1);
38 }
39 }
40 1;
41
42 In this case, the last statement in check_password() is the assignment.
43 The result of that assignment is the implicit return value, so a wrong
44 guess returns the right password! Adding a "return;" at the end of
45 that subroutine solves the problem.
46
47 The only exception allowed is an empty subroutine.
48
49 Be careful when fixing problems identified by this Policy; don't
50 blindly put a "return;" statement at the end of every subroutine.
51
53 If you've created your own terminal functions that behave like "die" or
54 "exit", then you can configure Perl::Critic to recognize those
55 functions as well. Just put something like this in your .perlcriticrc:
56
57 [Subroutines::RequireFinalReturn]
58 terminal_funcs = quit abort bailout
59
60 If you've created your own terminal methods, then you can configure
61 Perl::Critic to recognize those methods as well, but the class won't be
62 considered. For example if you define throw_exception as terminal,
63 then any method of that name will be terminal, regardless of class.
64 Just put something like this in your .perlcriticrc:
65
66 [Subroutines::RequireFinalReturn]
67 terminal_methods = throw_exception
68
70 We do not look for returns inside ternary operators. That construction
71 is too complicated to analyze right now. Besides, a better form is the
72 return outside of the ternary like this: "return foo ? 1 : bar ? 2 : 3"
73
75 Chris Dolan <cdolan@cpan.org>
76
78 Copyright (c) 2005-2011 Chris Dolan.
79
80 This program is free software; you can redistribute it and/or modify it
81 under the same terms as Perl itself. The full text of this license can
82 be found in the LICENSE file included with this module.
83
84
85
86perl v5.32.1 Perl::Critic2:0:2P1o-l0i3c-y2:4:Subroutines::RequireFinalReturn(3)