in reply to Re^2: ||= oddity
in thread ||= oddity

If it did work, it would produce useless results.
Given that (LHS ||= RHS) means (LHS = LHS || RHS),
Then (@a ||= (1,2,3)) means (@a = @a || (1,2,3)) and thus (@a = $#a+1 || 3).
Why would you ever want that.

On the other hand, it would be useful to expand the definition of the ||= operator so that (@a ||= EXPR) means (@a = @a ? @a : EXPR).
Similarly, it could be useful to expand the definition of &&= such that (@a &&= EXPR) means (@a = @a ? EXPR : ()).
However, none of **=, +=, *=, &=, <<=, -=, /=, |=, >>=, .=, %=, ^=, //= and x= would be useful for arrays.

Replies are listed 'Best First'.
Re^4: ||= oddity
by ambrus (Abbot) on May 30, 2008 at 09:09 UTC

    You're mostly right, but actually LHS || RHS in list context evaluates RHS in list context. Indeed, if you try

    perl -we '@bar = (); @bar = @bar || (1, 2, 3); warn "(@bar)";'
    you see that the array is filled with the right values: (1, 2, 3); but that's still useless for indeed when the array isn't empty at the beginning,
    perl -we '@bar = (42, 5); @bar = @bar || (1, 2, 3); warn "(@bar)";'
    the or operator returns the length of the array so the array will contain only (2).

    Nice trap.

Re^4: ||= oddity
by ambrus (Abbot) on May 30, 2008 at 09:14 UTC

    However, none of **=, +=, *=, &=, <<=, -=, /=, |=, >>=, .=, %=, ^=, //= and x= would be useful for arrays.

    Actually, x= would be:

    $ perl -we '@bar = (42, 5); (@bar) = (@bar) x 5; warn "(@bar)";' (42 5 42 5 42 5 42 5 42 5) at -e line 1. $ perl -we '@bar = (42, 5); (@bar) x= 5; warn "(@bar)";' Can't modify array dereference in repeat (x) at -e line 1, near "5;" Execution of -e aborted due to compilation errors. $