Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • This problem is already caught by "use warnings" or the -w switch:

    $ perl -we '$a = 0; if ($a = 1) {print "a == 1!\n" }'
    Found = in conditional, should be == at -e line 1.
    a == 1!

    Granted, it's not normally a fatal error like the one you get by reversing the if, but in my opinion it is enough, and you don't need to resort to the reversal that many people find less readable. (Also, I haven't made that particular mistake in years--I do other kinds of mistakes ;-)
    • "use warnings" will however not catch the other scenario, which n1vux mentions, when you have

      while ($a = 3) { }

      turn into an infinite loop. Nor will it catch

      if ($a = my_sub()) { }

      (and good thing too). Having said that, I'm puzzled, what is considered to be strange about this formatting? The parentheses around the condition?

      • It does warn in the first case:
        $ perl -we '$a=0; while($a=3){}'
        Found = in conditional, should be == at -e line 1.

        But not in the second, so good catch!

        "what is considered to be strange about this formatting?"

        I'm sure you can get used to it after using it for a while, but there are some of us who like to read from left to right, and place the important things at the beginning of the "sentence" (this may depend on your native language, of course). I see this as a question about $a, not about 3, so I'd rather place the emphasis on $a.