Jeffrey Ryan Thalhammer > Perl-Critic-More-1.003 > Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality

Download:
Perl-Critic-More-1.003.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

New  2
Open  3
View/Report Bugs
Module Version: 1.003   Source  

NAME ^

Perl::Critic::Policy::ValuesAndExpressions::RequireConstantOnLeftSideOfEquality - Putting the constant value on the left side of an equality exposes typos.

AFFILIATION ^

This policy is part of Perl::Critic::More, a bleeding edge supplement to Perl::Critic.

DESCRIPTION ^

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.

CONFIGURATION ^

This Policy is not configurable except for the standard options.

AUTHOR ^

Jeffrey Ryan Thalhammer <thaljef@cpan.org>

COPYRIGHT ^

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.

syntax highlighting: