I've eventually arrived at the conclusion that there is a bug here.

With my @x = (); my $x = $x[-1];

You don't get the "Modification of non-creatable array value attempted, subscript -1 ... " error, because when Perl attempts to normalise the subscript, it calls FETCHSIZE on the array to find the upper limit from which it will then subtract the negative index to find the actual index.

When the array is empty, FETCHSIZE returns 0, it therefore assumes that it can forgo autovivification of the array element as it will be initialised to undef anyway (and may never be used again), and just sets $x = undef;.

This is a bug because if @x were a tied array, then there is no guarentee that the attempt to FETCH the value of a currently non-existant array element wouldn't autovivify a value other than undef.

However, I think that this is already a known bug for tied arrays: (from Perl5.8.0 delta)

Tied/Magical Array/Hash Elements Do Not Autovivify For normal arrays $foo = \$bar[1] will assign undef to $bar[1] (assumi +ng that it didn't exist before), but for tied/magical arrays and hash +es such autovivification does not happen because there is currently n +o way to catch the reference creation. The same problem affects slici +ng over non-existent indices/keys of a tied/magical array/hash.

With my @x = (); $x[ -1 ]->meth;, Perl knows it needs a reference to the array element (not just it's value), and therefore tries to autovivify the element. At that point, it does the normalisation of the index, which results in a normalised negative index, which is illegal, and issues the error message.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re^7: Negative array subscript method call bug? by BrowserUk
in thread Negative array subscript method call bug? by BUU

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.