Re: proposed addition to perlvar

[email protected] (Abigail)
Newsgroups perl.perl5.porters
Message-ID <20100614093937.GG3893@almanda>
On Mon, Jun 14, 2010 at 11:22:58AM +0200, demerphq wrote:
> On 14 June 2010 10:46, Abigail <[email protected]> wrote:
> > On Mon, Jun 14, 2010 at 10:04:09AM +0200, demerphq wrote:
> >>
> >> I agree, but I think it would be the place to note that using $#arry
> >> unguarded by an array size assertion may result in a fatal exception.
> >
> > Fatal exception? Could you provide an example of that?
> >
> >    $ perl -wE 'my @x; say $x [$#x]; END {say "Hello"}'
> >    Use of uninitialized value in say at -e line 1.
> >
> >    Hello
> >    $
> 
> My mail was pre-coffee, and i have to admit i thought that fetching -1
> on an empty array was an error. You have a collection of old perls
> right? Was there an older version where fetching -1 on an empty array
> /was/ an error?

I currently don't have anything older than 5.005_04. In which it wasn't
an error. And I do not believe it ever was an error.

> 
> Anyway, I meant this exception, for /setting/ the last element in an array:
> 
> perl -lwe'use strict; my @array; $array[$#array]=1'
> Modification of non-creatable array value attempted, subscript -1 at -e line 1.
> 

Yes, but that's not a feature of $#.

 $ perl -wE 'my @a; $a [@a - 1] = 1'
 Modification of non-creatable array value attempted, subscript -1 at -e line 1.
 $


Nor is it a feature for empty arrays:

 $ perl -wE 'my @a = (1); $a [@a - 3] = 2;'
 Modification of non-creatable array value attempted, subscript -2 at -e line 1.
 $



Abigail
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.