Re: proposed addition to perlvar
[email protected] (demerphq)
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <[email protected]> |
On 14 June 2010 10:46, Abigail <[email protected]> wrote: > On Mon, Jun 14, 2010 at 10:04:09AM +0200, demerphq wrote: >> On 9 June 2010 23:08, Abigail <[email protected]> wrote: >> > On Wed, Jun 09, 2010 at 02:52:29PM +0000, Matt Sergeant wrote: >> >> On Tue, 8 Jun 2010, Chas. Owens wrote: >> >> >> >>> How about: >> >>> >> >>> =item $# >> >>> >> >>> C<$#> used to be a variable that could be used to format printed number. >> >>> After a deprecation cycle, its magic was removed in 5.10 and using it >> >>> now triggers a severe warning: C<$# is no longer supported>. >> >>> >> >>> C<$#> is also used as sigil, which, when prepended on the name of an >> >>> array gives the index of the last element in that array. >> >>> >> >>> my @a = ("a", "b", "c"); >> >>> my $last_index = $#a; # $last_index is 2 >> >>> my $last_element = $a[$#a]; # $last_element is "c" >> >> >> >> Do we want to "encourage" this kind of use or prefer to encourage the >> >> [-1] format? >> > >> > >> > Just because a construct is documented does not imply it's encouraged. >> > It's important for documentation to be complete. In a section showing >> > an example of '$#', the example should preferable have as few other >> > things as possible - I would prefer the above over any foreach construct. >> > These lines are about $#. Not foreach. Not ranges. And it is code that >> > people will encounter - and consult the documentation for. >> > >> > Let's stay clear of a -1 vs. $#a preference debate. This part of the >> > documentation is not the place. >> >> 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? 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. Yves -- perl -Mre=debug -e "/just|another|perl|hacker/"