in reply to not 'another victim of precedence' ? 'It is true' : 'the code is false'

It may be helpful to keep in mind that anytime a logical operator has both a symbol form, and a word form, the word form is always designed to be very very low precedence.

Examples:

I mention this because hopefully this consistant behavior will be easier to remember once its been pointed out explicitly.


Dave

Replies are listed 'Best First'.
Re^2: not 'another victim of precedence' ? 'It is true' : 'the code is false'
by bobf (Monsignor) on Apr 06, 2005 at 06:59 UTC

    Thanks - that's a very good point and I'm glad you mentioned it. I know the word forms are lower precedence than the symbol forms, * trumps +, and four of a kind beats a full house, but I still need to consult the precedence table regularly to keep them all straight. :)

    If in doubt (or to clarify the code) I'll usually either split the statement up (as friedo mentioned) or just add a set of parentheses. This time, however, I jumped to an incorrect conclusion regarding the source of the problem, and precedence didn't cross my mind until later.

Re: not 'another victim of precedence' ? 'It is true' : 'the code is false'
by jonadab (Parson) on Apr 06, 2005 at 21:11 UTC
    anytime a logical operator has both a symbol form, and a word form, the word form is always designed to be very very low precedence

    Indeed. I like to think of the spelled-out logical ops as almost being a special class of control-flow operators, useful for things like this:

    dosomething with some . very(comple)x expression or warn "Some Error Message";

    The spelled-out logical ops are more verbose (in terms of the number of characters) than the punctuative ones, and they connect more verbose things -- whole expressions. If you need to perform a logical operation on a single term, then you either want to use the punctuative logical ops, or parenthesize. (I tend to do the latter, because I like the way (foo and bar) reads better than foo && bar, but I'm pretty sure that's a personal idiosyncracy I have, and the other way should work just as well.)


    "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
Re^2: not 'another victim of precedence' ? 'It is true' : 'the code is false'
by ysth (Canon) on Apr 06, 2005 at 09:00 UTC

      Should I be proud that I seem to have forked Perl?

      =cut
      --Brent Dax
      There is no sig.

        Yes! More power to your...elbow!
Re^2: not 'another victim of precedence' ? 'It is true' : 'the code is false'
by Smylers (Pilgrim) on Apr 07, 2005 at 14:28 UTC
    It may be helpful to keep in mind that anytime a logical operator has both a symbol form, and a word form, the word form is always designed to be very very low precedence.

    Indeed. It occurred to me that I've never been caught by this because I never use the not operator, always using !.

    That made me wonder why Perl has not. I make use of the distinction between or and ||, but under what circumstances would not be useful?

    Smylers