Slashdot Mirror


Apocalypse 3

rob_99 writes: "The third installment of the Apocalypse is out!" You may have missed the first or second Apocalypses. This one is roughly "all about operators".

4 of 151 comments (clear)

  1. No, result should depend on types of operands by SMN · · Score: 3, Informative
    'a'+'b' = 'c'?
    'a'+'b' = "ab"?
    No, the results of this should rely on the type of the operands, as it does with numerical values in C and C++ and most C++ classes. Keep in mind that single quotes denote a character (really an 8-bit ASCII value), and double quotes indicate a string (usually zero-terminated):

    'a' + 'b' = 'Ã'
    (char and char results in another char by adding their ASCII values)

    "a" + 'b' = "ab"
    (string and char results in a string by concatenation)

    'a' + "b" = "ab"
    (char and string -- order does not matter, so see string and char above)

    "a" + "b" = "ab"
    (string and string results in a string by concatenation)

    To summarize, if a string is involved, then the char(s) is (are) promoted up to string(s) and the result depends on concatenation. If all operands are chars, then no promotion occurs and they are added by ASCII value.

    This is similar to the numerical addition rules of C, C++, and a number of similar languages. One simple example: If an int and a float are added, the int is promoted to be a float and the result is a float. If an int and an int are added, the result is an int and must by cast (either explicitly or implicitly) in order to be a float value.

    I'm not too familiar with Perl, but this seems to be the most sensible behavior. If I'm missing something and there's a valid reason not to use an addition sign for concatenation, please reply and let me know.

    --
    -- Imagine how much more advanced our technology would be if we had eight fingers per hand.
    1. Re:No, result should depend on types of operands by James+Lanfear · · Score: 3, Informative
      No, the results of this should rely on the type of the operands, as it does with numerical values in C and C++ and most C++ classes.

      They do, but the rather essential point that your missing is that in Perl both 1 and "1" are scalar -- they have the same type (in an obviously broader sense of the word). "1" + "1" produces "2" (or 2), as does 1 + "1", "1" + 1, and 1 + 1; and, as you might expect, "1" . "1" and 1 . 1 (note the whitespace) produces "11". Thus the need for distinct syntax, which is already present in the equality operators, where you have == vs eq, > vs gt, etc.

      Keep in mind that single quotes denote a character (really an 8-bit ASCII value), and double quotes indicate a string (usually zero-terminated)

      As has already been stated, single and double quoting controls variable interpolation in Perl; there are no character constants as such. And since we're playing lanaguage lawyer, (non-wide-)character constants in C are actually int's, and are not required to be ASCII (which only uses 7 bits, anyway), and strings are by definition null-terminated.

      I'm not too familiar with Perl

      Which begs the question of why you posted in the first place.

  2. Re:Not a good title by Matchstick · · Score: 3, Informative
    See the definition:
    Apocalypse \A*poc"a*lypse\, n. [L. apocalypsis, Gr. ?, fr. ? to uncover, to disclose; ? from + ? to cover, conceal: cf. F. apocalypse.] 1. The revelation delivered to St. John, in the isle of Patmos, near the close of the first century, forming the last book of the New Testament. 2. Anything viewed as a revelation; a disclosure.
    Or read the first paragraph of the first apocalypse:
    People get scared when they hear the word Apocalypse, but here I mean it in the good sense: a Revealing. An Apocalypse is supposed to reveal good news to good people. (And if it also happens to reveal bad news to bad people, so be it. Just don't be bad.)
  3. Re:More eclectic, less practical... by Fixer · · Score: 3, Informative

    Okay. All languages that are Turing complete are equivalent, as you well know. So why do we need more than one language?

    --
    "Avast! Prepare for the rodgering!" THWACK! "Arrr.. me nards.."