in reply to Re^2: grep for hash?
in thread grep for hash?

All of the references are to the hashes %new and %old; the leading @ and curlies is just the way you name slices. The only array involved in the whole thing is @keys.

As for relying on the ordering, I think it's pretty safe to count that the rvalue will be computed and available before the lvalue is populated with it.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: grep for hash?
by ikegami (Patriarch) on Jul 07, 2009 at 15:50 UTC

    I think it's pretty safe to count that the rvalue will be computed and available before the lvalue is populated with it.

    In general, no. The container can be placed on the stack before the value it will receive. The assignment only occurs after both the value and the container are on the stack.

    In Perl, operands are always evaluated from left to right, except the operands for assignment operators are always evaluated from right to left. But that's not documented.

    GivenDisinguishes these Interpretations
    Operator Precedence1+2*3(1+2)*3
    -vs-
    1+(2*3)
    Operator Associativity2**3**4(2**3)**4
    -vs-
    2**(3**4)
    Operand Evaluation Orderfoo()+bar()foo() -> bar() -> add
    -vs-
    bar() -> foo() -> add

    Update: I foresaw confusion between operator associativity and operand evaluation order, so I added the table.

      It's not explicitly documented but it's stated in perlsyn that the assignment operator works "as in C", and it has the same right-to-left associativity as C's, and the value of the assignment is itself a valid rvalue for another subsequent assignment. Granted it's not chiseled into stone tablets, but I don't think that it's a "ZOMGWTFBBQ no one knows what'll happen just don't do that" case like ++$a + $a++ is.

      That being said, your alternative with the separate assignment before use is definitely a clearer implementation.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        it has the same right-to-left associativity as C's

        Operator associativity is not related to operand evaluation order.

        Perl even have a case where they differ. Exponentiation is right-associative, but its operands are evaluated from left to right.

        it's stated in perlsyn that the assignment operator works "as in C"

        Operand evaluation order is not defined in C, and different compilers use different orders. (Well, so I was told.)

        but I don't think that it's a "ZOMGWTFBBQ no one knows what'll happen just don't do that

        I agree. Especially since my $x = $x; relies on it.