Login
Consolidated the ProhibitExit policies into a single Policy.
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / BuiltinFunctions / ProhibitExitInSubroutines.pm
CommitLineData
3f9ba540 1##############################################################################
572056d2
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
3f9ba540
JRT
6##############################################################################
7
572056d2 8package Perl::Critic::Policy::BuiltinFunctions::ProhibitExitInSubroutines;
3f9ba540
JRT
9
10use 5.006001;
11use strict;
12use warnings;
13use Readonly;
14
15use Perl::Critic::Utils qw{ :severities :classification };
16use base 'Perl::Critic::Policy';
17
18#-----------------------------------------------------------------------------
19
20our $VERSION = '1.095_001';
21
22#-----------------------------------------------------------------------------
23
24Readonly::Scalar my $DESC => q{"exit" called from within a subroutine};
25Readonly::Scalar my $EXPL => q{Use "die" or "croak" instead};
26
27#-----------------------------------------------------------------------------
28
572056d2
JRT
29sub supported_parameters { return () }
30sub default_severity { return $SEVERITY_MEDIUM }
31sub default_themes { return qw(core maintenance) }
32sub applies_to { return 'PPI::Token::Word' }
3f9ba540
JRT
33
34#-----------------------------------------------------------------------------
35
36sub violates {
37 my ( $self, $elem, $doc ) = @_;
38 return if $elem ne 'exit';
39 return if not is_function_call($elem);
cf901854 40
3f9ba540
JRT
41 my $statement = $elem->statement();
42 return if not $statement;
cf901854 43
3f9ba540
JRT
44 my $parent = $statement->parent();
45 return if not $parent;
cf901854 46
3f9ba540 47 return if $parent->isa('PPI::Document');
cf901854 48
3f9ba540
JRT
49 return $self->violation($DESC, $EXPL, $elem);
50}
51
52#-----------------------------------------------------------------------------
531;
54
55__END__
56
57#-----------------------------------------------------------------------------
58
59=pod
60
61=head1 NAME
62
572056d2 63Perl::Critic::Policy::BuiltinFunctions::ProhibitExitInSubroutines - Don't use exit() to throw exceptions from subroutines.
3f9ba540
JRT
64
65
66=head1 AFFILIATION
67
68This Policy is part of the core L<Perl::Critic|Perl::Critic>
69distribution.
70
71
72=head1 DESCRIPTION
73
3f9ba540
JRT
74A common newbie mistake is to use the C<exit> function when they encounter some
75kind error condition in their subroutines. But there is no way to trap an C<exit> call
cf901854 76to possibly recover from the error, which makes it difficult for others to use your
3f9ba540 77subroutine. Instead, you should be using C<die> or C<croak>, which can be trapped. Or
cf901854 78better yet, you can use a real exception mechanism, such as L<Exception::Class>.
3f9ba540
JRT
79
80So this Policy emits a violation any time that C<exit> is called from within a subroutine.
81
82To be fair, there are certain occasions where calling C<exit> from a subroutine is
83perfectly reasonable (C<Pod::Usage::pod2usage> is a very good example). In those cases,
84you should clearly document the behavior and add a C<"## no critic"> annotation
85for this Policy.
86
87
88=head1 SEE ALSO
89
572056d2 90L<Perl::Critic::Policy::ErrorHandling::RequireCarping|Perl::Critic::Policy::ErrorHandling::RequireCarping>
3f9ba540
JRT
91
92
93=head1 CONFIGURATION
94
95This Policy is not configurable except for the standard options.
96
97
98=head1 AUTHOR
99
100Jeffrey Ryan Thalhammer <thaljef@cpan.org>
101
102
103=head1 COPYRIGHT
104
105Copyright (c) 2005-2009 Jeffrey Ryan Thalhammer. All rights reserved.
106
107This program is free software; you can redistribute it and/or modify
108it under the same terms as Perl itself. The full text of this license
109can be found in the LICENSE file included with this module.
110
111=cut
112
113#############################################################################
114# Local Variables:
115# mode: cperl
116# cperl-indent-level: 4
117# fill-column: 78
118# indent-tabs-mode: nil
119# c-indentation-style: bsd
120# End:
121# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :