Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality - Putting the constant value on the left side of an equality exposes typos.
This policy is part of Perl::Critic::More, a bleeding edge supplement to Perl::Critic.
This policy warns you if you put a constant value (i.e. a literal number or some string) on the right side of a == operator when there is a variable or some other lvalue on the left side. In a nutshell:
==
if($foo == 42){} # not ok if(42 == $foo){} # ok if($foo eq 'bar'){} # not ok if('bar' eq $foo){} # ok
The rationale is that sometimes you might mistype = instead of ==, and if you're in the habit of putting the constant value on the left side of the equality, then Perl will give you a compile-time warning. Perhaps this is best explained with an example:
=
if ($foo == 42){} # This is what I want it to do. if ($foo = 42){} # But suppose this is what I actually type. if (42 = $foo){} # If I had (mis)typed it like this, then Perl gives a warning. if (42 == $foo){} # So this is what I should have attempted to type.
So this Policy doesn't actually tell you if you've mistyped = instead of ==. Rather, it encourages you to write your expressions in a certain way so that Perl can warn you when you mistyped it.
The eq operator is not prone to the same type of typo as the == operator, but this Policy still treats it the same way. Therefore, the rule is consistently applied to all equality operators, which helps you to get into the habit of writing compliant expressions faster.
eq
This Policy is not configurable except for the standard options.
Jeffrey Ryan Thalhammer <thaljef@cpan.org>
Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.
To install Perl::Critic::More, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Critic::More
CPAN shell
perl -MCPAN -e shell install Perl::Critic::More
For more information on module installation, please visit the detailed CPAN module installation guide.