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