Yes, there are some subtle points that I didn't go into.

First, making @a ||= ... the same as @a = @a || ... would just be leaving a trap. So it is actually a good thing (IMHO) that @a ||= ... is fatal rather than just being identical to @a = @a || .... (And I'm pretty sure that this fatalness was more just a result of implementation details than of extra work to not leave a non-fatal trap.)

I don't recall the details, but I do recall that there are some subtle differences even between $a = $a || ... vs. $a ||= .... The differences boil down to subtle differences in how $a ||= ... is implemented, and these boil down (IMHO) mostly to matters of efficiency of implementation.

$a = $a || ... actually deals with $a twice while $a ||= ... deals with $a once.

So one way to think about why @a ||= ... is fatal is to realize that it isn't just @a = @a || ... but that both of the "@a"s in that expression are dealt with in a single go. So, rather than being like @a = 0+@a || ..., it ends up more like 0+@a = 0+@a || ....

$ perl -e '@a ||= 1..3' Can't modify array dereference in logical or assignment (||=) ... $ perl -e '0+@a = 0+@a || 1..3' Can't modify addition (+) in scalar assignment ...

Or you can think of it more like scalar @a = scalar @a || ..., like you guessed.

If so, it seems strange that perlop#Assignment-Operators doesn’t highlight this important semantic difference

But it isn't an important semantic difference. It is a subtle difference that mostly doesn't matter. The case where it easily matters is when you try to assign to an array. But that difference isn't "important" because the difference is between something that fails loudly and something that does something you didn't intend.

op= is only useful on scalars. So there are some subtle details of implementation that are quite difficult to even intentionally arrange for them to matter when using a scalar. And those details are basically due to optimizations, which makes them much more likely to change in future versions of Perl. So these are not the types of things to try to document.

The thing to document about using op= on an array would be to simply note that it doesn't work. Trying to document inaccurate explanations about subtle implementation details related to that isn't particularly useful.

- tye        


In reply to Re^7: Conditional array initialisation? (documented?) by tye
in thread Conditional array initialisation? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.