On 14/08/2013 18:21, Alexey Mishustin wrote:

If I make a typo and write a single "equals" operator here:

if ($foo = 2) {
        print "yes\n";
}

...then the "warnings" pragma works OK and tells me "Found = in
conditional, should be ==..."

But if I make the same typo and write a single "equals" operator there:

if ($foo = $bar) {
        print "yes\n";
}

... then I get no warning; the script output is "yes".

Why is it so?

How could I catch such typos?

The `warnings` pragma catches the first form because the value of the
conditional is a *constant* 2, so the body of the `if` will *always* be
executed and it is a fair guess that the problem is `=` instaed of `==`.

In the second case the execution depends on the value of `$bar`, and it
is a reasonable thing to write if, say, you wanted to test a value and
assign it to a temporary variable at the same time. Perl cannot know
that the code was unintentional and will not warn you.

I have checked both Perl::Critic and B::Lint, and neither of these check
for an assignment operator in a conditional expression. The only thing
way I can think of doing this is to write a plugin for B::Lint, which
accepts Module::Pluggable plugins. It may be possible to write something
that will do what you want.

Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to