1Perl::Critic::Policy::SUPuseberrrlo:uC:toCinrntiertsii:bc:u:Rt:eePqdouliPireceryFl:i:nDSaoulcbRuremoteuuntrtinan(te3is)o:n:RequireFinalReturn(3)
2
3
4

NAME

6       Perl::Critic::Policy::Subroutines::RequireFinalReturn - End every path
7       through a subroutine with an explicit "return" statement.
8

AFFILIATION

10       This Policy is part of the core Perl::Critic distribution.
11

DESCRIPTION

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

CONFIGURATION

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::RequireFinalReturns]
58           terminal_funcs = quit abort bailout
59

LIMITATIONS

61       We do not look for returns inside ternary operators.  That construction
62       is too complicated to analyze right now.  Besides, a better form is the
63       return outside of the ternary like this: "return foo ? 1 : bar ? 2 : 3"
64

AUTHOR

66       Chris Dolan <cdolan@cpan.org>
67
69       Copyright (c) 2005-2009 Chris Dolan.
70
71       This program is free software; you can redistribute it and/or modify it
72       under the same terms as Perl itself.  The full text of this license can
73       be found in the LICENSE file included with this module.
74
75
76
77perl v5.12.1          Perl::Critic2:0:1P0o-l0i9c-y0:8:Subroutines::RequireFinalReturn(3)
Impressum