Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You found a fascinating edge case.</p> <p>When you explicitly set an element, it first springs into existence. If an array is extended so that multiple indices fall into range of this array, no scalars are implicitly initialized at these positions. Example:</p> <pre><code>my @array; $array[2] = undef; # this extends the array # now elements 0–2 report as `undef`, but only #2 was initalized </code></pre> <p>When we ask whether these elements exist, we get:</p> <pre><code>say "index $_ ", exists $array[$_] ? "exists" : "doesn't exist" for 0 .. 4; </code></pre> <p>Output:</p> <pre><code>index 0 doesn't exist index 1 doesn't exist index 2 exists index 3 doesn't exist index 4 doesn't exist </code></pre> <p>This optimization saves you from allocating unused scalars at these positions; the array access code just returns an <code>undef</code> when there isn't anything there stating otherwise.</p> <p>Now this squares badly with function calls. When a subroutine is called, a flat list of scalars is put on the stack, which is then accessible as <code>@_</code>. No copying takes place here, so this is call by alias. Now when the <code>$_[0]</code> element is accessed in your sub, there is no scalar here, so it creates a new one in <code>@_</code>:</p> <pre><code>sub crazy { say 1*exists $_[0]; $_[0] = 1; say 1*exists $_[0]; } my @array; $array[2] = 0; crazy @array; say 1*exists $array[0]; </code></pre> <p>Output:</p> <pre><code>0 1 0 </code></pre> <p>Internally, a scalar is a pointer to an <code>SV</code> struct. These pointers are copied onto the stack, so this makes actual modification of the original <code>@array</code> impossible.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload