in reply to Avoiding the == blues

Even easier, use warnings or -w:

>perl -w my $splort; if( $splort = 0 ) { print 'narf!'; } ^Z Found = in conditional, should be == at - line 2.

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re^2: Avoiding the == blues (!trivial)
by tye (Sage) on Dec 29, 2004 at 19:59 UTC

    That is not nearly as useful as it only catches trivial cases:

    > perl -w use strict; my $bool = 0; if( $bool = 0 ) { print "never\n"; } <EOF> Found = in conditional, should be == at - line 3. > perl -w use strict; my $bool = 0; sub test { return 0 == @ARGV } if( $bool = test() ) { print "Match\n"; } <EOF> Match

    - tye        

Re^2: Avoiding the == blues
by friedo (Prior) on Dec 29, 2004 at 20:00 UTC
    You are absolutely correct, of course, but this doesn't work for other languages, or for badly written scripts which generate thousands of warnings that you have to maintain. :)
      In some other languages, if takes a boolean, which is (deliberately) not what an assignment returns, so if (x = 0) doesn't get past the compiler.
      Well, saying "other languages" isn't quite correct, as it's the compiler that will issue a warning, if any. And gcc with -Wall will. It will even catch cases like:
      if (x = y)
      unlike Perls -w, which will let you do
      if ($x = $y)
      while keeping silent.