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) $
6 ##############################################################################
8 package Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef;
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{"return" statement with explicit "undef"};
23 Readonly::Scalar my $EXPL => [ 199 ];
25 #-----------------------------------------------------------------------------
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' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
36 return if ($elem ne 'return');
37 return if is_hash_key($elem);
39 my $sib = $elem->snext_sibling();
41 return if !$sib->isa('PPI::Token::Word');
42 return if $sib ne 'undef';
44 # Must be 'return undef'
45 return $self->violation( $DESC, $EXPL, $elem );
52 #-----------------------------------------------------------------------------
58 Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef - Return failure with bare C<return> instead of C<return undef>.
62 This Policy is part of the core L<Perl::Critic> distribution.
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.
76 -f $file || return undef; #file doesn't exist!
78 #Continue reading file...
83 if ( my @data = read_file($filename) ){
85 # if $filename doesn't exist,
86 # @data will be (undef),
87 # but I'll still be in here!
93 # This is my error handling code.
94 # I probably want to be in here
95 # if $filname doesn't exist.
97 die "$filename not found";
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
107 -f $file || return; #DWIM!
109 #Continue reading file...
115 This Policy is not configurable except for the standard options.
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.
129 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
133 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
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.
143 # cperl-indent-level: 4
145 # indent-tabs-mode: nil
146 # c-indentation-style: bsd
148 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :