



Perl Semantics




to this:






  1. I’m either traveling by car or just staying home.
  2. I’m either traveling by car or by train.


I’m either traveling by car or just staying home.

In Perl, this is the equivalent of:




I’m either traveling by car or by train.



Closing Thoughts


7 thoughts on “Ruby: On The Perl Origins Of “&&” And “||” Versus “and” And “or”.”

  1. Why is “I’m either traveling by car or by train” a misleading statement in English? It doesn’t imply that both possibilities will be true… I don’t see why you call it a misstatement.

  2. Just read English “or else” for Perlish/Rubish “or”, and the meaning is clear. English has several meanings of “or”, and the sequential meaning is one of them: Funny or die. Put up or shut up.

    Analogously, “A and B” means “A, and iff A succeeds, B”. This is the meaning of English “and” in instructions: Add water and stir.

    There’s no need for a xor operator as xor is basically the same as != . Naturally there’s no lazy evaluation with xor, as you always need both operands to determine the result.

  3. I would argue that the word “and” in common usage is still ambiguous, though we can say “and then” to imply the logical consequences of A being true. For example, the statement “Please put Alice in the car and [then] take her to day care.” seems to say B iff A, where it is necessary to know A to determine whether or not we should do B. Personally, though, I tend to not say “then”. It’s also interesting that in real life we could have just said B, “Please take Alice to day care.”, and it would be irrelevant if A is true.

    The statement “Please pick up the dry cleaning and pick up Alice from day care.”, however, implies that both A and B are true, not necessarily implying order; in other words they could just as well be written as two statement, “Please pick up the dry cleaning. Please pick up Alice from day care.” The use of “and” is only to create a list of items that need to be true, not to imply a relationship between them.

    We can easily mix these semantics in sentence like, “Please pick up the dry cleaning and Alice from day care, and make her a peanut butter and jelly sandwich and sliced apple or pear.” Despite several logically ambiguities I’d bet we’ll all mentally parse the semantics essentially the same way. We know that if the dry cleaners is closed we still need to pick up Alice, that a peanut butter and jelly sandwich is a single item and not “(peanut butter) and (jelly sandwich)”, that Alice should have some fruit even if we can’t make a sandwich, and if Alice wants both apple and pear that’s probably ok. (In others words it’s not necessarily an xor: we’re ambiguous.)

    Sorry… Not trying to dwell on idiosyncrasies, just pointing out some of the inferences we make in English. 🙂

  4. I’m sorry, but your article is a little misleading.

    actually, perl’s “or” and “and” and ruby ones are a little different beasts, though intendend to be used in a similiar circuimstances

    Perl operators work much the same as && and ||, albeit with much lower precedence. “and” has higher precendence than “or”. So, for expressions with simple “boolean”s (should quote that word for Perl) && and ||, and “and” and “or are more or less interchangeable.

    but in ruby “and” and “or” are on the same precedence level!

    so, perl’s

    func1 or func2 and func3

    is equivalent of

    func1 or (func2 and func3)

    and won’t call both func2 and func3 if func1 is “true”

    while in ruby this would be equivalent of

    (func1 or func2) and func3

    and won’t call func2, but WILL call func3 if func1 is true

    subtile enough to NOT recommend using “and” and “or” in ruby at all, unless you really know what you’re doing. postfix “if” and “unless” are better for control flow.

  5. (Sorry to bump this old thread, but that last comment should not be left for posterity here…)

    @codesnik Bash programmers have no problem with the Ruby “and” and “or”, since in both languages their precedence is equal.

    Your argument to not use “and” and “or” in Ruby “at all” is way too strong, based on too fine a point, since you would be crazy to suggest not using “&&” and “||” operators in Bash.

    When needed to produce more concise yet readable code, “and” and “or” in Ruby is just plain good. Anyone calling himself a Ruby expert cannot ignore them.

Leave a Reply
