Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / Subroutines / ProhibitExplicitReturnUndef.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Subroutines/ProhibitExplicitReturnUndef.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{"return" statement with explicit "undef"};
23 Readonly::Scalar my $EXPL => [ 199 ];
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters { return ()                 }
28 sub default_severity     { return $SEVERITY_HIGHEST  }
29 sub default_themes       { return qw(core pbp bugs)  }
30 sub applies_to           { return 'PPI::Token::Word' }
31
32 #-----------------------------------------------------------------------------
33
34 sub violates {
35     my ( $self, $elem, undef ) = @_;
36     return if ($elem ne 'return');
37     return if is_hash_key($elem);
38
39     my $sib = $elem->snext_sibling();
40     return if !$sib;
41     return if !$sib->isa('PPI::Token::Word');
42     return if $sib ne 'undef';
43
44     # Must be 'return undef'
45     return $self->violation( $DESC, $EXPL, $elem );
46 }
47
48 1;
49
50 __END__
51
52 #-----------------------------------------------------------------------------
53
54 =pod
55
56 =head1 NAME
57
58 Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef - Return failure with bare C<return> instead of C<return undef>.
59
60 =head1 AFFILIATION
61
62 This Policy is part of the core L<Perl::Critic> distribution.
63
64
65 =head1 DESCRIPTION
66
67 Returning C<undef> upon failure from a subroutine is pretty common.
68 But if the subroutine is called in list context, an explicit C<return
69 undef;> statement will return a one-element list containing
70 C<(undef)>.  Now if that list is subsequently put in a boolean context
71 to test for failure, then it evaluates to true.  But you probably
72 wanted it to be false.
73
74   sub read_file {
75       my $file = shift;
76       -f $file || return undef;  #file doesn't exist!
77
78       #Continue reading file...
79   }
80
81   #and later...
82
83   if ( my @data = read_file($filename) ){
84
85       # if $filename doesn't exist,
86       # @data will be (undef),
87       # but I'll still be in here!
88
89       process(@data);
90   }
91   else{
92
93       # This is my error handling code.
94       # I probably want to be in here
95       # if $filname doesn't exist.
96
97       die "$filename not found";
98   }
99
100 The solution is to just use a bare C<return> statement whenever you
101 want to return failure.  In list context, Perl will then give you an
102 empty list (which is false), and C<undef> in scalar context (which is
103 also false).
104
105   sub read_file {
106       my $file = shift;
107       -f $file || return;  #DWIM!
108
109       #Continue reading file...
110   }
111
112
113 =head1 CONFIGURATION
114
115 This Policy is not configurable except for the standard options.
116
117
118 =head1 NOTES
119
120 You can fool this policy pretty easily by hiding C<undef> in a boolean
121 expression.  But don't bother trying.  In fact, using return values to
122 indicate failure is pretty poor technique anyway.  Consider using
123 C<die> or C<croak> with C<eval>, or the L<Error> module for a much
124 more robust exception-handling model.  Conway has a real nice
125 discussion on error handling in chapter 13 of PBP.
126
127 =head1 AUTHOR
128
129 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
130
131 =head1 COPYRIGHT
132
133 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
134
135 This program is free software; you can redistribute it and/or modify
136 it under the same terms as Perl itself.  The full text of this license
137 can be found in the LICENSE file included with this module.
138
139 =cut
140
141 # Local Variables:
142 #   mode: cperl
143 #   cperl-indent-level: 4
144 #   fill-column: 78
145 #   indent-tabs-mode: nil
146 #   c-indentation-style: bsd
147 # End:
148 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :