line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################## |
2
|
|
|
|
|
|
|
# $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/RequireInterpolationOfMetachars.pm $ |
3
|
|
|
|
|
|
|
# $Date: 2008-07-07 09:09:13 -0700 (Mon, 07 Jul 2008) $ |
4
|
|
|
|
|
|
|
# $Author: clonezone $ |
5
|
|
|
|
|
|
|
# $Revision: 2537 $ |
6
|
|
|
|
|
|
|
############################################################################## |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality; |
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
5645
|
use 5.006001; |
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
229
|
|
11
|
|
|
|
|
|
|
|
12
|
6
|
|
|
6
|
|
29
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
167
|
|
13
|
6
|
|
|
6
|
|
43
|
use warnings; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
143
|
|
14
|
|
|
|
|
|
|
|
15
|
6
|
|
|
6
|
|
41
|
use Readonly; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
348
|
|
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
6
|
|
34
|
use Perl::Critic::Utils qw{ :severities }; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
340
|
|
18
|
6
|
|
|
6
|
|
758
|
use base 'Perl::Critic::Policy'; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
2159
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '1.003'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Readonly::Scalar my $DESC => q{Constant value on right side of equality}; |
27
|
|
|
|
|
|
|
Readonly::Scalar my $EXPL => |
28
|
|
|
|
|
|
|
q{Putting the constant on the left exposes typos}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
31
|
|
|
|
|
|
|
|
32
|
4
|
|
|
4
|
0
|
16970
|
sub supported_parameters { return () } |
33
|
7
|
|
|
7
|
1
|
268
|
sub default_severity { return $SEVERITY_LOW } |
34
|
1
|
|
|
1
|
1
|
86
|
sub default_themes { return qw(more) } |
35
|
3
|
|
|
3
|
1
|
47821
|
sub applies_to { return qw(PPI::Token::Operator) } |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub violates { |
40
|
18
|
|
|
18
|
1
|
969
|
my ( $self, $elem, undef ) = @_; |
41
|
18
|
100
|
100
|
|
|
41
|
return if !( q<==> eq $elem || q<eq> eq $elem ); |
42
|
|
|
|
|
|
|
|
43
|
15
|
|
50
|
|
|
313
|
my $right_sib = $elem->snext_sibling() || return; |
44
|
15
|
|
50
|
|
|
330
|
my $left_sib = $elem->sprevious_sibling() || return; |
45
|
|
|
|
|
|
|
|
46
|
15
|
100
|
100
|
|
|
283
|
if ( !_is_constant_like($left_sib) && _is_constant_like($right_sib) ) { |
47
|
6
|
|
|
|
|
26
|
return $self->violation( $DESC, $EXPL, $right_sib ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
9
|
|
|
|
|
27
|
return; # ok! |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _is_constant_like { |
56
|
24
|
|
|
24
|
|
29
|
my $elem = shift; |
57
|
24
|
100
|
|
|
|
116
|
return 1 if $elem->isa('PPI::Token::Number'); |
58
|
18
|
100
|
|
|
|
85
|
return 1 if $elem->isa('PPI::Token::Quote'); |
59
|
12
|
|
|
|
|
50
|
return 0; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
__END__ |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=pod |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=for stopwords lvalue mistyped |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 NAME |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality - Putting the constant value on the left side of an equality exposes typos. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 AFFILIATION |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This policy is part of L<Perl::Critic::More|Perl::Critic::More>, a bleeding |
79
|
|
|
|
|
|
|
edge supplement to L<Perl::Critic|Perl::Critic>. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 DESCRIPTION |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This policy warns you if you put a constant value (i.e. a literal number or |
84
|
|
|
|
|
|
|
some string) on the right side of a C<==> operator when there is a variable or |
85
|
|
|
|
|
|
|
some other lvalue on the left side. In a nutshell: |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
if($foo == 42){} # not ok |
88
|
|
|
|
|
|
|
if(42 == $foo){} # ok |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
if($foo eq 'bar'){} # not ok |
91
|
|
|
|
|
|
|
if('bar' eq $foo){} # ok |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
The rationale is that sometimes you might mistype C<=> instead of C<==>, and |
94
|
|
|
|
|
|
|
if you're in the habit of putting the constant value on the left side of the |
95
|
|
|
|
|
|
|
equality, then Perl will give you a compile-time warning. Perhaps this is |
96
|
|
|
|
|
|
|
best explained with an example: |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
if ($foo == 42){} # This is what I want it to do. |
99
|
|
|
|
|
|
|
if ($foo = 42){} # But suppose this is what I actually type. |
100
|
|
|
|
|
|
|
if (42 = $foo){} # If I had (mis)typed it like this, then Perl gives a warning. |
101
|
|
|
|
|
|
|
if (42 == $foo){} # So this is what I should have attempted to type. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
So this Policy doesn't actually tell you if you've mistyped C<=> instead of |
104
|
|
|
|
|
|
|
C<==>. Rather, it encourages you to write your expressions in a certain way |
105
|
|
|
|
|
|
|
so that Perl can warn you when you mistyped it. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
The C<eq> operator is not prone to the same type of typo as the C<==> |
108
|
|
|
|
|
|
|
operator, but this Policy still treats it the same way. Therefore, the rule |
109
|
|
|
|
|
|
|
is consistently applied to all equality operators, which helps you to get into |
110
|
|
|
|
|
|
|
the habit of writing compliant expressions faster. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 CONFIGURATION |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This Policy is not configurable except for the standard options. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Jeffrey Ryan Thalhammer <thaljef@cpan.org> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 COPYRIGHT |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
128
|
|
|
|
|
|
|
it under the same terms as Perl itself. The full text of this license |
129
|
|
|
|
|
|
|
can be found in the LICENSE file included with this module. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# Local Variables: |
134
|
|
|
|
|
|
|
# mode: cperl |
135
|
|
|
|
|
|
|
# cperl-indent-level: 4 |
136
|
|
|
|
|
|
|
# fill-column: 78 |
137
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
138
|
|
|
|
|
|
|
# c-indentation-style: bsd |
139
|
|
|
|
|
|
|
# End: |
140
|
|
|
|
|
|
|
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |