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.
In reply to Re^7: Negative array subscript method call bug?
by BrowserUk
in thread Negative array subscript method call bug?
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |