http://www.perlmonks.org?node_id=705517


in reply to Re^3: Avoiding silly programming mistakes
in thread Avoiding silly programming mistakes

All your example have a declaration in the test; so it seems more likely you want to do the assignment than the comparison.

However, Perl doesn't warn on

if ($foo = foo()) {...}
This is actually a common idiom, so it would be wrong for Perl to warn. Note that swapping the arguments around won't help you either:
if (foo() = $foo) {...}
may be valid (if foo() returns an lvalue). And whether it's valid or not is something you only know at run time - at compile time Perl cannot know whether foo returns an lvalue or not. So at best, you get a run-time error.

And if you want to compare two variables, there's no defense against mistyping '==' as '=', as both if ($foo = $bar) and if ($foo == $bar) is common.

Replies are listed 'Best First'.
Re^5: Avoiding silly programming mistakes
by Limbic~Region (Chancellor) on Aug 20, 2008 at 14:48 UTC
    JavaFan,
    I really don't mean to sound flippant but did you even read my node?

    I was showing that perl doesn't issue a warning any time it sees an assignment in a conditional - only when it makes sense. This was intended for others reading along, not for you since you obviously know what you are talking about.

    This is actually a common idiom, so it would be wrong for Perl to warn. Note that swapping the arguments around won't help you either: ... may be valid (if foo() returns an lvalue).

    Yep, exactly what I said. Perl assumes you know what you are doing and when flipping operands, an error would happen unless there was an lvalue at play.

    Cheers - L~R